
# OpenAI Compatible Providers

You can use the [OpenAI Compatible Provider](https://www.npmjs.com/package/@ai-sdk/openai-compatible) 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](/providers/openai-compatible-providers/custom-providers).

We provide detailed documentation for the following OpenAI compatible providers:

- [LM Studio](/providers/openai-compatible-providers/lmstudio)
- [NIM](/providers/openai-compatible-providers/nim)
- [Heroku](/providers/openai-compatible-providers/heroku)
- [Clarifai](/providers/openai-compatible-providers/clarifai)

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:

<Tabs items={['pnpm', 'npm', 'yarn', 'bun']}>
  <Tab>
    <Snippet text="pnpm add @ai-sdk/openai-compatible" dark />
  </Tab>
  <Tab>
    <Snippet text="npm install @ai-sdk/openai-compatible" dark />
  </Tab>
  <Tab>
    <Snippet text="yarn add @ai-sdk/openai-compatible" dark />
  </Tab>

  <Tab>
    <Snippet text="bun add @ai-sdk/openai-compatible" dark />
  </Tab>
</Tabs>

## Provider Instance

To use an OpenAI compatible provider, you can create a custom provider instance with the `createOpenAICompatible` function from `@ai-sdk/openai-compatible`:

```ts
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';

const provider = createOpenAICompatible({
  name: 'provider-name',
  apiKey: process.env.PROVIDER_API_KEY,
  baseURL: 'https://api.provider.com/v1',
  includeUsage: true, // Include usage information in streaming responses
});
```

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 `Authorization`
  header to request headers with the value `Bearer <apiKey>`. This will be added
  before any headers potentially specified in the `headers` option.

- **headers** _Record&lt;string,string&gt;_

  Optional custom headers to include in requests. These will be added to request headers
  after any headers potentially added by use of the `apiKey` option.

- **queryParams** _Record&lt;string,string&gt;_

  Optional custom url query parameters to include in request urls.

- **fetch** _(input: RequestInfo, init?: RequestInit) => Promise&lt;Response&gt;_

  Custom [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch) implementation.
  Defaults to the global `fetch` function.
  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()`, and `provider.languageModel()`.

## Language Models

You can create provider models using a provider instance.
The first argument is the model id, e.g. `model-id`.

```ts
const model = provider('model-id');
```

### Example

You can use provider language models to generate text with the `generateText` function:

```ts
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { generateText } from 'ai';

const provider = createOpenAICompatible({
  name: 'provider-name',
  apiKey: process.env.PROVIDER_API_KEY,
  baseURL: 'https://api.provider.com/v1',
});

const { text } = await generateText({
  model: provider('model-id'),
  prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});
```

### Including model ids for auto-completion

```ts
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { generateText } from 'ai';

type ExampleChatModelIds =
  | 'meta-llama/Llama-3-70b-chat-hf'
  | 'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo'
  | (string & {});

type ExampleCompletionModelIds =
  | 'codellama/CodeLlama-34b-Instruct-hf'
  | 'Qwen/Qwen2.5-Coder-32B-Instruct'
  | (string & {});

type ExampleEmbeddingModelIds =
  | 'BAAI/bge-large-en-v1.5'
  | 'bert-base-uncased'
  | (string & {});

const model = createOpenAICompatible<
  ExampleChatModelIds,
  ExampleCompletionModelIds,
  ExampleEmbeddingModelIds
>({
  name: 'example',
  apiKey: process.env.PROVIDER_API_KEY,
  baseURL: 'https://api.example.com/v1',
});

// Subsequent calls to e.g. `model.chatModel` will auto-complete the model id
// from the list of `ExampleChatModelIds` while still allowing free-form
// strings as well.

const { text } = await generateText({
  model: model.chatModel('meta-llama/Llama-3-70b-chat-hf'),
  prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});
```

### Custom query parameters

Some providers may require custom query parameters. An example is the [Azure AI
Model Inference
API](https://learn.microsoft.com/en-us/azure/machine-learning/reference-model-inference-chat-completions?view=azureml-api-2)
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.

```ts highlight="7-9"
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';

const provider = createOpenAICompatible({
  name: 'provider-name',
  apiKey: process.env.PROVIDER_API_KEY,
  baseURL: 'https://api.provider.com/v1',
  queryParams: {
    'api-version': '1.0.0',
  },
});
```

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:

```ts
const provider = createOpenAICompatible({
  name: 'provider-name',
  apiKey: process.env.PROVIDER_API_KEY,
  baseURL: 'https://api.provider.com/v1',
});

const { text } = await generateText({
  model: provider('model-id'),
  prompt: 'Hello',
  providerOptions: {
    'provider-name': { customOption: 'magic-value' },
  },
});
```

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:

1. A function to extract metadata from complete responses
2. 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:

```typescript
const myMetadataExtractor: MetadataExtractor = {
  // Process complete, non-streaming responses
  extractMetadata: ({ parsedBody }) => {
    // You have access to the complete raw response
    // Extract any fields the provider includes
    return {
      myProvider: {
        standardUsage: parsedBody.usage,
        experimentalFeatures: parsedBody.beta_features,
        customMetrics: {
          processingTime: parsedBody.server_timing?.total_ms,
          modelVersion: parsedBody.model_version,
          // ... any other provider-specific data
        },
      },
    };
  },

  // Process streaming responses
  createStreamExtractor: () => {
    let accumulatedData = {
      timing: [],
      customFields: {},
    };

    return {
      // Process each chunk's raw data
      processChunk: parsedChunk => {
        if (parsedChunk.server_timing) {
          accumulatedData.timing.push(parsedChunk.server_timing);
        }
        if (parsedChunk.custom_data) {
          Object.assign(accumulatedData.customFields, parsedChunk.custom_data);
        }
      },
      // Build final metadata from accumulated data
      buildMetadata: () => ({
        myProvider: {
          streamTiming: accumulatedData.timing,
          customData: accumulatedData.customFields,
        },
      }),
    };
  },
};
```

You can provide a metadata extractor when creating your provider instance:

```typescript
const provider = createOpenAICompatible({
  name: 'my-provider',
  apiKey: process.env.PROVIDER_API_KEY,
  baseURL: 'https://api.provider.com/v1',
  metadataExtractor: myMetadataExtractor,
});
```

The extracted metadata will be included in the response under the `providerMetadata` field:

```typescript
const { text, providerMetadata } = await generateText({
  model: provider('model-id'),
  prompt: 'Hello',
});

console.log(providerMetadata.myProvider.customMetric);
```

This allows you to access provider-specific information while maintaining a consistent interface across different providers.


## Navigation

- [AI SDK Providers](/v5/providers/ai-sdk-providers)
  - [AI Gateway](/v5/providers/ai-sdk-providers/ai-gateway)
  - [xAI Grok](/v5/providers/ai-sdk-providers/xai)
  - [Vercel](/v5/providers/ai-sdk-providers/vercel)
  - [OpenAI](/v5/providers/ai-sdk-providers/openai)
  - [Azure OpenAI](/v5/providers/ai-sdk-providers/azure)
  - [Anthropic](/v5/providers/ai-sdk-providers/anthropic)
  - [Amazon Bedrock](/v5/providers/ai-sdk-providers/amazon-bedrock)
  - [Groq](/v5/providers/ai-sdk-providers/groq)
  - [Fal](/v5/providers/ai-sdk-providers/fal)
  - [AssemblyAI](/v5/providers/ai-sdk-providers/assemblyai)
  - [DeepInfra](/v5/providers/ai-sdk-providers/deepinfra)
  - [Deepgram](/v5/providers/ai-sdk-providers/deepgram)
  - [Black Forest Labs](/v5/providers/ai-sdk-providers/black-forest-labs)
  - [Gladia](/v5/providers/ai-sdk-providers/gladia)
  - [LMNT](/v5/providers/ai-sdk-providers/lmnt)
  - [Google Generative AI](/v5/providers/ai-sdk-providers/google-generative-ai)
  - [Hume](/v5/providers/ai-sdk-providers/hume)
  - [Google Vertex AI](/v5/providers/ai-sdk-providers/google-vertex)
  - [Rev.ai](/v5/providers/ai-sdk-providers/revai)
  - [Baseten](/v5/providers/ai-sdk-providers/baseten)
  - [Hugging Face](/v5/providers/ai-sdk-providers/huggingface)
  - [Mistral AI](/v5/providers/ai-sdk-providers/mistral)
  - [Together.ai](/v5/providers/ai-sdk-providers/togetherai)
  - [Cohere](/v5/providers/ai-sdk-providers/cohere)
  - [Fireworks](/v5/providers/ai-sdk-providers/fireworks)
  - [DeepSeek](/v5/providers/ai-sdk-providers/deepseek)
  - [Moonshot AI](/v5/providers/ai-sdk-providers/moonshotai)
  - [Alibaba](/v5/providers/ai-sdk-providers/alibaba)
  - [Cerebras](/v5/providers/ai-sdk-providers/cerebras)
  - [Replicate](/v5/providers/ai-sdk-providers/replicate)
  - [Perplexity](/v5/providers/ai-sdk-providers/perplexity)
  - [Luma](/v5/providers/ai-sdk-providers/luma)
  - [ElevenLabs](/v5/providers/ai-sdk-providers/elevenlabs)
- [OpenAI Compatible Providers](/v5/providers/openai-compatible-providers)
  - [Writing a Custom Provider](/v5/providers/openai-compatible-providers/custom-providers)
  - [LM Studio](/v5/providers/openai-compatible-providers/lmstudio)
  - [NVIDIA NIM](/v5/providers/openai-compatible-providers/nim)
  - [Clarifai](/v5/providers/openai-compatible-providers/clarifai)
  - [Heroku](/v5/providers/openai-compatible-providers/heroku)
- [Community Providers](/v5/providers/community-providers)
  - [Writing a Custom Provider](/v5/providers/community-providers/custom-providers)
  - [Qwen](/v5/providers/community-providers/qwen)
  - [Ollama](/v5/providers/community-providers/ollama)
  - [A2A](/v5/providers/community-providers/a2a)
  - [ACP (Agent Client Protocol)](/v5/providers/community-providers/acp)
  - [Helicone](/v5/providers/community-providers/helicone)
  - [FriendliAI](/v5/providers/community-providers/friendliai)
  - [Portkey](/v5/providers/community-providers/portkey)
  - [Built-in AI](/v5/providers/community-providers/built-in-ai)
  - [Gemini CLI](/v5/providers/community-providers/gemini-cli)
  - [MCP Sampling AI Provider](/v5/providers/community-providers/mcp-sampling)
  - [Automatic1111](/v5/providers/community-providers/automatic1111)
  - [Cloudflare Workers AI](/v5/providers/community-providers/cloudflare-workers-ai)
  - [Cloudflare AI Gateway](/v5/providers/community-providers/cloudflare-ai-gateway)
  - [OpenRouter](/v5/providers/community-providers/openrouter)
  - [Azure AI](/v5/providers/community-providers/azure-ai)
  - [Aihubmix](/v5/providers/community-providers/aihubmix)
  - [SAP AI Core](/v5/providers/community-providers/sap-ai)
  - [Crosshatch](/v5/providers/community-providers/crosshatch)
  - [Requesty](/v5/providers/community-providers/requesty)
  - [MiniMax](/v5/providers/community-providers/minimax)
  - [Mixedbread](/v5/providers/community-providers/mixedbread)
  - [Voyage AI](/v5/providers/community-providers/voyage-ai)
  - [Jina AI](/v5/providers/community-providers/jina-ai)
  - [Mem0](/v5/providers/community-providers/mem0)
  - [Letta](/v5/providers/community-providers/letta)
  - [Supermemory](/v5/providers/community-providers/supermemory)
  - [React Native Apple](/v5/providers/community-providers/react-native-apple)
  - [Anthropic Vertex](/v5/providers/community-providers/anthropic-vertex-ai)
  - [Spark](/v5/providers/community-providers/spark)
  - [Inflection AI](/v5/providers/community-providers/inflection-ai)
  - [LangDB](/v5/providers/community-providers/langdb)
  - [Zhipu AI](/v5/providers/community-providers/zhipu)
  - [SambaNova](/v5/providers/community-providers/sambanova)
  - [Dify](/v5/providers/community-providers/dify)
  - [Sarvam](/v5/providers/community-providers/sarvam)
  - [AI/ML API](/v5/providers/community-providers/aimlapi)
  - [Claude Code](/v5/providers/community-providers/claude-code)
- [Adapters](/v5/providers/adapters)
  - [LangChain](/v5/providers/adapters/langchain)
  - [LlamaIndex](/v5/providers/adapters/llamaindex)
- [Observability Integrations](/v5/providers/observability)
  - [Axiom](/v5/providers/observability/axiom)
  - [Braintrust](/v5/providers/observability/braintrust)
  - [Helicone](/v5/providers/observability/helicone)
  - [Laminar](/v5/providers/observability/laminar)
  - [Langfuse](/v5/providers/observability/langfuse)
  - [LangSmith](/v5/providers/observability/langsmith)
  - [LangWatch](/v5/providers/observability/langwatch)
  - [Maxim](/v5/providers/observability/maxim)
  - [Patronus](/v5/providers/observability/patronus)
  - [Scorecard](/v5/providers/observability/scorecard)
  - [SigNoz](/v5/providers/observability/signoz)
  - [Traceloop](/v5/providers/observability/traceloop)
  - [Weave](/v5/providers/observability/weave)


[Full Sitemap](/sitemap.md)
