OpenAI Compatible Providers
Use OpenAI compatible providers with the AI SDK.
You can use the OpenAI Compatible Provider package to use language model providers that implement the OpenAI API.
Below we focus on the general setup and provider instance creation. You can also write a custom provider package leveraging the OpenAI Compatible package.
We provide detailed documentation for the following OpenAI compatible providers:
The general setup and provider instance creation is the same for all of these providers.
Setup
The OpenAI Compatible provider is available via the @ai-sdk/openai-compatible module. You can install it with:
$ pnpm add @ai-sdk/openai-compatible
$ npm install @ai-sdk/openai-compatible
$ yarn add @ai-sdk/openai-compatible
$ bun add @ai-sdk/openai-compatible
Provider Instance
To use an OpenAI compatible provider, you can create a custom provider instance with the createOpenAICompatible function from @ai-sdk/openai-compatible:
You can use the following optional settings to customize the provider instance:
-
baseURL string
Set the URL prefix for API calls.
-
apiKey string
API key for authenticating requests. If specified, adds an
Authorizationheader to request headers with the valueBearer <apiKey>. This will be added before any headers potentially specified in theheadersoption. -
headers Record<string,string>
Optional custom headers to include in requests. These will be added to request headers after any headers potentially added by use of the
apiKeyoption. -
queryParams Record<string,string>
Optional custom url query parameters to include in request urls.
-
fetch (input: RequestInfo, init?: RequestInit) => Promise<Response>
Custom fetch implementation. Defaults to the global
fetchfunction. You can use it as a middleware to intercept requests, or to provide a custom fetch implementation for e.g. testing. -
includeUsage boolean
Include usage information in streaming responses. When enabled, usage data will be included in the response metadata for streaming requests. Defaults to
undefined(false). -
supportsStructuredOutputs boolean
Set to true if the provider supports structured outputs. Only relevant for
provider(),provider.chatModel(), andprovider.languageModel().
Language Models
You can create provider models using a provider instance.
The first argument is the model id, e.g. model-id.
Example
You can use provider language models to generate text with the generateText function:
Including model ids for auto-completion
Custom query parameters
Some providers may require custom query parameters. An example is the Azure AI
Model Inference
API
which requires an api-version query parameter.
You can set these via the optional queryParams provider setting. These will be
added to all requests made by the provider.
For example, with the above configuration, API requests would include the query parameter in the URL like:
https://api.provider.com/v1/chat/completions?api-version=1.0.0.
Provider-specific options
The OpenAI Compatible provider supports adding provider-specific options to the request body. These are specified with the providerOptions field in the request body.
For example, if you create a provider instance with the name provider-name, you can add a custom-option field to the request body like this:
The request body sent to the provider will include the customOption field with the value magic-value. This gives you an easy way to add provider-specific options to requests without having to modify the provider or AI SDK code.
Custom Metadata Extraction
The OpenAI Compatible provider supports extracting provider-specific metadata from API responses through metadata extractors. These extractors allow you to capture additional information returned by the provider beyond the standard response format.
Metadata extractors receive the raw, unprocessed response data from the provider, giving you complete flexibility to extract any custom fields or experimental features that the provider may include. This is particularly useful when:
- Working with providers that include non-standard response fields
- Experimenting with beta or preview features
- Capturing provider-specific metrics or debugging information
- Supporting rapid provider API evolution without SDK changes
Metadata extractors work with both streaming and non-streaming chat completions and consist of two main components:
- A function to extract metadata from complete responses
- A streaming extractor that can accumulate metadata across chunks in a streaming response
Here's an example metadata extractor that captures both standard and custom provider data:
You can provide a metadata extractor when creating your provider instance:
The extracted metadata will be included in the response under the providerMetadata field:
This allows you to access provider-specific information while maintaining a consistent interface across different providers.