UNPKG

@ai-sdk/openai

Version:

The **[OpenAI provider](https://ai-sdk.dev/providers/ai-sdk-providers/openai)** for the [AI SDK](https://ai-sdk.dev/docs) contains language model support for the OpenAI chat and completion APIs and embedding model support for the OpenAI embeddings API.

1,439 lines (1,155 loc) 98 kB
--- title: OpenAI description: Learn how to use the OpenAI provider for the AI SDK. --- # OpenAI Provider The [OpenAI](https://openai.com/) provider contains language model support for the OpenAI responses, chat, and completion APIs, as well as embedding model support for the OpenAI embeddings API. ## Setup The OpenAI provider is available in the `@ai-sdk/openai` module. You can install it with <InstallPackages packages="@ai-sdk/openai" /> ## Provider Instance You can import the default provider instance `openai` from `@ai-sdk/openai`: ```ts import { openai } from '@ai-sdk/openai'; ``` If you need a customized setup, you can import `createOpenAI` from `@ai-sdk/openai` and create a provider instance with your settings: ```ts import { createOpenAI } from '@ai-sdk/openai'; const openai = createOpenAI({ // custom settings, e.g. headers: { 'header-name': 'header-value', }, }); ``` You can use the following optional settings to customize the OpenAI provider instance: - **baseURL** _string_ Use a different URL prefix for API calls, e.g. to use proxy servers. The default prefix is `https://api.openai.com/v1`. The default OpenAI model factory (`openai('model-id')`) uses the Responses API. If your custom base URL only supports the Chat Completions API, create chat models with `openai.chat('model-id')` instead, or use the [OpenAI-compatible provider](/providers/openai-compatible-providers). - **apiKey** _string_ API key that is being sent using the `Authorization` header. It defaults to the `OPENAI_API_KEY` environment variable. - **name** _string_ The provider name. You can set this when using OpenAI compatible providers to change the model provider property. Defaults to `openai`. - **organization** _string_ OpenAI Organization. - **project** _string_ OpenAI project. - **headers** _Record&lt;string,string&gt;_ Custom headers to include in the requests. - **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. ## Language Models The OpenAI provider instance is a function that you can invoke to create a language model: ```ts const model = openai('gpt-5'); ``` It automatically selects the correct API based on the model id. You can also pass additional settings in the second argument: ```ts const model = openai('gpt-5', { // additional settings }); ``` The available options depend on the API that's automatically chosen for the model (see below). If you want to explicitly select a specific model API, you can use `.responses`, `.chat`, or `.completion`. <Note> Since AI SDK 5, the OpenAI responses API is called by default (unless you specify e.g. 'openai.chat') </Note> ### Example You can use OpenAI language models to generate text with the `generateText` function: ```ts import { openai } from '@ai-sdk/openai'; import { generateText } from 'ai'; const { text } = await generateText({ model: openai('gpt-5'), prompt: 'Write a vegetarian lasagna recipe for 4 people.', }); ``` OpenAI language models can also be used in the `streamText` function and support structured data generation with [`Output`](/docs/reference/ai-sdk-core/output) (see [AI SDK Core](/docs/ai-sdk-core)). ### Responses Models You can use the OpenAI responses API with the `openai(modelId)` or `openai.responses(modelId)` factory methods. It is the default API that is used by the OpenAI provider (since AI SDK 5). ```ts const model = openai('gpt-5'); ``` Further configuration can be done using OpenAI provider options. You can validate the provider options using the `OpenAILanguageModelResponsesOptions` type. ```ts import { openai, OpenAILanguageModelResponsesOptions } from '@ai-sdk/openai'; import { generateText } from 'ai'; const result = await generateText({ model: openai('gpt-5'), // or openai.responses('gpt-5') providerOptions: { openai: { parallelToolCalls: false, store: false, user: 'user_123', // ... } satisfies OpenAILanguageModelResponsesOptions, }, // ... }); ``` The following provider options are available: - **parallelToolCalls** _boolean_ Whether to use parallel tool calls. Defaults to `true`. - **store** _boolean_ Whether to store the generation. Defaults to `true`. - **passThroughUnsupportedFiles** _boolean_ Whether to pass through non-image file types as generic input files. Defaults to `false`, which restricts inline file inputs to images and PDFs. Enable this when the target OpenAI Responses model supports additional file media types. - **maxToolCalls** _integer_ The maximum number of total calls to built-in tools that can be processed in a response. This maximum number applies across all built-in tool calls, not per individual tool. Any further attempts to call a tool by the model will be ignored. - **metadata** _Record&lt;string, string&gt;_ Additional metadata to store with the generation. - **conversation** _string_ The ID of the OpenAI Conversation to continue. You must create a conversation first via the [OpenAI API](https://platform.openai.com/docs/api-reference/conversations/create). Cannot be used in conjunction with `previousResponseId`. Defaults to `undefined`. - **previousResponseId** _string_ The ID of the previous response. You can use it to continue a conversation. Defaults to `undefined`. - **instructions** _string_ Instructions for the model. They can be used to change the system or developer message when continuing a conversation using the `previousResponseId` option. Defaults to `undefined`. - **logprobs** _boolean | number_ Return the log probabilities of the tokens. Including logprobs will increase the response size and can slow down response times. However, it can be useful to better understand how the model is behaving. Setting to `true` returns the log probabilities of the tokens that were generated. Setting to a number (1-20) returns the log probabilities of the top n tokens that were generated. - **user** _string_ A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Defaults to `undefined`. - **reasoningEffort** _'none' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh' | 'max'_ Reasoning effort for reasoning models. Defaults to `medium`. If you use `providerOptions` to set the `reasoningEffort` option, this model setting will be ignored. <Note> Supported reasoning efforts vary by model. GPT-5.6 supports `'none'`, `'low'`, `'medium'`, `'high'`, `'xhigh'`, and `'max'`. </Note> - **reasoningMode** _'standard' | 'pro'_ Controls how much model work GPT-5.6 performs before returning a final answer. `'standard'` is the default. Use `'pro'` for difficult tasks where quality matters more than latency and token usage. - **reasoningContext** _'auto' | 'current_turn' | 'all_turns'_ Controls which available reasoning items GPT-5.6 can reuse. `'auto'` uses the model default, `'current_turn'` excludes reasoning from earlier turns, and `'all_turns'` makes compatible earlier reasoning available. The effective context is returned as `providerMetadata.openai.reasoningContext`. - **reasoningSummary** _'auto' | 'detailed'_ Controls whether the model returns its reasoning process. Set to `'auto'` for a condensed summary, `'detailed'` for more comprehensive reasoning. Defaults to `undefined` (no reasoning summaries). When enabled, reasoning summaries appear in the stream as events with type `'reasoning'` and in non-streaming responses within the `reasoning` field. - **strictJsonSchema** _boolean_ Whether to use strict JSON schema validation. Defaults to `true`. <Note type="warning"> OpenAI structured outputs have several [limitations](https://openai.com/index/introducing-structured-outputs-in-the-api), in particular around the [supported schemas](https://platform.openai.com/docs/guides/structured-outputs/supported-schemas), and are therefore opt-in. For example, optional schema properties are not supported. You need to change Zod `.nullish()` and `.optional()` to `.nullable()`. </Note> - **serviceTier** _'auto' | 'flex' | 'priority' | 'default'_ Service tier for the request. Set to 'flex' for 50% cheaper processing at the cost of increased latency (available for o3, o4-mini, and gpt-5 models). Set to 'priority' for faster processing with Enterprise access (available for gpt-4, gpt-5, gpt-5-mini, o3, o4-mini; gpt-5-nano is not supported). Defaults to 'auto'. - **textVerbosity** _'low' | 'medium' | 'high'_ Controls the verbosity of the model's response. Lower values result in more concise responses, while higher values result in more verbose responses. Defaults to `'medium'`. - **include** _Array&lt;string&gt;_ Specifies additional content to include in the response. Supported values: `['file_search_call.results']` for including file search results in responses. `['message.output_text.logprobs']` for logprobs. Defaults to `undefined`. - **truncation** _string_ The truncation strategy to use for the model response. - Auto: If the input to this Response exceeds the model's context window size, the model will truncate the response to fit the context window by dropping items from the beginning of the conversation. - disabled (default): If the input size will exceed the context window size for a model, the request will fail with a 400 error. - **promptCacheKey** _string_ A cache key for manual prompt caching control. Used by OpenAI to cache responses for similar requests to optimize your cache hit rates. - **promptCacheOptions** _object_ Configures prompt caching for GPT-5.6 and later models. `mode` can be `'implicit'` or `'explicit'`, and `ttl` currently only supports `'30m'`. In explicit mode, only content blocks marked with a prompt cache breakpoint are cached. - **promptCacheRetention** _'in_memory' | '24h'_ The legacy retention policy for models before GPT-5.6. Set to `'24h'` to enable extended prompt caching on supported models. For GPT-5.6 and later models, use `promptCacheOptions.ttl` instead. - **safetyIdentifier** _string_ A stable identifier used to help detect users of your application that may be violating OpenAI's usage policies. The IDs should be a string that uniquely identifies each user. - **systemMessageMode** _'system' | 'developer' | 'remove'_ Controls the role of the system message when making requests. By default (when omitted), for models that support reasoning the `system` message is automatically converted to a `developer` message. Setting `systemMessageMode` to `system` passes the system message as a system-level instruction; `developer` passes it as a developer message; `remove` omits the system message from the request. - **forceReasoning** _boolean_ Force treating this model as a reasoning model. This is useful for "stealth" reasoning models (e.g. via a custom baseURL) where the model ID is not recognized by the SDK's allowlist. When enabled, the SDK applies reasoning-model parameter compatibility rules and defaults `systemMessageMode` to `developer` unless overridden. - **contextManagement** _Array&lt;object&gt;_ Enable server-side context management (compaction). When configured, the server automatically compresses conversation context when token usage crosses a specified threshold. Each object in the array should have: - `type`: `'compaction'` - `compactThreshold`: _number_ — the token count at which compaction is triggered The OpenAI responses provider also returns provider-specific metadata: For Responses models, you can type this metadata using `OpenaiResponsesProviderMetadata`: ```ts import { openai, type OpenaiResponsesProviderMetadata } from '@ai-sdk/openai'; import { generateText } from 'ai'; const result = await generateText({ model: openai('gpt-5'), }); const providerMetadata = result.providerMetadata as | OpenaiResponsesProviderMetadata | undefined; const { responseId, logprobs, serviceTier, reasoningContext } = providerMetadata?.openai ?? {}; // responseId can be used to continue a conversation (previousResponseId). console.log(responseId); ``` The following OpenAI-specific metadata may be returned: - **responseId** _string | null | undefined_ The ID of the response. Can be used to continue a conversation. - **logprobs** _(optional)_ Log probabilities of output tokens (when enabled). - **serviceTier** _(optional)_ Service tier information returned by the API. - **reasoningContext** _(optional)_ Effective persisted-reasoning context returned by GPT-5.6 (`'current_turn'` or `'all_turns'`). #### Reasoning Output For reasoning models like `gpt-5`, you can enable reasoning summaries to see the model's thought process. Different models support different summarizers—for example, `o4-mini` supports detailed summaries. Set `reasoningSummary: "auto"` to automatically receive the richest level available. When `reasoningEffort` is set to a value other than `'none'`, the OpenAI Responses provider defaults `reasoningSummary` to `'detailed'`; set `reasoningSummary: null` to omit reasoning summaries. ```ts highlight="8-9,16" import { openai, type OpenAILanguageModelResponsesOptions, } from '@ai-sdk/openai'; import { streamText } from 'ai'; const result = streamText({ model: openai('gpt-5'), prompt: 'Tell me about the Mission burrito debate in San Francisco.', providerOptions: { openai: { reasoningSummary: 'detailed', // 'auto' for condensed or 'detailed' for comprehensive } satisfies OpenAILanguageModelResponsesOptions, }, }); for await (const part of result.stream) { if (part.type === 'reasoning') { console.log(`Reasoning: ${part.textDelta}`); } else if (part.type === 'text-delta') { process.stdout.write(part.textDelta); } } ``` For non-streaming calls with `generateText`, the reasoning summaries are available in the `reasoning` field of the response: ```ts highlight="8-9,13" import { openai, type OpenAILanguageModelResponsesOptions, } from '@ai-sdk/openai'; import { generateText } from 'ai'; const result = await generateText({ model: openai('gpt-5'), prompt: 'Tell me about the Mission burrito debate in San Francisco.', providerOptions: { openai: { reasoningSummary: 'auto', } satisfies OpenAILanguageModelResponsesOptions, }, }); console.log('Reasoning:', result.reasoning); ``` Learn more about reasoning summaries in the [OpenAI documentation](https://platform.openai.com/docs/guides/reasoning?api-mode=responses#reasoning-summaries). #### WebSocket Transport OpenAI's [WebSocket API](https://developers.openai.com/api/docs/guides/websocket-mode) keeps a persistent connection open, which can significantly reduce Time-to-First-Byte (TTFB) in agentic workflows with many tool calls. After the initial connection, subsequent requests skip TCP/TLS/HTTP negotiation entirely. The [`ai-sdk-openai-websocket-fetch`](https://www.npmjs.com/package/ai-sdk-openai-websocket-fetch) package provides a drop-in `fetch` replacement that routes streaming requests through a persistent WebSocket connection. <InstallPackages packages="ai-sdk-openai-websocket-fetch" /> Pass the WebSocket fetch to `createOpenAI` via the `fetch` option: ```ts highlight="2,6-7,15" import { createOpenAI } from '@ai-sdk/openai'; import { createWebSocketFetch } from 'ai-sdk-openai-websocket-fetch'; import { streamText } from 'ai'; // Create a WebSocket-backed fetch instance const wsFetch = createWebSocketFetch(); const openai = createOpenAI({ fetch: wsFetch }); const result = streamText({ model: openai('gpt-4.1-mini'), prompt: 'Hello!', tools: { // ... }, onFinish: () => wsFetch.close(), // close the WebSocket when done }); ``` The first request will be slower because it must establish the WebSocket connection (DNS + TCP + TLS + WebSocket upgrade). After that, subsequent steps in a multi-step tool-calling loop reuse the open connection, resulting in lower TTFB per step. <Note> The WebSocket transport only routes streaming requests to the OpenAI Responses API (`POST /responses` with `stream: true`) through the WebSocket. All other requests (non-streaming, embeddings, etc.) fall through to the standard `fetch` implementation. </Note> You can see a live side-by-side comparison of HTTP vs WebSocket streaming performance in the [demo app](https://github.com/vercel-labs/ai-sdk-openai-websocket). #### Verbosity Control You can control the length and detail of model responses using the `textVerbosity` parameter: ```ts import { openai, type OpenAILanguageModelResponsesOptions, } from '@ai-sdk/openai'; import { generateText } from 'ai'; const result = await generateText({ model: openai('gpt-5-mini'), prompt: 'Write a poem about a boy and his first pet dog.', providerOptions: { openai: { textVerbosity: 'low', // 'low' for concise, 'medium' (default), or 'high' for verbose } satisfies OpenAILanguageModelResponsesOptions, }, }); ``` The `textVerbosity` parameter scales output length without changing the underlying prompt: - `'low'`: Produces terse, minimal responses - `'medium'`: Balanced detail (default) - `'high'`: Verbose responses with comprehensive detail #### Namespaced Function Calls OpenAI supports grouping related function tools into [namespaces](https://developers.openai.com/api/docs/guides/function-calling#defining-namespaces). When the Responses API returns a `function_call` with a `namespace`, the OpenAI provider exposes this value on the generated `tool-call` part as `providerMetadata.openai.namespace`. ```ts for (const part of result.content) { if (part.type === 'tool-call') { console.log(part.providerMetadata?.openai?.namespace); } } ``` When using `streamText`, the namespace is available on the `tool-input-end` event and on the final `tool-call` event: ```ts for await (const part of result.stream) { if (part.type === 'tool-input-end' || part.type === 'tool-call') { console.log(part.providerMetadata?.openai?.namespace); } } ``` If you persist or reconstruct messages for later turns, preserve the OpenAI provider metadata on tool-call parts. The SDK uses `providerMetadata.openai.namespace` or `providerOptions.openai.namespace` to round-trip the namespace back to OpenAI on subsequent requests. #### Web Search Tool The OpenAI responses API supports web search through the `openai.tools.webSearch` tool. ```ts const result = await generateText({ model: openai('gpt-5'), prompt: 'What happened in San Francisco last week?', tools: { web_search: openai.tools.webSearch({ // optional configuration: externalWebAccess: true, searchContextSize: 'high', userLocation: { type: 'approximate', city: 'San Francisco', region: 'California', }, filters: { allowedDomains: ['sfchronicle.com', 'sfgate.com'], }, }), }, // Force web search tool (optional): toolChoice: { type: 'tool', toolName: 'web_search' }, }); // URL sources directly from `results` const sources = result.sources; // Or access sources from tool results for (const toolResult of result.toolResults) { if (toolResult.toolName === 'web_search') { console.log('Query:', toolResult.output.action.query); console.log('Sources:', toolResult.output.sources); // `sources` is an array of object: { type: 'url', url: string } } } ``` The web search tool supports the following configuration options: - **externalWebAccess** _boolean_ - Whether to use external web access for fetching live content. Defaults to `true`. - **searchContextSize** _'low' | 'medium' | 'high'_ - Controls the amount of context used for the search. Higher values provide more comprehensive results but may have higher latency and cost. - **userLocation** - Optional location information to provide geographically relevant results. Includes `type` (always `'approximate'`), `country`, `city`, `region`, and `timezone`. - **filters** - Optional filter configuration to restrict search results. - **allowedDomains** _string[]_ - Array of allowed domains for the search. Subdomains of the provided domains are automatically included. For detailed information on configuration options see the [OpenAI Web Search Tool documentation](https://platform.openai.com/docs/guides/tools-web-search?api-mode=responses). #### File Search Tool The OpenAI responses API supports file search through the `openai.tools.fileSearch` tool. You can force the use of the file search tool by setting the `toolChoice` parameter to `{ type: 'tool', toolName: 'file_search' }`. ```ts const result = await generateText({ model: openai('gpt-5'), prompt: 'What does the document say about user authentication?', tools: { file_search: openai.tools.fileSearch({ vectorStoreIds: ['vs_123'], // configuration below is optional: maxNumResults: 5, filters: { key: 'author', type: 'eq', value: 'Jane Smith', }, ranking: { ranker: 'auto', scoreThreshold: 0.5, }, }), }, providerOptions: { openai: { // optional: include results include: ['file_search_call.results'], } satisfies OpenAILanguageModelResponsesOptions, }, }); ``` The file search tool supports filtering with both comparison and compound filters: **Comparison filters** - Filter by a single attribute: - `eq` - Equal to - `ne` - Not equal to - `gt` - Greater than - `gte` - Greater than or equal to - `lt` - Less than - `lte` - Less than or equal to - `in` - Value is in array - `nin` - Value is not in array ```ts // Single comparison filter filters: { key: 'year', type: 'gte', value: 2023 } // Filter with array values filters: { key: 'status', type: 'in', value: ['published', 'reviewed'] } ``` **Compound filters** - Combine multiple filters with `and` or `or`: ```ts // Compound filter with AND filters: { type: 'and', filters: [ { key: 'author', type: 'eq', value: 'Jane Smith' }, { key: 'year', type: 'gte', value: 2023 }, ], } // Compound filter with OR filters: { type: 'or', filters: [ { key: 'department', type: 'eq', value: 'Engineering' }, { key: 'department', type: 'eq', value: 'Research' }, ], } ``` #### Image Generation Tool OpenAI's Responses API supports multi-modal image generation as a provider-defined tool. Availability is restricted to specific models (for example, `gpt-5` variants). You can use the image tool with either `generateText` or `streamText`: ```ts import { openai } from '@ai-sdk/openai'; import { generateText } from 'ai'; const result = await generateText({ model: openai('gpt-5'), prompt: 'Generate an image of an echidna swimming across the Mozambique channel.', tools: { image_generation: openai.tools.imageGeneration({ outputFormat: 'webp' }), }, }); for (const toolResult of result.staticToolResults) { if (toolResult.toolName === 'image_generation') { const base64Image = toolResult.output.result; } } ``` ```ts import { openai } from '@ai-sdk/openai'; import { streamText } from 'ai'; const result = streamText({ model: openai('gpt-5'), prompt: 'Generate an image of an echidna swimming across the Mozambique channel.', tools: { image_generation: openai.tools.imageGeneration({ outputFormat: 'webp', quality: 'low', }), }, }); for await (const part of result.stream) { if (part.type == 'tool-result' && !part.dynamic) { const base64Image = part.output.result; } } ``` <Note> When you set `store: false`, then previously generated images will not be accessible by the model. We recommend using the image generation tool without setting `store: false`. </Note> For complete details on model availability, image quality controls, supported sizes, and tool-specific parameters, refer to the OpenAI documentation: - Image generation overview and models: [OpenAI Image Generation](https://platform.openai.com/docs/guides/image-generation) - Image generation tool parameters (background, size, quality, format, etc.): [Image Generation Tool Options](https://platform.openai.com/docs/guides/tools-image-generation#tool-options) #### Code Interpreter Tool The OpenAI responses API supports the code interpreter tool through the `openai.tools.codeInterpreter` tool. This allows models to write and execute Python code. ```ts import { openai } from '@ai-sdk/openai'; import { generateText } from 'ai'; const result = await generateText({ model: openai('gpt-5'), prompt: 'Write and run Python code to calculate the factorial of 10', tools: { code_interpreter: openai.tools.codeInterpreter({ // optional configuration: container: { fileIds: ['file-123', 'file-456'], // optional file IDs to make available }, }), }, }); ``` The code interpreter tool can be configured with: - **container**: Either a container ID string or an object with `fileIds` to specify uploaded files that should be available to the code interpreter <Note> When working with files generated by the Code Interpreter, reference information can be obtained from both [annotations in Text Parts](#typed-providermetadata-in-text-parts) and [`providerMetadata` in Source Document Parts](#typed-providermetadata-in-source-document-parts). </Note> #### MCP Tool The OpenAI responses API supports connecting to [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) servers through the `openai.tools.mcp` tool. This allows models to call tools exposed by remote MCP servers or service connectors. ```ts import { openai } from '@ai-sdk/openai'; import { generateText } from 'ai'; const result = await generateText({ model: openai('gpt-5'), prompt: 'Search the web for the latest news about AI developments', tools: { mcp: openai.tools.mcp({ serverLabel: 'web-search', serverUrl: 'https://mcp.exa.ai/mcp', serverDescription: 'A web-search API for AI agents', }), }, }); ``` The MCP tool can be configured with: - **serverLabel** _string_ (required) A label to identify the MCP server. This label is used in tool calls to distinguish between multiple MCP servers. - **serverUrl** _string_ (required if `connectorId` is not provided) The URL for the MCP server. Either `serverUrl` or `connectorId` must be provided. - **connectorId** _string_ (required if `serverUrl` is not provided) Identifier for a service connector. Either `serverUrl` or `connectorId` must be provided. - **serverDescription** _string_ (optional) Optional description of the MCP server that helps the model understand its purpose. - **allowedTools** _string[] | object_ (optional) Controls which tools from the MCP server are available. Can be: - An array of tool names: `['tool1', 'tool2']` - An object with filters: ```ts { readOnly: true, // Only allow read-only tools toolNames: ['tool1', 'tool2'] // Specific tool names } ``` - **authorization** _string_ (optional) OAuth access token for authenticating with the MCP server or connector. - **headers** _Record&lt;string, string&gt;_ (optional) Optional HTTP headers to include in requests to the MCP server. - **requireApproval** _'always' | 'never' | object_ (optional) Controls which MCP tool calls require user approval before execution. Can be: - `'always'`: All MCP tool calls require approval - `'never'`: No MCP tool calls require approval (default) - An object with filters: ```ts { never: { toolNames: ['safe_tool', 'another_safe_tool']; // Skip approval for these tools } } ``` When approval is required, the model will return a `tool-approval-request` content part that you can use to prompt the user for approval. See [Human in the Loop](/cookbook/next/human-in-the-loop) for more details on implementing approval workflows. <Note> When `requireApproval` is not set, tool calls are approved by default. Be sure to connect to only trusted MCP servers, who you trust to share your data with. </Note> <Note> The OpenAI MCP tool is different from the general MCP client approach documented in [MCP Tools](/docs/ai-sdk-core/mcp-tools). The OpenAI MCP tool is a built-in provider-defined tool that allows OpenAI models to directly connect to MCP servers, while the general MCP client requires you to convert MCP tools to AI SDK tools first. </Note> #### Local Shell Tool The OpenAI responses API support the local shell tool for Codex models through the `openai.tools.localShell` tool. Local shell is a tool that allows agents to run shell commands locally on a machine you or the user provides. ```ts import { openai } from '@ai-sdk/openai'; import { generateText } from 'ai'; const result = await generateText({ model: openai.responses('gpt-5-codex'), tools: { local_shell: openai.tools.localShell({ execute: async ({ action }) => { // ... your implementation, e.g. sandbox access ... return { output: stdout }; }, }), }, prompt: 'List the files in my home directory.', stopWhen: isStepCount(2), }); ``` #### Shell Tool The OpenAI Responses API supports the shell tool through the `openai.tools.shell` tool. The shell tool allows running bash commands and interacting with a command line. The model proposes shell commands; your integration executes them and returns the outputs. <Note type="warning"> Running arbitrary shell commands can be dangerous. Always sandbox execution or add strict allow-/deny-lists before forwarding a command to the system shell. </Note> The shell tool supports three environment modes that control where commands are executed: ##### Local Execution (default) When no `environment` is specified (or `type: 'local'` is used), commands are executed locally via your `execute` callback: ```ts import { openai } from '@ai-sdk/openai'; import { generateText } from 'ai'; const result = await generateText({ model: openai('gpt-5.2'), tools: { shell: openai.tools.shell({ execute: async ({ action }) => { // ... your implementation, e.g. sandbox access ... return { output: results }; }, }), }, prompt: 'List the files in the current directory and show disk usage.', }); ``` ##### Hosted Container (auto) Set `environment.type` to `'containerAuto'` to run commands in an OpenAI-hosted container. No `execute` callback is needed — OpenAI handles execution server-side: ```ts const result = await generateText({ model: openai('gpt-5.2'), tools: { shell: openai.tools.shell({ environment: { type: 'containerAuto', // optional configuration: memoryLimit: '4g', fileIds: ['file-abc123'], networkPolicy: { type: 'allowlist', allowedDomains: ['example.com'], }, }, }), }, prompt: 'Install numpy and compute the eigenvalues of a 3x3 matrix.', }); ``` The `containerAuto` environment supports: - **fileIds** _string[]_ - File IDs to make available in the container - **memoryLimit** _'1g' | '4g' | '16g' | '64g'_ - Memory limit for the container - **networkPolicy** - Network access policy: - `{ type: 'disabled' }` — no network access - `{ type: 'allowlist', allowedDomains: string[], domainSecrets?: Array<{ domain, name, value }> }` — allow specific domains with optional secrets ##### Existing Container Reference Set `environment.type` to `'containerReference'` to use an existing container by ID: ```ts const result = await generateText({ model: openai('gpt-5.2'), tools: { shell: openai.tools.shell({ environment: { type: 'containerReference', containerId: 'cntr_abc123', }, }), }, prompt: 'Check the status of running processes.', }); ``` ##### Execute Callback For local execution (default or `type: 'local'`), your execute function must return an output array with results for each command: - **stdout** _string_ - Standard output from the command - **stderr** _string_ - Standard error from the command - **outcome** - Either `{ type: 'timeout' }` or `{ type: 'exit', exitCode: number }` ##### Skills [Skills](https://platform.openai.com/docs/guides/tools-skills) are versioned bundles of files with a `SKILL.md` manifest that extend the shell tool's capabilities. They can be attached to both `containerAuto` and `local` environments. **Container skills** support two formats — by reference (for skills uploaded to OpenAI) or inline (as a base64-encoded zip): ```ts const result = await generateText({ model: openai('gpt-5.2'), tools: { shell: openai.tools.shell({ environment: { type: 'containerAuto', skills: [ // By reference: { type: 'skillReference', skillId: 'skill_abc123' }, // Or inline: { type: 'inline', name: 'my-skill', description: 'What this skill does', source: { type: 'base64', mediaType: 'application/zip', data: readFileSync('./my-skill.zip').toString('base64'), }, }, ], }, }), }, prompt: 'Use the skill to solve this problem.', }); ``` **Local skills** point to a directory on disk containing a `SKILL.md` file: ```ts const result = await generateText({ model: openai('gpt-5.2'), tools: { shell: openai.tools.shell({ execute: async ({ action }) => { // ... your local execution implementation ... return { output: results }; }, environment: { type: 'local', skills: [ { name: 'my-skill', description: 'What this skill does', path: resolve('path/to/skill-directory'), }, ], }, }), }, prompt: 'Use the skill to solve this problem.', stopWhen: isStepCount(5), }); ``` For more details on creating skills, see the [OpenAI Skills documentation](https://platform.openai.com/docs/guides/tools-skills). #### Apply Patch Tool The OpenAI Responses API supports the apply patch tool for GPT-5.1 models through the `openai.tools.applyPatch` tool. The apply patch tool lets the model create, update, and delete files in your codebase using structured diffs. Instead of just suggesting edits, the model emits patch operations that your application applies and reports back on, enabling iterative, multi-step code editing workflows. ```ts import { openai } from '@ai-sdk/openai'; import { generateText, isStepCount } from 'ai'; const result = await generateText({ model: openai('gpt-5.1'), tools: { apply_patch: openai.tools.applyPatch({ execute: async ({ callId, operation }) => { // ... your implementation for applying the diffs. }, }), }, prompt: 'Create a python file that calculates the factorial of a number', stopWhen: isStepCount(5), }); ``` Your execute function must return: - **status** _'completed' | 'failed'_ - Whether the patch was applied successfully - **output** _string_ (optional) - Human-readable log text (e.g., results or error messages) #### Tool Search Tool search allows the model to dynamically search for and load tools into context as needed, rather than loading all tool definitions up front. This can reduce token usage, cost, and latency when you have many tools. Mark the tools you want to make searchable with `deferLoading: true` in their `providerOptions`. There are two execution modes: - **Server-executed (hosted):** OpenAI searches across the deferred tools declared in the request and returns the loaded subset in the same response. No extra round-trip is needed. - **Client-executed:** The model emits a `tool_search_call`, your application performs the lookup, and you return the matching tools via the `execute` callback. ##### Server-Executed (Hosted) Tool Search Use hosted tool search when the candidate tools are already known at request time. Add `openai.tools.toolSearch()` with no arguments and mark your tools with `deferLoading: true`: ```ts import { openai } from '@ai-sdk/openai'; import { generateText, tool, isStepCount } from 'ai'; import { z } from 'zod'; const result = await generateText({ model: openai.responses('gpt-5.4'), prompt: 'What is the weather in San Francisco?', stopWhen: isStepCount(10), tools: { toolSearch: openai.tools.toolSearch(), get_weather: tool({ description: 'Get the current weather at a specific location', inputSchema: z.object({ location: z.string(), unit: z.enum(['celsius', 'fahrenheit']), }), execute: async ({ location, unit }) => ({ location, temperature: unit === 'celsius' ? 18 : 64, }), providerOptions: { openai: { deferLoading: true }, }, }), search_files: tool({ description: 'Search through files in the workspace', inputSchema: z.object({ query: z.string() }), execute: async ({ query }) => ({ results: [`Found 3 files matching "${query}"`], }), providerOptions: { openai: { deferLoading: true }, }, }), }, }); ``` In hosted mode, the model internally searches the deferred tools, loads the relevant ones, and proceeds to call them — all within a single response. The `tool_search_call` and `tool_search_output` items appear in the response with `execution: 'server'` and `call_id: null`. ##### Namespaces Use `providerOptions.openai.namespace` to group related function tools for OpenAI. The SDK keeps each tool executable as a normal AI SDK tool, but serializes grouped tools as OpenAI `namespace` entries in the request: ```ts const crmNamespace = { name: 'crm', description: 'CRM tools for customer lookup and order management.', }; const result = await generateText({ model: openai.responses('gpt-5.4'), prompt: 'List open orders for customer cust_123.', tools: { toolSearch: openai.tools.toolSearch(), get_customer_profile: tool({ description: 'Fetch a customer profile by customer ID.', inputSchema: z.object({ customer_id: z.string() }), execute: async ({ customer_id }) => ({ customer_id }), providerOptions: { openai: { namespace: crmNamespace }, }, }), list_open_orders: tool({ description: 'List open orders for a customer ID.', inputSchema: z.object({ customer_id: z.string() }), execute: async ({ customer_id }) => ({ customer_id, orders: [] }), providerOptions: { openai: { namespace: crmNamespace, deferLoading: true, }, }, }), }, }); ``` Tools in the same namespace must use the same namespace `name` and `description`. For best results with tool search, keep namespace descriptions concise and put detailed usage guidance on the individual function tools. ##### Client-Executed Tool Search Use client-executed tool search when tool discovery depends on runtime state — for example, tools that vary per tenant, project, or external system. Pass `execution: 'client'` along with a `description`, `parameters` schema, and an `execute` callback: ```ts import { openai } from '@ai-sdk/openai'; import { generateText, tool, isStepCount } from 'ai'; import { z } from 'zod'; const result = await generateText({ model: openai.responses('gpt-5.4'), prompt: 'What is the weather in San Francisco?', stopWhen: isStepCount(10), tools: { toolSearch: openai.tools.toolSearch({ execution: 'client', description: 'Search for available tools based on what the user needs.', parameters: { type: 'object', properties: { goal: { type: 'string', description: 'What the user is trying to accomplish', }, }, required: ['goal'], additionalProperties: false, }, execute: async ({ arguments: args }) => { // Your custom tool discovery logic here. // Return the tools that match the search goal. return { tools: [ { type: 'function', name: 'get_weather', description: 'Get the current weather at a specific location', deferLoading: true, parameters: { type: 'object', properties: { location: { type: 'string' }, }, required: ['location'], additionalProperties: false, }, }, ], }; }, }), get_weather: tool({ description: 'Get the current weather at a specific location', inputSchema: z.object({ location: z.string() }), execute: async ({ location }) => ({ location, temperature: 64, condition: 'Partly cloudy', }), providerOptions: { openai: { deferLoading: true }, }, }), }, }); ``` In client mode, the flow spans two steps: 1. **Step 1:** The model emits a `tool_search_call` with `execution: 'client'` and a non-null `call_id`. The SDK calls your `execute` callback with the search arguments. Your callback returns the discovered tools. 2. **Step 2:** The SDK sends the `tool_search_output` (with the matching `call_id`) back to the model. The model can now call the loaded tools as normal function calls. For more details, see the [OpenAI Tool Search documentation](https://platform.openai.com/docs/guides/tools-tool-search). #### Custom Tool The OpenAI Responses API supports [custom tools](https://developers.openai.com/api/docs/guides/function-calling/#custom-tools) through the `openai.tools.customTool` tool. Custom tools return a raw string instead of JSON, optionally constrained to a grammar (regex or Lark syntax). This makes them useful for generating structured text like SQL queries, code snippets, or any output that must match a specific pattern. ```ts import { openai } from '@ai-sdk/openai'; import { generateText, isStepCount } from 'ai'; const result = await generateText({ model: openai.responses('gpt-5.2-codex'), tools: { write_sql: openai.tools.customTool({ description: 'Write a SQL SELECT query to answer the user question.', format: { type: 'grammar', syntax: 'regex', definition: 'SELECT .+', }, execute: async input => { // input is a raw string matching the grammar, e.g. "SELECT * FROM users WHERE age > 25" const rows = await db.query(input); return JSON.stringify(rows); }, }), }, toolChoice: 'required', prompt: 'Write a SQL query to get all users older than 25.', stopWhen: isStepCount(3), }); ``` Custom tools also work with `streamText`: ```ts import { openai } from '@ai-sdk/openai'; import { streamText } from 'ai'; const result = streamText({ model: openai.responses('gpt-5.2-codex'), tools: { write_sql: openai.tools.customTool({ description: 'Write a SQL SELECT query to answer the user question.', format: { type: 'grammar', syntax: 'regex', definition: 'SELECT .+', }, }), }, toolChoice: 'required', prompt: 'Write a SQL query to get all users older than 25.', }); for await (const chunk of result.stream) { if (chunk.type === 'tool-call') { console.log(`Tool: ${chunk.toolName}`); console.log(`Input: ${chunk.input}`); } } ``` The custom tool can be configured with: - **description** _string_ (optional) - A description of what the tool does, to help the model understand when to use it. - **format** _object_ (optional) - The output format constraint. Omit for unconstrained text output. - **type** _'grammar' | 'text'_ - The format type. Use `'grammar'` for constrained output or `'text'` for explicit unconstrained text. - **syntax** _'regex' | 'lark'_ - (grammar only) The grammar syntax. Use `'regex'` for regular expression patterns or `'lark'` for [Lark parser grammar](https://lark-parser.readthedocs.io/). - **definition** _string_ - (grammar only) The grammar definition string (a regex pattern or Lark grammar). - **execute** _function_ (optional) - An async function that receives the raw string input and returns a string result. Enables multi-turn tool calling. #### Image Inputs The OpenAI Responses API supports Image inputs for appropriate models. You can pass Image files as part of the message content using the 'image' type: ```ts const result = await generateText({ model: openai('gpt-5'), messages: [ { role: 'user', content: [ { type: 'text', text: 'Please describe the image.', }, { type: 'file', mediaType: 'image', data: readFileSync('./data/image.png'), }, ], }, ], }); ``` The model will have access to the image and will respond to questions about it. The image should be passed using the `image` field. You can also pass a file-id from the OpenAI Files API. ```ts { type: 'file', mediaType: 'image', data: 'file-8EFBcWHsQxZV7YGezBC1fq' } ``` You can also pass the URL of an image. ```ts { type: 'file', mediaType: 'image', data: 'https://sample.edu/image.png', } ``` #### PDF Inputs The OpenAI Responses API supports reading PDF files. You can pass PDF files as part of the message content using the `file` type: ```ts const result = await generateText({ model: openai('gpt-5'), messages: [ { role: 'user', content: [ { type: 'text', text: 'What is an embedding model?', }, { type: 'file', data: readFileSync('./data/ai.pdf'), mediaType: 'application/pdf', filename: 'ai.pdf', // optional }, ], }, ], }); ``` You can also pass a file-id from the OpenAI Files API. ```ts { type: 'file', data: 'file-8EFBcWHsQxZV7YGezBC1fq', mediaType: 'application/pdf', } ``` You can also pass the URL of a pdf. ```ts { type: 'file', data: 'https://sample.edu/example.pdf', mediaType: 'application/pdf', filename: 'ai.pdf', // optional } ``` The model will have access to the contents of the PDF file and respond to questions about it. The PDF file should be passed using the `data` field, and the `mediaType` should be set to `'application/pdf'`. #### Structured Outputs The OpenAI Responses API supports structured outputs. You can use `generateText` or `streamText` with [`Output`](/docs/reference/ai-sdk-core/output) to enforce structured outputs. ```ts const result = await generateText({ model: openai('gpt-4.1'), output: Output.object({ schema: z.object({ recipe: z.object({ name: z.string(), ingredients: z.array( z.object({ name: z.string(), amount: z.string(), }), ), steps: z.array(z.string()), }), }), }), prompt: 'Generate a lasagna recipe.', }); ``` #### Typed providerMetadata in Text Parts When using the OpenAI Responses API, the SDK attaches OpenAI-specific metadata to output parts via `providerMetadata`. This metadata can be used on the client side for tasks such as rendering citations or downloading files generated by the Code Interpreter. To enable type-safe handling of this metadata, the AI SDK exports dedicated TypeScript types. For text parts, when `part.type === 'text'`, the `providerMetadata` is provided in the form of `OpenaiResponsesTextProviderMetadata`. This metadata includes the following fields: - `itemId` The ID of the output item in the Responses API. - `annotations` (optional) An array of annotation objects generated by the model. If no annotations are present, this property itself may be omitted (`undefined`). Each element in `annotations` is a discriminated union with a required `type` field. Supported types include, for example: - `url_citation` - `file_citation` - `container_file_citation` - `file_path` These annotations directly correspond to the annotation objects defined by the Responses API and can be used for inline reference rendering or output analysis. For details, see the official OpenAI documentation: [Responses API – output text annotations](https://platform.openai.com/docs/api-reference/responses/object?lang=javascript#responses-object-output-output_message-content-output_text-annotations). ```ts import { openai, type OpenaiResponsesTextProviderMetadata, } from '@ai-sdk/openai'; import { generateText } from 'ai'; const result = await generateText({ model: openai('gpt-4.1-mini'), prompt: 'Create a program that generates five random numbers between 1 and 100 with two decimal places, and show me the execution results. Also save the result to a file.', tools: { code_interpreter: openai.tools.codeInterpreter(), web_search: openai.tools.webSearch(), file_search: openai.tools.fileSearch({ vectorStoreIds: ['vs_1234'] }), // requires a configured vector store }, }); for (const part of result.content) { if (part.type === 'text') { const providerMetadata = part.providerMetadata as | OpenaiResponsesTextProviderMetadata | undefined; if (!providerMetadata) continue; const { itemId: _itemId, annotations } = providerMetadata.openai; if (!annotations) continue; for (const annotation of annotations) { switch (annotation.type) { case 'url_citation': // url_citation is returned from web_search and provides: // properties: type, url, title, start_index and end_index break; case 'file_citation': // file_citation is returned from file_search and provides: // properties: type, file_id, filename and index break;