Prerequisites
This guide uses the generic@ai-sdk/openai-compatible adapter to connect AI SDK’s generateText to this project’s Chat Completions endpoint. Use Node.js 22.18+ or 24 LTS, an API key, and a model verified for Chat. Run the key and script only on a server or in a terminal.
The documentation directory pins ai@7.0.141 and @ai-sdk/openai-compatible@3.0.69; install with npm ci. In your own Node project, install:
provider.chatModel() and does not automatically switch to Responses.
Complete runnable example
Save asexamples/ai-sdk.mts, then run:
/v1 in the Base URL, excluding /chat/completions. This text example accepts one completed plain-text answer only. Refusal, tool calls, truncation, or empty text produce a nonzero exit without printing partial content as a successful result.
Key settings
Enable extensions individually
This script verifies non-streaming text only. Streaming, tools, structured output, vision, and embeddings need separate configuration and model verification. Adapter options such asincludeUsage and supportsStructuredOutputs cannot create upstream capabilities; enable them after confirming channel support. Browser useChat, framework route handlers, and SDK data-stream protocols also require a separate server implementation. Never pass the API key directly to the browser.
For protocol details, see Chat API, streaming, tool calling, and structured output.
Local validation includes strict type checking of all .mts examples and real SDK requests against a local HTTP fixture for this script. It does not validate real models. References: official adapter documentation and generateText parameters.
阅读中文