@ai-sdk/openai 0.0.0-70e0935a-20260114150030 → 0.0.0-98261322-20260122142521

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/CHANGELOG.md +43 -4
  2. package/dist/index.d.mts +1 -1
  3. package/dist/index.d.ts +1 -1
  4. package/dist/index.js +15 -1
  5. package/dist/index.js.map +1 -1
  6. package/dist/index.mjs +15 -1
  7. package/dist/index.mjs.map +1 -1
  8. package/dist/internal/index.d.mts +1 -1
  9. package/dist/internal/index.d.ts +1 -1
  10. package/dist/internal/index.js +14 -0
  11. package/dist/internal/index.js.map +1 -1
  12. package/dist/internal/index.mjs +14 -0
  13. package/dist/internal/index.mjs.map +1 -1
  14. package/docs/03-openai.mdx +2018 -0
  15. package/package.json +10 -5
  16. package/src/chat/__fixtures__/azure-model-router.1.chunks.txt +8 -0
  17. package/src/chat/__snapshots__/openai-chat-language-model.test.ts.snap +88 -0
  18. package/src/chat/convert-openai-chat-usage.ts +57 -0
  19. package/src/chat/convert-to-openai-chat-messages.test.ts +516 -0
  20. package/src/chat/convert-to-openai-chat-messages.ts +225 -0
  21. package/src/chat/get-response-metadata.ts +15 -0
  22. package/src/chat/map-openai-finish-reason.ts +19 -0
  23. package/src/chat/openai-chat-api.ts +198 -0
  24. package/src/chat/openai-chat-language-model.test.ts +3496 -0
  25. package/src/chat/openai-chat-language-model.ts +700 -0
  26. package/src/chat/openai-chat-options.ts +186 -0
  27. package/src/chat/openai-chat-prepare-tools.test.ts +322 -0
  28. package/src/chat/openai-chat-prepare-tools.ts +84 -0
  29. package/src/chat/openai-chat-prompt.ts +70 -0
  30. package/src/completion/convert-openai-completion-usage.ts +46 -0
  31. package/src/completion/convert-to-openai-completion-prompt.ts +93 -0
  32. package/src/completion/get-response-metadata.ts +15 -0
  33. package/src/completion/map-openai-finish-reason.ts +19 -0
  34. package/src/completion/openai-completion-api.ts +81 -0
  35. package/src/completion/openai-completion-language-model.test.ts +752 -0
  36. package/src/completion/openai-completion-language-model.ts +336 -0
  37. package/src/completion/openai-completion-options.ts +58 -0
  38. package/src/embedding/__snapshots__/openai-embedding-model.test.ts.snap +43 -0
  39. package/src/embedding/openai-embedding-api.ts +13 -0
  40. package/src/embedding/openai-embedding-model.test.ts +146 -0
  41. package/src/embedding/openai-embedding-model.ts +95 -0
  42. package/src/embedding/openai-embedding-options.ts +30 -0
  43. package/src/image/openai-image-api.ts +35 -0
  44. package/src/image/openai-image-model.test.ts +722 -0
  45. package/src/image/openai-image-model.ts +305 -0
  46. package/src/image/openai-image-options.ts +28 -0
  47. package/src/index.ts +9 -0
  48. package/src/internal/index.ts +19 -0
  49. package/src/openai-config.ts +18 -0
  50. package/src/openai-error.test.ts +34 -0
  51. package/src/openai-error.ts +22 -0
  52. package/src/openai-language-model-capabilities.test.ts +93 -0
  53. package/src/openai-language-model-capabilities.ts +54 -0
  54. package/src/openai-provider.test.ts +98 -0
  55. package/src/openai-provider.ts +270 -0
  56. package/src/openai-tools.ts +114 -0
  57. package/src/responses/__fixtures__/openai-apply-patch-tool-delete.1.chunks.txt +5 -0
  58. package/src/responses/__fixtures__/openai-apply-patch-tool.1.chunks.txt +38 -0
  59. package/src/responses/__fixtures__/openai-apply-patch-tool.1.json +69 -0
  60. package/src/responses/__fixtures__/openai-code-interpreter-tool.1.chunks.txt +393 -0
  61. package/src/responses/__fixtures__/openai-code-interpreter-tool.1.json +137 -0
  62. package/src/responses/__fixtures__/openai-error.1.chunks.txt +4 -0
  63. package/src/responses/__fixtures__/openai-error.1.json +8 -0
  64. package/src/responses/__fixtures__/openai-file-search-tool.1.chunks.txt +94 -0
  65. package/src/responses/__fixtures__/openai-file-search-tool.1.json +89 -0
  66. package/src/responses/__fixtures__/openai-file-search-tool.2.chunks.txt +93 -0
  67. package/src/responses/__fixtures__/openai-file-search-tool.2.json +112 -0
  68. package/src/responses/__fixtures__/openai-image-generation-tool.1.chunks.txt +16 -0
  69. package/src/responses/__fixtures__/openai-image-generation-tool.1.json +96 -0
  70. package/src/responses/__fixtures__/openai-local-shell-tool.1.chunks.txt +7 -0
  71. package/src/responses/__fixtures__/openai-local-shell-tool.1.json +70 -0
  72. package/src/responses/__fixtures__/openai-mcp-tool-approval.1.chunks.txt +11 -0
  73. package/src/responses/__fixtures__/openai-mcp-tool-approval.1.json +169 -0
  74. package/src/responses/__fixtures__/openai-mcp-tool-approval.2.chunks.txt +123 -0
  75. package/src/responses/__fixtures__/openai-mcp-tool-approval.2.json +176 -0
  76. package/src/responses/__fixtures__/openai-mcp-tool-approval.3.chunks.txt +11 -0
  77. package/src/responses/__fixtures__/openai-mcp-tool-approval.3.json +169 -0
  78. package/src/responses/__fixtures__/openai-mcp-tool-approval.4.chunks.txt +84 -0
  79. package/src/responses/__fixtures__/openai-mcp-tool-approval.4.json +182 -0
  80. package/src/responses/__fixtures__/openai-mcp-tool.1.chunks.txt +373 -0
  81. package/src/responses/__fixtures__/openai-mcp-tool.1.json +159 -0
  82. package/src/responses/__fixtures__/openai-reasoning-encrypted-content.1.chunks.txt +110 -0
  83. package/src/responses/__fixtures__/openai-reasoning-encrypted-content.1.json +117 -0
  84. package/src/responses/__fixtures__/openai-shell-tool.1.chunks.txt +182 -0
  85. package/src/responses/__fixtures__/openai-shell-tool.1.json +73 -0
  86. package/src/responses/__fixtures__/openai-web-search-tool.1.chunks.txt +185 -0
  87. package/src/responses/__fixtures__/openai-web-search-tool.1.json +266 -0
  88. package/src/responses/__snapshots__/openai-responses-language-model.test.ts.snap +10955 -0
  89. package/src/responses/convert-openai-responses-usage.ts +53 -0
  90. package/src/responses/convert-to-openai-responses-input.test.ts +2976 -0
  91. package/src/responses/convert-to-openai-responses-input.ts +578 -0
  92. package/src/responses/map-openai-responses-finish-reason.ts +22 -0
  93. package/src/responses/openai-responses-api.test.ts +89 -0
  94. package/src/responses/openai-responses-api.ts +1086 -0
  95. package/src/responses/openai-responses-language-model.test.ts +6927 -0
  96. package/src/responses/openai-responses-language-model.ts +1932 -0
  97. package/src/responses/openai-responses-options.ts +312 -0
  98. package/src/responses/openai-responses-prepare-tools.test.ts +924 -0
  99. package/src/responses/openai-responses-prepare-tools.ts +264 -0
  100. package/src/responses/openai-responses-provider-metadata.ts +39 -0
  101. package/src/speech/openai-speech-api.ts +38 -0
  102. package/src/speech/openai-speech-model.test.ts +202 -0
  103. package/src/speech/openai-speech-model.ts +137 -0
  104. package/src/speech/openai-speech-options.ts +22 -0
  105. package/src/tool/apply-patch.ts +141 -0
  106. package/src/tool/code-interpreter.ts +104 -0
  107. package/src/tool/file-search.ts +145 -0
  108. package/src/tool/image-generation.ts +126 -0
  109. package/src/tool/local-shell.test-d.ts +20 -0
  110. package/src/tool/local-shell.ts +72 -0
  111. package/src/tool/mcp.ts +125 -0
  112. package/src/tool/shell.ts +85 -0
  113. package/src/tool/web-search-preview.ts +139 -0
  114. package/src/tool/web-search.test-d.ts +13 -0
  115. package/src/tool/web-search.ts +179 -0
  116. package/src/transcription/openai-transcription-api.ts +37 -0
  117. package/src/transcription/openai-transcription-model.test.ts +507 -0
  118. package/src/transcription/openai-transcription-model.ts +232 -0
  119. package/src/transcription/openai-transcription-options.ts +50 -0
  120. package/src/transcription/transcription-test.mp3 +0 -0
  121. package/src/version.ts +6 -0
@@ -0,0 +1,2018 @@
1
+ ---
2
+ title: OpenAI
3
+ description: Learn how to use the OpenAI provider for the AI SDK.
4
+ ---
5
+
6
+ # OpenAI Provider
7
+
8
+ 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.
9
+
10
+ ## Setup
11
+
12
+ The OpenAI provider is available in the `@ai-sdk/openai` module. You can install it with
13
+
14
+ <Tabs items={['pnpm', 'npm', 'yarn', 'bun']}>
15
+ <Tab>
16
+ <Snippet text="pnpm add @ai-sdk/openai" dark />
17
+ </Tab>
18
+ <Tab>
19
+ <Snippet text="npm install @ai-sdk/openai" dark />
20
+ </Tab>
21
+ <Tab>
22
+ <Snippet text="yarn add @ai-sdk/openai" dark />
23
+ </Tab>
24
+
25
+ <Tab>
26
+ <Snippet text="bun add @ai-sdk/openai" dark />
27
+ </Tab>
28
+ </Tabs>
29
+
30
+ ## Provider Instance
31
+
32
+ You can import the default provider instance `openai` from `@ai-sdk/openai`:
33
+
34
+ ```ts
35
+ import { openai } from '@ai-sdk/openai';
36
+ ```
37
+
38
+ If you need a customized setup, you can import `createOpenAI` from `@ai-sdk/openai` and create a provider instance with your settings:
39
+
40
+ ```ts
41
+ import { createOpenAI } from '@ai-sdk/openai';
42
+
43
+ const openai = createOpenAI({
44
+ // custom settings, e.g.
45
+ headers: {
46
+ 'header-name': 'header-value',
47
+ },
48
+ });
49
+ ```
50
+
51
+ You can use the following optional settings to customize the OpenAI provider instance:
52
+
53
+ - **baseURL** _string_
54
+
55
+ Use a different URL prefix for API calls, e.g. to use proxy servers.
56
+ The default prefix is `https://api.openai.com/v1`.
57
+
58
+ - **apiKey** _string_
59
+
60
+ API key that is being sent using the `Authorization` header.
61
+ It defaults to the `OPENAI_API_KEY` environment variable.
62
+
63
+ - **name** _string_
64
+
65
+ The provider name. You can set this when using OpenAI compatible providers
66
+ to change the model provider property. Defaults to `openai`.
67
+
68
+ - **organization** _string_
69
+
70
+ OpenAI Organization.
71
+
72
+ - **project** _string_
73
+
74
+ OpenAI project.
75
+
76
+ - **headers** _Record&lt;string,string&gt;_
77
+
78
+ Custom headers to include in the requests.
79
+
80
+ - **fetch** _(input: RequestInfo, init?: RequestInit) => Promise&lt;Response&gt;_
81
+
82
+ Custom [fetch](https://developer.mozilla.org/en-US/docs/Web/API/fetch) implementation.
83
+ Defaults to the global `fetch` function.
84
+ You can use it as a middleware to intercept requests,
85
+ or to provide a custom fetch implementation for e.g. testing.
86
+
87
+ ## Language Models
88
+
89
+ The OpenAI provider instance is a function that you can invoke to create a language model:
90
+
91
+ ```ts
92
+ const model = openai('gpt-5');
93
+ ```
94
+
95
+ It automatically selects the correct API based on the model id.
96
+ You can also pass additional settings in the second argument:
97
+
98
+ ```ts
99
+ const model = openai('gpt-5', {
100
+ // additional settings
101
+ });
102
+ ```
103
+
104
+ The available options depend on the API that's automatically chosen for the model (see below).
105
+ If you want to explicitly select a specific model API, you can use `.responses`, `.chat`, or `.completion`.
106
+
107
+ <Note>
108
+ Since AI SDK 5, the OpenAI responses API is called by default (unless you
109
+ specify e.g. 'openai.chat')
110
+ </Note>
111
+
112
+ ### Example
113
+
114
+ You can use OpenAI language models to generate text with the `generateText` function:
115
+
116
+ ```ts
117
+ import { openai } from '@ai-sdk/openai';
118
+ import { generateText } from 'ai';
119
+
120
+ const { text } = await generateText({
121
+ model: openai('gpt-5'),
122
+ prompt: 'Write a vegetarian lasagna recipe for 4 people.',
123
+ });
124
+ ```
125
+
126
+ OpenAI language models can also be used in the `streamText`, `generateObject`, and `streamObject` functions
127
+ (see [AI SDK Core](/docs/ai-sdk-core)).
128
+
129
+ ### Responses Models
130
+
131
+ 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).
132
+
133
+ ```ts
134
+ const model = openai('gpt-5');
135
+ ```
136
+
137
+ Further configuration can be done using OpenAI provider options.
138
+ You can validate the provider options using the `OpenAIResponsesProviderOptions` type.
139
+
140
+ ```ts
141
+ import { openai, OpenAIResponsesProviderOptions } from '@ai-sdk/openai';
142
+ import { generateText } from 'ai';
143
+
144
+ const result = await generateText({
145
+ model: openai('gpt-5'), // or openai.responses('gpt-5')
146
+ providerOptions: {
147
+ openai: {
148
+ parallelToolCalls: false,
149
+ store: false,
150
+ user: 'user_123',
151
+ // ...
152
+ } satisfies OpenAIResponsesProviderOptions,
153
+ },
154
+ // ...
155
+ });
156
+ ```
157
+
158
+ The following provider options are available:
159
+
160
+ - **parallelToolCalls** _boolean_
161
+ Whether to use parallel tool calls. Defaults to `true`.
162
+
163
+ - **store** _boolean_
164
+
165
+ Whether to store the generation. Defaults to `true`.
166
+
167
+ - **maxToolCalls** _integer_
168
+ The maximum number of total calls to built-in tools that can be processed in a response.
169
+ This maximum number applies across all built-in tool calls, not per individual tool.
170
+ Any further attempts to call a tool by the model will be ignored.
171
+
172
+ - **metadata** _Record&lt;string, string&gt;_
173
+ Additional metadata to store with the generation.
174
+
175
+ - **conversation** _string_
176
+ The ID of the OpenAI Conversation to continue.
177
+ You must create a conversation first via the [OpenAI API](https://platform.openai.com/docs/api-reference/conversations/create).
178
+ Cannot be used in conjunction with `previousResponseId`.
179
+ Defaults to `undefined`.
180
+
181
+ - **previousResponseId** _string_
182
+ The ID of the previous response. You can use it to continue a conversation. Defaults to `undefined`.
183
+
184
+ - **instructions** _string_
185
+ Instructions for the model.
186
+ They can be used to change the system or developer message when continuing a conversation using the `previousResponseId` option.
187
+ Defaults to `undefined`.
188
+
189
+ - **user** _string_
190
+ A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. Defaults to `undefined`.
191
+
192
+ - **reasoningEffort** _'none' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh'_
193
+ Reasoning effort for reasoning models. Defaults to `medium`. If you use `providerOptions` to set the `reasoningEffort` option, this model setting will be ignored.
194
+
195
+ <Note>
196
+ The 'none' type for `reasoningEffort` is only available for OpenAI's GPT-5.1
197
+ models. Also, the 'xhigh' type for `reasoningEffort` is only available for
198
+ OpenAI's GPT-5.1-Codex-Max model. Setting `reasoningEffort` to 'none' or
199
+ 'xhigh' with unsupported models will result in an error.
200
+ </Note>
201
+
202
+ - **reasoningSummary** _'auto' | 'detailed'_
203
+ 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.
204
+
205
+ - **strictJsonSchema** _boolean_
206
+ Whether to use strict JSON schema validation. Defaults to `true`.
207
+
208
+ <Note type="warning">
209
+ OpenAI structured outputs have several
210
+ [limitations](https://openai.com/index/introducing-structured-outputs-in-the-api),
211
+ in particular around the [supported
212
+ schemas](https://platform.openai.com/docs/guides/structured-outputs/supported-schemas),
213
+ and are therefore opt-in. For example, optional schema properties are not
214
+ supported. You need to change Zod `.nullish()` and `.optional()` to
215
+ `.nullable()`.
216
+ </Note>
217
+
218
+ - **serviceTier** _'auto' | 'flex' | 'priority' | 'default'_
219
+ Service tier for the request. Set to 'flex' for 50% cheaper processing
220
+ at the cost of increased latency (available for o3, o4-mini, and gpt-5 models).
221
+ 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).
222
+
223
+ Defaults to 'auto'.
224
+
225
+ - **textVerbosity** _'low' | 'medium' | 'high'_
226
+ Controls the verbosity of the model's response. Lower values result in more concise responses,
227
+ while higher values result in more verbose responses. Defaults to `'medium'`.
228
+
229
+ - **include** _Array&lt;string&gt;_
230
+ Specifies additional content to include in the response. Supported values:
231
+ `['file_search_call.results']` for including file search results in responses.
232
+ `['message.output_text.logprobs']` for logprobs.
233
+ Defaults to `undefined`.
234
+
235
+ - **truncation** _string_
236
+ The truncation strategy to use for the model response.
237
+
238
+ - 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.
239
+ - disabled (default): If the input size will exceed the context window size for a model, the request will fail with a 400 error.
240
+
241
+ - **promptCacheKey** _string_
242
+ A cache key for manual prompt caching control. Used by OpenAI to cache responses for similar requests to optimize your cache hit rates.
243
+
244
+ - **promptCacheRetention** _'in_memory' | '24h'_
245
+ The retention policy for the prompt cache. Set to `'24h'` to enable extended prompt caching, which keeps cached prefixes active for up to 24 hours. Defaults to `'in_memory'` for standard prompt caching. Note: `'24h'` is currently only available for the 5.1 series of models.
246
+
247
+ - **safetyIdentifier** _string_
248
+ 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.
249
+
250
+ - **systemMessageMode** _'system' | 'developer' | 'remove'_
251
+ 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.
252
+
253
+ - **forceReasoning** _boolean_
254
+ 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.
255
+
256
+ The OpenAI responses provider also returns provider-specific metadata:
257
+
258
+ ```ts
259
+ const { providerMetadata } = await generateText({
260
+ model: openai.responses('gpt-5'),
261
+ });
262
+
263
+ const openaiMetadata = providerMetadata?.openai;
264
+ ```
265
+
266
+ The following OpenAI-specific metadata is returned:
267
+
268
+ - **responseId** _string_
269
+ The ID of the response. Can be used to continue a conversation.
270
+
271
+ - **cachedPromptTokens** _number_
272
+ The number of prompt tokens that were a cache hit.
273
+
274
+ - **reasoningTokens** _number_
275
+ The number of reasoning tokens that the model generated.
276
+
277
+ #### Reasoning Output
278
+
279
+ 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.
280
+
281
+ ```ts highlight="8-9,16"
282
+ import { openai } from '@ai-sdk/openai';
283
+ import { streamText } from 'ai';
284
+
285
+ const result = streamText({
286
+ model: openai('gpt-5'),
287
+ prompt: 'Tell me about the Mission burrito debate in San Francisco.',
288
+ providerOptions: {
289
+ openai: {
290
+ reasoningSummary: 'detailed', // 'auto' for condensed or 'detailed' for comprehensive
291
+ },
292
+ },
293
+ });
294
+
295
+ for await (const part of result.fullStream) {
296
+ if (part.type === 'reasoning') {
297
+ console.log(`Reasoning: ${part.textDelta}`);
298
+ } else if (part.type === 'text-delta') {
299
+ process.stdout.write(part.textDelta);
300
+ }
301
+ }
302
+ ```
303
+
304
+ For non-streaming calls with `generateText`, the reasoning summaries are available in the `reasoning` field of the response:
305
+
306
+ ```ts highlight="8-9,13"
307
+ import { openai } from '@ai-sdk/openai';
308
+ import { generateText } from 'ai';
309
+
310
+ const result = await generateText({
311
+ model: openai('gpt-5'),
312
+ prompt: 'Tell me about the Mission burrito debate in San Francisco.',
313
+ providerOptions: {
314
+ openai: {
315
+ reasoningSummary: 'auto',
316
+ },
317
+ },
318
+ });
319
+ console.log('Reasoning:', result.reasoning);
320
+ ```
321
+
322
+ Learn more about reasoning summaries in the [OpenAI documentation](https://platform.openai.com/docs/guides/reasoning?api-mode=responses#reasoning-summaries).
323
+
324
+ #### Verbosity Control
325
+
326
+ You can control the length and detail of model responses using the `textVerbosity` parameter:
327
+
328
+ ```ts
329
+ import { openai } from '@ai-sdk/openai';
330
+ import { generateText } from 'ai';
331
+
332
+ const result = await generateText({
333
+ model: openai('gpt-5-mini'),
334
+ prompt: 'Write a poem about a boy and his first pet dog.',
335
+ providerOptions: {
336
+ openai: {
337
+ textVerbosity: 'low', // 'low' for concise, 'medium' (default), or 'high' for verbose
338
+ },
339
+ },
340
+ });
341
+ ```
342
+
343
+ The `textVerbosity` parameter scales output length without changing the underlying prompt:
344
+
345
+ - `'low'`: Produces terse, minimal responses
346
+ - `'medium'`: Balanced detail (default)
347
+ - `'high'`: Verbose responses with comprehensive detail
348
+
349
+ #### Web Search Tool
350
+
351
+ The OpenAI responses API supports web search through the `openai.tools.webSearch` tool.
352
+
353
+ ```ts
354
+ const result = await generateText({
355
+ model: openai('gpt-5'),
356
+ prompt: 'What happened in San Francisco last week?',
357
+ tools: {
358
+ web_search: openai.tools.webSearch({
359
+ // optional configuration:
360
+ externalWebAccess: true,
361
+ searchContextSize: 'high',
362
+ userLocation: {
363
+ type: 'approximate',
364
+ city: 'San Francisco',
365
+ region: 'California',
366
+ },
367
+ }),
368
+ },
369
+ // Force web search tool (optional):
370
+ toolChoice: { type: 'tool', toolName: 'web_search' },
371
+ });
372
+
373
+ // URL sources directly from `results`
374
+ const sources = result.sources;
375
+
376
+ // Or access sources from tool results
377
+ for (const toolResult of result.toolResults) {
378
+ if (toolResult.toolName === 'web_search') {
379
+ console.log('Query:', toolResult.output.action.query);
380
+ console.log('Sources:', toolResult.output.sources);
381
+ // `sources` is an array of object: { type: 'url', url: string }
382
+ }
383
+ }
384
+ ```
385
+
386
+ 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).
387
+
388
+ #### File Search Tool
389
+
390
+ The OpenAI responses API supports file search through the `openai.tools.fileSearch` tool.
391
+
392
+ You can force the use of the file search tool by setting the `toolChoice` parameter to `{ type: 'tool', toolName: 'file_search' }`.
393
+
394
+ ```ts
395
+ const result = await generateText({
396
+ model: openai('gpt-5'),
397
+ prompt: 'What does the document say about user authentication?',
398
+ tools: {
399
+ file_search: openai.tools.fileSearch({
400
+ vectorStoreIds: ['vs_123'],
401
+ // configuration below is optional:
402
+ maxNumResults: 5,
403
+ filters: {
404
+ key: 'author',
405
+ type: 'eq',
406
+ value: 'Jane Smith',
407
+ },
408
+ ranking: {
409
+ ranker: 'auto',
410
+ scoreThreshold: 0.5,
411
+ },
412
+ }),
413
+ },
414
+ providerOptions: {
415
+ openai: {
416
+ // optional: include results
417
+ include: ['file_search_call.results'],
418
+ } satisfies OpenAIResponsesProviderOptions,
419
+ },
420
+ });
421
+ ```
422
+
423
+ #### Image Generation Tool
424
+
425
+ OpenAI's Responses API supports multi-modal image generation as a provider-defined tool.
426
+ Availability is restricted to specific models (for example, `gpt-5` variants).
427
+
428
+ You can use the image tool with either `generateText` or `streamText`:
429
+
430
+ ```ts
431
+ import { openai } from '@ai-sdk/openai';
432
+ import { generateText } from 'ai';
433
+
434
+ const result = await generateText({
435
+ model: openai('gpt-5'),
436
+ prompt:
437
+ 'Generate an image of an echidna swimming across the Mozambique channel.',
438
+ tools: {
439
+ image_generation: openai.tools.imageGeneration({ outputFormat: 'webp' }),
440
+ },
441
+ });
442
+
443
+ for (const toolResult of result.staticToolResults) {
444
+ if (toolResult.toolName === 'image_generation') {
445
+ const base64Image = toolResult.output.result;
446
+ }
447
+ }
448
+ ```
449
+
450
+ ```ts
451
+ import { openai } from '@ai-sdk/openai';
452
+ import { streamText } from 'ai';
453
+
454
+ const result = streamText({
455
+ model: openai('gpt-5'),
456
+ prompt:
457
+ 'Generate an image of an echidna swimming across the Mozambique channel.',
458
+ tools: {
459
+ image_generation: openai.tools.imageGeneration({
460
+ outputFormat: 'webp',
461
+ quality: 'low',
462
+ }),
463
+ },
464
+ });
465
+
466
+ for await (const part of result.fullStream) {
467
+ if (part.type == 'tool-result' && !part.dynamic) {
468
+ const base64Image = part.output.result;
469
+ }
470
+ }
471
+ ```
472
+
473
+ <Note>
474
+ When you set `store: false`, then previously generated images will not be
475
+ accessible by the model. We recommend using the image generation tool without
476
+ setting `store: false`.
477
+ </Note>
478
+
479
+ For complete details on model availability, image quality controls, supported sizes, and tool-specific parameters,
480
+ refer to the OpenAI documentation:
481
+
482
+ - Image generation overview and models: [OpenAI Image Generation](https://platform.openai.com/docs/guides/image-generation)
483
+ - Image generation tool parameters (background, size, quality, format, etc.): [Image Generation Tool Options](https://platform.openai.com/docs/guides/tools-image-generation#tool-options)
484
+
485
+ #### Code Interpreter Tool
486
+
487
+ The OpenAI responses API supports the code interpreter tool through the `openai.tools.codeInterpreter` tool.
488
+ This allows models to write and execute Python code.
489
+
490
+ ```ts
491
+ import { openai } from '@ai-sdk/openai';
492
+ import { generateText } from 'ai';
493
+
494
+ const result = await generateText({
495
+ model: openai('gpt-5'),
496
+ prompt: 'Write and run Python code to calculate the factorial of 10',
497
+ tools: {
498
+ code_interpreter: openai.tools.codeInterpreter({
499
+ // optional configuration:
500
+ container: {
501
+ fileIds: ['file-123', 'file-456'], // optional file IDs to make available
502
+ },
503
+ }),
504
+ },
505
+ });
506
+ ```
507
+
508
+ The code interpreter tool can be configured with:
509
+
510
+ - **container**: Either a container ID string or an object with `fileIds` to specify uploaded files that should be available to the code interpreter
511
+
512
+ <Note>
513
+ When working with files generated by the Code Interpreter, reference
514
+ information can be obtained from both [annotations in Text
515
+ Parts](#typed-providermetadata-in-text-parts) and [`providerMetadata` in
516
+ Source Document Parts](#typed-providermetadata-in-source-document-parts).
517
+ </Note>
518
+
519
+ #### MCP Tool
520
+
521
+ 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.
522
+
523
+ ```ts
524
+ import { openai } from '@ai-sdk/openai';
525
+ import { generateText } from 'ai';
526
+
527
+ const result = await generateText({
528
+ model: openai('gpt-5'),
529
+ prompt: 'Search the web for the latest news about AI developments',
530
+ tools: {
531
+ mcp: openai.tools.mcp({
532
+ serverLabel: 'web-search',
533
+ serverUrl: 'https://mcp.exa.ai/mcp',
534
+ serverDescription: 'A web-search API for AI agents',
535
+ }),
536
+ },
537
+ });
538
+ ```
539
+
540
+ The MCP tool can be configured with:
541
+
542
+ - **serverLabel** _string_ (required)
543
+
544
+ A label to identify the MCP server. This label is used in tool calls to distinguish between multiple MCP servers.
545
+
546
+ - **serverUrl** _string_ (required if `connectorId` is not provided)
547
+
548
+ The URL for the MCP server. Either `serverUrl` or `connectorId` must be provided.
549
+
550
+ - **connectorId** _string_ (required if `serverUrl` is not provided)
551
+
552
+ Identifier for a service connector. Either `serverUrl` or `connectorId` must be provided.
553
+
554
+ - **serverDescription** _string_ (optional)
555
+
556
+ Optional description of the MCP server that helps the model understand its purpose.
557
+
558
+ - **allowedTools** _string[] | object_ (optional)
559
+
560
+ Controls which tools from the MCP server are available. Can be:
561
+
562
+ - An array of tool names: `['tool1', 'tool2']`
563
+ - An object with filters:
564
+ ```ts
565
+ {
566
+ readOnly: true, // Only allow read-only tools
567
+ toolNames: ['tool1', 'tool2'] // Specific tool names
568
+ }
569
+ ```
570
+
571
+ - **authorization** _string_ (optional)
572
+
573
+ OAuth access token for authenticating with the MCP server or connector.
574
+
575
+ - **headers** _Record&lt;string, string&gt;_ (optional)
576
+
577
+ Optional HTTP headers to include in requests to the MCP server.
578
+
579
+ - **requireApproval** _'always' | 'never' | object_ (optional)
580
+
581
+ Controls which MCP tool calls require user approval before execution. Can be:
582
+
583
+ - `'always'`: All MCP tool calls require approval
584
+ - `'never'`: No MCP tool calls require approval (default)
585
+ - An object with filters:
586
+ ```ts
587
+ {
588
+ never: {
589
+ toolNames: ['safe_tool', 'another_safe_tool']; // Skip approval for these tools
590
+ }
591
+ }
592
+ ```
593
+
594
+ 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.
595
+
596
+ <Note>
597
+ When `requireApproval` is not set, tool calls are approved by default. Be sure
598
+ to connect to only trusted MCP servers, who you trust to share your data with.
599
+ </Note>
600
+
601
+ <Note>
602
+ The OpenAI MCP tool is different from the general MCP client approach
603
+ documented in [MCP Tools](/docs/ai-sdk-core/mcp-tools). The OpenAI MCP tool is
604
+ a built-in provider-defined tool that allows OpenAI models to directly connect
605
+ to MCP servers, while the general MCP client requires you to convert MCP tools
606
+ to AI SDK tools first.
607
+ </Note>
608
+
609
+ #### Local Shell Tool
610
+
611
+ The OpenAI responses API support the local shell tool for Codex models through the `openai.tools.localShell` tool.
612
+ Local shell is a tool that allows agents to run shell commands locally on a machine you or the user provides.
613
+
614
+ ```ts
615
+ import { openai } from '@ai-sdk/openai';
616
+ import { generateText } from 'ai';
617
+
618
+ const result = await generateText({
619
+ model: openai.responses('gpt-5-codex'),
620
+ tools: {
621
+ local_shell: openai.tools.localShell({
622
+ execute: async ({ action }) => {
623
+ // ... your implementation, e.g. sandbox access ...
624
+ return { output: stdout };
625
+ },
626
+ }),
627
+ },
628
+ prompt: 'List the files in my home directory.',
629
+ stopWhen: stepCountIs(2),
630
+ });
631
+ ```
632
+
633
+ #### Shell Tool
634
+
635
+ The OpenAI Responses API supports the shell tool for GPT-5.1 models through the `openai.tools.shell` tool.
636
+ The shell tool allows allows running bash commands and interacting with a command line.
637
+ The model proposes shell commands; your integration executes them and returns the outputs.
638
+
639
+ <Note type="warning">
640
+ Running arbitrary shell commands can be dangerous. Always sandbox execution or
641
+ add strict allow-/deny-lists before forwarding a command to the system shell.
642
+ </Note>
643
+
644
+ ```ts
645
+ import { openai } from '@ai-sdk/openai';
646
+ import { generateText } from 'ai';
647
+ import { exec } from 'child_process';
648
+ import { promisify } from 'util';
649
+
650
+ const execAsync = promisify(exec);
651
+
652
+ const result = await generateText({
653
+ model: openai('gpt-5.1'),
654
+ tools: {
655
+ shell: openai.tools.shell({
656
+ execute: async ({ action }) => {
657
+ // ... your implementation, e.g. sandbox access ...
658
+ return { output: results };
659
+ },
660
+ }),
661
+ },
662
+ prompt: 'List the files in the current directory and show disk usage.',
663
+ });
664
+ ```
665
+
666
+ Your execute function must return an output array with results for each command:
667
+
668
+ - **stdout** _string_ - Standard output from the command
669
+ - **stderr** _string_ - Standard error from the command
670
+ - **outcome** - Either `{ type: 'timeout' }` or `{ type: 'exit', exitCode: number }`
671
+
672
+ #### Apply Patch Tool
673
+
674
+ The OpenAI Responses API supports the apply patch tool for GPT-5.1 models through the `openai.tools.applyPatch` tool.
675
+ The apply patch tool lets the model create, update, and delete files in your codebase using structured diffs.
676
+ Instead of just suggesting edits, the model emits patch operations that your application applies and reports back on,
677
+ enabling iterative, multi-step code editing workflows.
678
+
679
+ ```ts
680
+ import { openai } from '@ai-sdk/openai';
681
+ import { generateText, stepCountIs } from 'ai';
682
+
683
+ const result = await generateText({
684
+ model: openai('gpt-5.1'),
685
+ tools: {
686
+ apply_patch: openai.tools.applyPatch({
687
+ execute: async ({ callId, operation }) => {
688
+ // ... your implementation for applying the diffs.
689
+ },
690
+ }),
691
+ },
692
+ prompt: 'Create a python file that calculates the factorial of a number',
693
+ stopWhen: stepCountIs(5),
694
+ });
695
+ ```
696
+
697
+ Your execute function must return:
698
+
699
+ - **status** _'completed' | 'failed'_ - Whether the patch was applied successfully
700
+ - **output** _string_ (optional) - Human-readable log text (e.g., results or error messages)
701
+
702
+ #### Image Inputs
703
+
704
+ The OpenAI Responses API supports Image inputs for appropriate models.
705
+ You can pass Image files as part of the message content using the 'image' type:
706
+
707
+ ```ts
708
+ const result = await generateText({
709
+ model: openai('gpt-5'),
710
+ messages: [
711
+ {
712
+ role: 'user',
713
+ content: [
714
+ {
715
+ type: 'text',
716
+ text: 'Please describe the image.',
717
+ },
718
+ {
719
+ type: 'image',
720
+ image: readFileSync('./data/image.png'),
721
+ },
722
+ ],
723
+ },
724
+ ],
725
+ });
726
+ ```
727
+
728
+ The model will have access to the image and will respond to questions about it.
729
+ The image should be passed using the `image` field.
730
+
731
+ You can also pass a file-id from the OpenAI Files API.
732
+
733
+ ```ts
734
+ {
735
+ type: 'image',
736
+ image: 'file-8EFBcWHsQxZV7YGezBC1fq'
737
+ }
738
+ ```
739
+
740
+ You can also pass the URL of an image.
741
+
742
+ ```ts
743
+ {
744
+ type: 'image',
745
+ image: 'https://sample.edu/image.png',
746
+ }
747
+ ```
748
+
749
+ #### PDF Inputs
750
+
751
+ The OpenAI Responses API supports reading PDF files.
752
+ You can pass PDF files as part of the message content using the `file` type:
753
+
754
+ ```ts
755
+ const result = await generateText({
756
+ model: openai('gpt-5'),
757
+ messages: [
758
+ {
759
+ role: 'user',
760
+ content: [
761
+ {
762
+ type: 'text',
763
+ text: 'What is an embedding model?',
764
+ },
765
+ {
766
+ type: 'file',
767
+ data: readFileSync('./data/ai.pdf'),
768
+ mediaType: 'application/pdf',
769
+ filename: 'ai.pdf', // optional
770
+ },
771
+ ],
772
+ },
773
+ ],
774
+ });
775
+ ```
776
+
777
+ You can also pass a file-id from the OpenAI Files API.
778
+
779
+ ```ts
780
+ {
781
+ type: 'file',
782
+ data: 'file-8EFBcWHsQxZV7YGezBC1fq',
783
+ mediaType: 'application/pdf',
784
+ }
785
+ ```
786
+
787
+ You can also pass the URL of a pdf.
788
+
789
+ ```ts
790
+ {
791
+ type: 'file',
792
+ data: 'https://sample.edu/example.pdf',
793
+ mediaType: 'application/pdf',
794
+ filename: 'ai.pdf', // optional
795
+ }
796
+ ```
797
+
798
+ The model will have access to the contents of the PDF file and
799
+ respond to questions about it.
800
+ The PDF file should be passed using the `data` field,
801
+ and the `mediaType` should be set to `'application/pdf'`.
802
+
803
+ #### Structured Outputs
804
+
805
+ The OpenAI Responses API supports structured outputs. You can enforce structured outputs using `generateObject` or `streamObject`, which expose a `schema` option. Additionally, you can pass a Zod or JSON Schema object to the `output` option when using `generateText` or `streamText`.
806
+
807
+ ```ts
808
+ // Using generateObject
809
+ const result = await generateObject({
810
+ model: openai('gpt-4.1'),
811
+ schema: z.object({
812
+ recipe: z.object({
813
+ name: z.string(),
814
+ ingredients: z.array(
815
+ z.object({
816
+ name: z.string(),
817
+ amount: z.string(),
818
+ }),
819
+ ),
820
+ steps: z.array(z.string()),
821
+ }),
822
+ }),
823
+ prompt: 'Generate a lasagna recipe.',
824
+ });
825
+
826
+ // Using generateText
827
+ const result = await generateText({
828
+ model: openai('gpt-4.1'),
829
+ prompt: 'How do I make a pizza?',
830
+ output: Output.object({
831
+ schema: z.object({
832
+ ingredients: z.array(z.string()),
833
+ steps: z.array(z.string()),
834
+ }),
835
+ }),
836
+ });
837
+ ```
838
+
839
+ #### Typed providerMetadata in Text Parts
840
+
841
+ When using the OpenAI Responses API, the SDK attaches OpenAI-specific metadata to output parts via `providerMetadata`.
842
+
843
+ This metadata can be used on the client side for tasks such as rendering citations or downloading files generated by the Code Interpreter.
844
+ To enable type-safe handling of this metadata, the AI SDK exports dedicated TypeScript types.
845
+
846
+ For text parts, when `part.type === 'text'`, the `providerMetadata` is provided in the form of `OpenaiResponsesTextProviderMetadata`.
847
+
848
+ This metadata includes the following fields:
849
+
850
+ - `itemId`
851
+ The ID of the output item in the Responses API.
852
+ - `annotations` (optional)
853
+ An array of annotation objects generated by the model.
854
+ If no annotations are present, this property itself may be omitted (`undefined`).
855
+
856
+ Each element in `annotations` is a discriminated union with a required `type` field. Supported types include, for example:
857
+
858
+ - `url_citation`
859
+ - `file_citation`
860
+ - `container_file_citation`
861
+ - `file_path`
862
+
863
+ These annotations directly correspond to the annotation objects defined by the Responses API and can be used for inline reference rendering or output analysis.
864
+ For details, see the official OpenAI documentation:
865
+ [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).
866
+
867
+ ```ts
868
+ import {
869
+ openai,
870
+ type OpenaiResponsesTextProviderMetadata,
871
+ } from '@ai-sdk/openai';
872
+ import { generateText } from 'ai';
873
+
874
+ const result = await generateText({
875
+ model: openai('gpt-4.1-mini'),
876
+ prompt:
877
+ '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.',
878
+ tools: {
879
+ code_interpreter: openai.tools.codeInterpreter(),
880
+ web_search: openai.tools.webSearch(),
881
+ file_search: openai.tools.fileSearch({ vectorStoreIds: ['vs_1234'] }), // requires a configured vector store
882
+ },
883
+ });
884
+
885
+ for (const part of result.content) {
886
+ if (part.type === 'text') {
887
+ const providerMetadata = part.providerMetadata as
888
+ | OpenaiResponsesTextProviderMetadata
889
+ | undefined;
890
+ if (!providerMetadata) continue;
891
+ const { itemId: _itemId, annotations } = providerMetadata.openai;
892
+
893
+ if (!annotations) continue;
894
+ for (const annotation of annotations) {
895
+ switch (annotation.type) {
896
+ case 'url_citation':
897
+ // url_citation is returned from web_search and provides:
898
+ // properties: type, url, title, start_index and end_index
899
+ break;
900
+ case 'file_citation':
901
+ // file_citation is returned from file_search and provides:
902
+ // properties: type, file_id, filename and index
903
+ break;
904
+ case 'container_file_citation':
905
+ // container_file_citation is returned from code_interpreter and provides:
906
+ // properties: type, container_id, file_id, filename, start_index and end_index
907
+ break;
908
+ case 'file_path':
909
+ // file_path provides:
910
+ // properties: type, file_id and index
911
+ break;
912
+ default: {
913
+ const _exhaustiveCheck: never = annotation;
914
+ throw new Error(
915
+ `Unhandled annotation: ${JSON.stringify(_exhaustiveCheck)}`,
916
+ );
917
+ }
918
+ }
919
+ }
920
+ }
921
+ }
922
+ ```
923
+
924
+ <Note>
925
+ When implementing file downloads for files generated by the Code Interpreter,
926
+ the `container_id` and `file_id` available in `providerMetadata` can be used
927
+ to retrieve the file content. For details, see the [Retrieve container file
928
+ content](https://platform.openai.com/docs/api-reference/container-files/retrieveContainerFileContent)
929
+ API.
930
+ </Note>
931
+
932
+ #### Typed providerMetadata in Source Document Parts
933
+
934
+ For source document parts, when `part.type === 'source'` and `sourceType === 'document'`, the `providerMetadata` is provided as `OpenaiResponsesSourceDocumentProviderMetadata`.
935
+
936
+ This metadata is also a discriminated union with a required `type` field. Supported types include:
937
+
938
+ - `file_citation`
939
+ - `container_file_citation`
940
+ - `file_path`
941
+
942
+ Each type includes the identifiers required to work with the referenced resource, such as `fileId` and `containerId`.
943
+
944
+ ```ts
945
+ import {
946
+ openai,
947
+ type OpenaiResponsesSourceDocumentProviderMetadata,
948
+ } from '@ai-sdk/openai';
949
+ import { generateText } from 'ai';
950
+
951
+ const result = await generateText({
952
+ model: openai('gpt-4.1-mini'),
953
+ prompt:
954
+ '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.',
955
+ tools: {
956
+ code_interpreter: openai.tools.codeInterpreter(),
957
+ web_search: openai.tools.webSearch(),
958
+ file_search: openai.tools.fileSearch({ vectorStoreIds: ['vs_1234'] }), // requires a configured vector store
959
+ },
960
+ });
961
+
962
+ for (const part of result.content) {
963
+ if (part.type === 'source') {
964
+ if (part.sourceType === 'document') {
965
+ const providerMetadata = part.providerMetadata as
966
+ | OpenaiResponsesSourceDocumentProviderMetadata
967
+ | undefined;
968
+ if (!providerMetadata) continue;
969
+ const annotation = providerMetadata.openai;
970
+ switch (annotation.type) {
971
+ case 'file_citation':
972
+ // file_citation is returned from file_search and provides:
973
+ // properties: type, fileId and index
974
+ // The filename can be accessed via part.filename.
975
+ break;
976
+ case 'container_file_citation':
977
+ // container_file_citation is returned from code_interpreter and provides:
978
+ // properties: type, containerId and fileId
979
+ // The filename can be accessed via part.filename.
980
+ break;
981
+ case 'file_path':
982
+ // file_path provides:
983
+ // properties: type, fileId and index
984
+ break;
985
+ default: {
986
+ const _exhaustiveCheck: never = annotation;
987
+ throw new Error(
988
+ `Unhandled annotation: ${JSON.stringify(_exhaustiveCheck)}`,
989
+ );
990
+ }
991
+ }
992
+ }
993
+ }
994
+ }
995
+ ```
996
+
997
+ <Note>
998
+ Annotations in text parts follow the OpenAI Responses API specification and
999
+ therefore use snake_case properties (e.g. `file_id`, `container_id`). In
1000
+ contrast, `providerMetadata` for source document parts is normalized by the
1001
+ SDK to camelCase (e.g. `fileId`, `containerId`). Fields that depend on the
1002
+ original text content, such as `start_index` and `end_index`, are omitted, as
1003
+ are fields like `filename` that are directly available on the source object.
1004
+ </Note>
1005
+
1006
+ ### Chat Models
1007
+
1008
+ You can create models that call the [OpenAI chat API](https://platform.openai.com/docs/api-reference/chat) using the `.chat()` factory method.
1009
+ The first argument is the model id, e.g. `gpt-4`.
1010
+ The OpenAI chat models support tool calls and some have multi-modal capabilities.
1011
+
1012
+ ```ts
1013
+ const model = openai.chat('gpt-5');
1014
+ ```
1015
+
1016
+ OpenAI chat models support also some model specific provider options that are not part of the [standard call settings](/docs/ai-sdk-core/settings).
1017
+ You can pass them in the `providerOptions` argument:
1018
+
1019
+ ```ts
1020
+ import { openai, type OpenAIChatLanguageModelOptions } from '@ai-sdk/openai';
1021
+
1022
+ const model = openai.chat('gpt-5');
1023
+
1024
+ await generateText({
1025
+ model,
1026
+ providerOptions: {
1027
+ openai: {
1028
+ logitBias: {
1029
+ // optional likelihood for specific tokens
1030
+ '50256': -100,
1031
+ },
1032
+ user: 'test-user', // optional unique user identifier
1033
+ } satisfies OpenAIChatLanguageModelOptions,
1034
+ },
1035
+ });
1036
+ ```
1037
+
1038
+ The following optional provider options are available for OpenAI chat models:
1039
+
1040
+ - **logitBias** _Record&lt;number, number&gt;_
1041
+
1042
+ Modifies the likelihood of specified tokens appearing in the completion.
1043
+
1044
+ Accepts a JSON object that maps tokens (specified by their token ID in
1045
+ the GPT tokenizer) to an associated bias value from -100 to 100. You
1046
+ can use this tokenizer tool to convert text to token IDs. Mathematically,
1047
+ the bias is added to the logits generated by the model prior to sampling.
1048
+ The exact effect will vary per model, but values between -1 and 1 should
1049
+ decrease or increase likelihood of selection; values like -100 or 100
1050
+ should result in a ban or exclusive selection of the relevant token.
1051
+
1052
+ As an example, you can pass `{"50256": -100}` to prevent the token from being generated.
1053
+
1054
+ - **logprobs** _boolean | number_
1055
+
1056
+ Return the log probabilities of the tokens. Including logprobs will increase
1057
+ the response size and can slow down response times. However, it can
1058
+ be useful to better understand how the model is behaving.
1059
+
1060
+ Setting to true will return the log probabilities of the tokens that
1061
+ were generated.
1062
+
1063
+ Setting to a number will return the log probabilities of the top n
1064
+ tokens that were generated.
1065
+
1066
+ - **parallelToolCalls** _boolean_
1067
+
1068
+ Whether to enable parallel function calling during tool use. Defaults to `true`.
1069
+
1070
+ - **user** _string_
1071
+
1072
+ A unique identifier representing your end-user, which can help OpenAI to
1073
+ monitor and detect abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids).
1074
+
1075
+ - **reasoningEffort** _'minimal' | 'low' | 'medium' | 'high' | 'xhigh'_
1076
+
1077
+ Reasoning effort for reasoning models. Defaults to `medium`. If you use
1078
+ `providerOptions` to set the `reasoningEffort` option, this
1079
+ model setting will be ignored.
1080
+
1081
+ - **maxCompletionTokens** _number_
1082
+
1083
+ Maximum number of completion tokens to generate. Useful for reasoning models.
1084
+
1085
+ - **store** _boolean_
1086
+
1087
+ Whether to enable persistence in Responses API.
1088
+
1089
+ - **metadata** _Record&lt;string, string&gt;_
1090
+
1091
+ Metadata to associate with the request.
1092
+
1093
+ - **prediction** _Record&lt;string, any&gt;_
1094
+
1095
+ Parameters for prediction mode.
1096
+
1097
+ - **serviceTier** _'auto' | 'flex' | 'priority' | 'default'_
1098
+
1099
+ Service tier for the request. Set to 'flex' for 50% cheaper processing
1100
+ at the cost of increased latency (available for o3, o4-mini, and gpt-5 models).
1101
+ 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).
1102
+
1103
+ Defaults to 'auto'.
1104
+
1105
+ - **strictJsonSchema** _boolean_
1106
+
1107
+ Whether to use strict JSON schema validation.
1108
+ Defaults to `true`.
1109
+
1110
+ - **textVerbosity** _'low' | 'medium' | 'high'_
1111
+
1112
+ Controls the verbosity of the model's responses. Lower values will result in more concise responses, while higher values will result in more verbose responses.
1113
+
1114
+ - **promptCacheKey** _string_
1115
+
1116
+ A cache key for manual prompt caching control. Used by OpenAI to cache responses for similar requests to optimize your cache hit rates.
1117
+
1118
+ - **promptCacheRetention** _'in_memory' | '24h'_
1119
+
1120
+ The retention policy for the prompt cache. Set to `'24h'` to enable extended prompt caching, which keeps cached prefixes active for up to 24 hours. Defaults to `'in_memory'` for standard prompt caching. Note: `'24h'` is currently only available for the 5.1 series of models.
1121
+
1122
+ - **safetyIdentifier** _string_
1123
+
1124
+ 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.
1125
+
1126
+ #### Reasoning
1127
+
1128
+ OpenAI has introduced the `o1`,`o3`, and `o4` series of [reasoning models](https://platform.openai.com/docs/guides/reasoning).
1129
+ Currently, `o4-mini`, `o3`, `o3-mini`, and `o1` are available via both the chat and responses APIs. The
1130
+ models `codex-mini-latest` and `computer-use-preview` are available only via the [responses API](#responses-models).
1131
+
1132
+ Reasoning models currently only generate text, have several limitations, and are only supported using `generateText` and `streamText`.
1133
+
1134
+ They support additional settings and response metadata:
1135
+
1136
+ - You can use `providerOptions` to set
1137
+
1138
+ - the `reasoningEffort` option (or alternatively the `reasoningEffort` model setting), which determines the amount of reasoning the model performs.
1139
+
1140
+ - You can use response `providerMetadata` to access the number of reasoning tokens that the model generated.
1141
+
1142
+ ```ts highlight="4,7-11,17"
1143
+ import { openai } from '@ai-sdk/openai';
1144
+ import { generateText } from 'ai';
1145
+
1146
+ const { text, usage, providerMetadata } = await generateText({
1147
+ model: openai.chat('gpt-5'),
1148
+ prompt: 'Invent a new holiday and describe its traditions.',
1149
+ providerOptions: {
1150
+ openai: {
1151
+ reasoningEffort: 'low',
1152
+ },
1153
+ },
1154
+ });
1155
+
1156
+ console.log(text);
1157
+ console.log('Usage:', {
1158
+ ...usage,
1159
+ reasoningTokens: providerMetadata?.openai?.reasoningTokens,
1160
+ });
1161
+ ```
1162
+
1163
+ <Note>
1164
+ System messages are automatically converted to OpenAI developer messages for
1165
+ reasoning models when supported.
1166
+ </Note>
1167
+
1168
+ - You can control how system messages are handled by providerOptions `systemMessageMode`:
1169
+
1170
+ - `developer`: treat the prompt as a developer message (default for reasoning models).
1171
+ - `system`: keep the system message as a system-level instruction.
1172
+ - `remove`: remove the system message from the messages.
1173
+
1174
+ ```ts highlight="12"
1175
+ import { openai } from '@ai-sdk/openai';
1176
+ import { generateText } from 'ai';
1177
+
1178
+ const result = await generateText({
1179
+ model: openai.chat('gpt-5'),
1180
+ messages: [
1181
+ { role: 'system', content: 'You are a helpful assistant.' },
1182
+ { role: 'user', content: 'Tell me a joke.' },
1183
+ ],
1184
+ providerOptions: {
1185
+ openai: {
1186
+ systemMessageMode: 'system',
1187
+ },
1188
+ },
1189
+ });
1190
+ ```
1191
+
1192
+ <Note>
1193
+ Reasoning models require additional runtime inference to complete their
1194
+ reasoning phase before generating a response. This introduces longer latency
1195
+ compared to other models.
1196
+ </Note>
1197
+
1198
+ <Note>
1199
+ `maxOutputTokens` is automatically mapped to `max_completion_tokens` for
1200
+ reasoning models.
1201
+ </Note>
1202
+
1203
+ #### Strict Structured Outputs
1204
+
1205
+ Strict structured outputs are enabled by default.
1206
+ You can disable them by setting the `strictJsonSchema` option to `false`.
1207
+
1208
+ ```ts highlight="7"
1209
+ import { openai, OpenAIChatLanguageModelOptions } from '@ai-sdk/openai';
1210
+ import { generateObject } from 'ai';
1211
+ import { z } from 'zod';
1212
+
1213
+ const result = await generateObject({
1214
+ model: openai.chat('gpt-4o-2024-08-06'),
1215
+ providerOptions: {
1216
+ openai: {
1217
+ strictJsonSchema: false,
1218
+ } satisfies OpenAIChatLanguageModelOptions,
1219
+ },
1220
+ schemaName: 'recipe',
1221
+ schemaDescription: 'A recipe for lasagna.',
1222
+ schema: z.object({
1223
+ name: z.string(),
1224
+ ingredients: z.array(
1225
+ z.object({
1226
+ name: z.string(),
1227
+ amount: z.string(),
1228
+ }),
1229
+ ),
1230
+ steps: z.array(z.string()),
1231
+ }),
1232
+ prompt: 'Generate a lasagna recipe.',
1233
+ });
1234
+
1235
+ console.log(JSON.stringify(result.object, null, 2));
1236
+ ```
1237
+
1238
+ <Note type="warning">
1239
+ OpenAI structured outputs have several
1240
+ [limitations](https://openai.com/index/introducing-structured-outputs-in-the-api),
1241
+ in particular around the [supported schemas](https://platform.openai.com/docs/guides/structured-outputs/supported-schemas),
1242
+ and are therefore opt-in.
1243
+
1244
+ For example, optional schema properties are not supported.
1245
+ You need to change Zod `.nullish()` and `.optional()` to `.nullable()`.
1246
+
1247
+ </Note>
1248
+
1249
+ #### Logprobs
1250
+
1251
+ OpenAI provides logprobs information for completion/chat models.
1252
+ You can access it in the `providerMetadata` object.
1253
+
1254
+ ```ts highlight="11"
1255
+ import { openai } from '@ai-sdk/openai';
1256
+ import { generateText } from 'ai';
1257
+
1258
+ const result = await generateText({
1259
+ model: openai.chat('gpt-5'),
1260
+ prompt: 'Write a vegetarian lasagna recipe for 4 people.',
1261
+ providerOptions: {
1262
+ openai: {
1263
+ // this can also be a number,
1264
+ // refer to logprobs provider options section for more
1265
+ logprobs: true,
1266
+ },
1267
+ },
1268
+ });
1269
+
1270
+ const openaiMetadata = (await result.providerMetadata)?.openai;
1271
+
1272
+ const logprobs = openaiMetadata?.logprobs;
1273
+ ```
1274
+
1275
+ #### Image Support
1276
+
1277
+ The OpenAI Chat API supports Image inputs for appropriate models.
1278
+ You can pass Image files as part of the message content using the 'image' type:
1279
+
1280
+ ```ts
1281
+ const result = await generateText({
1282
+ model: openai.chat('gpt-5'),
1283
+ messages: [
1284
+ {
1285
+ role: 'user',
1286
+ content: [
1287
+ {
1288
+ type: 'text',
1289
+ text: 'Please describe the image.',
1290
+ },
1291
+ {
1292
+ type: 'image',
1293
+ image: readFileSync('./data/image.png'),
1294
+ },
1295
+ ],
1296
+ },
1297
+ ],
1298
+ });
1299
+ ```
1300
+
1301
+ The model will have access to the image and will respond to questions about it.
1302
+ The image should be passed using the `image` field.
1303
+
1304
+ You can also pass the URL of an image.
1305
+
1306
+ ```ts
1307
+ {
1308
+ type: 'image',
1309
+ image: 'https://sample.edu/image.png',
1310
+ }
1311
+ ```
1312
+
1313
+ #### PDF support
1314
+
1315
+ The OpenAI Chat API supports reading PDF files.
1316
+ You can pass PDF files as part of the message content using the `file` type:
1317
+
1318
+ ```ts
1319
+ const result = await generateText({
1320
+ model: openai.chat('gpt-5'),
1321
+ messages: [
1322
+ {
1323
+ role: 'user',
1324
+ content: [
1325
+ {
1326
+ type: 'text',
1327
+ text: 'What is an embedding model?',
1328
+ },
1329
+ {
1330
+ type: 'file',
1331
+ data: readFileSync('./data/ai.pdf'),
1332
+ mediaType: 'application/pdf',
1333
+ filename: 'ai.pdf', // optional
1334
+ },
1335
+ ],
1336
+ },
1337
+ ],
1338
+ });
1339
+ ```
1340
+
1341
+ The model will have access to the contents of the PDF file and
1342
+ respond to questions about it.
1343
+ The PDF file should be passed using the `data` field,
1344
+ and the `mediaType` should be set to `'application/pdf'`.
1345
+
1346
+ You can also pass a file-id from the OpenAI Files API.
1347
+
1348
+ ```ts
1349
+ {
1350
+ type: 'file',
1351
+ data: 'file-8EFBcWHsQxZV7YGezBC1fq',
1352
+ mediaType: 'application/pdf',
1353
+ }
1354
+ ```
1355
+
1356
+ You can also pass the URL of a PDF.
1357
+
1358
+ ```ts
1359
+ {
1360
+ type: 'file',
1361
+ data: 'https://sample.edu/example.pdf',
1362
+ mediaType: 'application/pdf',
1363
+ filename: 'ai.pdf', // optional
1364
+ }
1365
+ ```
1366
+
1367
+ #### Predicted Outputs
1368
+
1369
+ OpenAI supports [predicted outputs](https://platform.openai.com/docs/guides/latency-optimization#use-predicted-outputs) for `gpt-4o` and `gpt-4o-mini`.
1370
+ Predicted outputs help you reduce latency by allowing you to specify a base text that the model should modify.
1371
+ You can enable predicted outputs by adding the `prediction` option to the `providerOptions.openai` object:
1372
+
1373
+ ```ts highlight="15-18"
1374
+ const result = streamText({
1375
+ model: openai.chat('gpt-5'),
1376
+ messages: [
1377
+ {
1378
+ role: 'user',
1379
+ content: 'Replace the Username property with an Email property.',
1380
+ },
1381
+ {
1382
+ role: 'user',
1383
+ content: existingCode,
1384
+ },
1385
+ ],
1386
+ providerOptions: {
1387
+ openai: {
1388
+ prediction: {
1389
+ type: 'content',
1390
+ content: existingCode,
1391
+ },
1392
+ },
1393
+ },
1394
+ });
1395
+ ```
1396
+
1397
+ OpenAI provides usage information for predicted outputs (`acceptedPredictionTokens` and `rejectedPredictionTokens`).
1398
+ You can access it in the `providerMetadata` object.
1399
+
1400
+ ```ts highlight="11"
1401
+ const openaiMetadata = (await result.providerMetadata)?.openai;
1402
+
1403
+ const acceptedPredictionTokens = openaiMetadata?.acceptedPredictionTokens;
1404
+ const rejectedPredictionTokens = openaiMetadata?.rejectedPredictionTokens;
1405
+ ```
1406
+
1407
+ <Note type="warning">
1408
+ OpenAI Predicted Outputs have several
1409
+ [limitations](https://platform.openai.com/docs/guides/predicted-outputs#limitations),
1410
+ e.g. unsupported API parameters and no tool calling support.
1411
+ </Note>
1412
+
1413
+ #### Image Detail
1414
+
1415
+ You can use the `openai` provider option to set the [image input detail](https://platform.openai.com/docs/guides/images-vision?api-mode=responses#specify-image-input-detail-level) to `high`, `low`, or `auto`:
1416
+
1417
+ ```ts highlight="13-16"
1418
+ const result = await generateText({
1419
+ model: openai.chat('gpt-5'),
1420
+ messages: [
1421
+ {
1422
+ role: 'user',
1423
+ content: [
1424
+ { type: 'text', text: 'Describe the image in detail.' },
1425
+ {
1426
+ type: 'image',
1427
+ image:
1428
+ 'https://github.com/vercel/ai/blob/main/examples/ai-functions/data/comic-cat.png?raw=true',
1429
+
1430
+ // OpenAI specific options - image detail:
1431
+ providerOptions: {
1432
+ openai: { imageDetail: 'low' },
1433
+ },
1434
+ },
1435
+ ],
1436
+ },
1437
+ ],
1438
+ });
1439
+ ```
1440
+
1441
+ <Note type="warning">
1442
+ Because the `UIMessage` type (used by AI SDK UI hooks like `useChat`) does not
1443
+ support the `providerOptions` property, you can use `convertToModelMessages`
1444
+ first before passing the messages to functions like `generateText` or
1445
+ `streamText`. For more details on `providerOptions` usage, see
1446
+ [here](/docs/foundations/prompts#provider-options).
1447
+ </Note>
1448
+
1449
+ #### Distillation
1450
+
1451
+ OpenAI supports model distillation for some models.
1452
+ If you want to store a generation for use in the distillation process, you can add the `store` option to the `providerOptions.openai` object.
1453
+ This will save the generation to the OpenAI platform for later use in distillation.
1454
+
1455
+ ```typescript highlight="9-16"
1456
+ import { openai } from '@ai-sdk/openai';
1457
+ import { generateText } from 'ai';
1458
+ import 'dotenv/config';
1459
+
1460
+ async function main() {
1461
+ const { text, usage } = await generateText({
1462
+ model: openai.chat('gpt-4o-mini'),
1463
+ prompt: 'Who worked on the original macintosh?',
1464
+ providerOptions: {
1465
+ openai: {
1466
+ store: true,
1467
+ metadata: {
1468
+ custom: 'value',
1469
+ },
1470
+ },
1471
+ },
1472
+ });
1473
+
1474
+ console.log(text);
1475
+ console.log();
1476
+ console.log('Usage:', usage);
1477
+ }
1478
+
1479
+ main().catch(console.error);
1480
+ ```
1481
+
1482
+ #### Prompt Caching
1483
+
1484
+ OpenAI has introduced [Prompt Caching](https://platform.openai.com/docs/guides/prompt-caching) for supported models
1485
+ including `gpt-4o` and `gpt-4o-mini`.
1486
+
1487
+ - Prompt caching is automatically enabled for these models, when the prompt is 1024 tokens or longer. It does
1488
+ not need to be explicitly enabled.
1489
+ - You can use response `providerMetadata` to access the number of prompt tokens that were a cache hit.
1490
+ - Note that caching behavior is dependent on load on OpenAI's infrastructure. Prompt prefixes generally remain in the
1491
+ cache following 5-10 minutes of inactivity before they are evicted, but during off-peak periods they may persist for up
1492
+ to an hour.
1493
+
1494
+ ```ts highlight="11"
1495
+ import { openai } from '@ai-sdk/openai';
1496
+ import { generateText } from 'ai';
1497
+
1498
+ const { text, usage, providerMetadata } = await generateText({
1499
+ model: openai.chat('gpt-4o-mini'),
1500
+ prompt: `A 1024-token or longer prompt...`,
1501
+ });
1502
+
1503
+ console.log(`usage:`, {
1504
+ ...usage,
1505
+ cachedPromptTokens: providerMetadata?.openai?.cachedPromptTokens,
1506
+ });
1507
+ ```
1508
+
1509
+ To improve cache hit rates, you can manually control caching using the `promptCacheKey` option:
1510
+
1511
+ ```ts highlight="7-11"
1512
+ import { openai } from '@ai-sdk/openai';
1513
+ import { generateText } from 'ai';
1514
+
1515
+ const { text, usage, providerMetadata } = await generateText({
1516
+ model: openai.chat('gpt-5'),
1517
+ prompt: `A 1024-token or longer prompt...`,
1518
+ providerOptions: {
1519
+ openai: {
1520
+ promptCacheKey: 'my-custom-cache-key-123',
1521
+ },
1522
+ },
1523
+ });
1524
+
1525
+ console.log(`usage:`, {
1526
+ ...usage,
1527
+ cachedPromptTokens: providerMetadata?.openai?.cachedPromptTokens,
1528
+ });
1529
+ ```
1530
+
1531
+ For GPT-5.1 models, you can enable extended prompt caching that keeps cached prefixes active for up to 24 hours:
1532
+
1533
+ ```ts highlight="7-12"
1534
+ import { openai } from '@ai-sdk/openai';
1535
+ import { generateText } from 'ai';
1536
+
1537
+ const { text, usage, providerMetadata } = await generateText({
1538
+ model: openai.chat('gpt-5.1'),
1539
+ prompt: `A 1024-token or longer prompt...`,
1540
+ providerOptions: {
1541
+ openai: {
1542
+ promptCacheKey: 'my-custom-cache-key-123',
1543
+ promptCacheRetention: '24h', // Extended caching for GPT-5.1
1544
+ },
1545
+ },
1546
+ });
1547
+
1548
+ console.log(`usage:`, {
1549
+ ...usage,
1550
+ cachedPromptTokens: providerMetadata?.openai?.cachedPromptTokens,
1551
+ });
1552
+ ```
1553
+
1554
+ #### Audio Input
1555
+
1556
+ With the `gpt-4o-audio-preview` model, you can pass audio files to the model.
1557
+
1558
+ <Note type="warning">
1559
+ The `gpt-4o-audio-preview` model is currently in preview and requires at least
1560
+ some audio inputs. It will not work with non-audio data.
1561
+ </Note>
1562
+
1563
+ ```ts highlight="12-14"
1564
+ import { openai } from '@ai-sdk/openai';
1565
+ import { generateText } from 'ai';
1566
+
1567
+ const result = await generateText({
1568
+ model: openai.chat('gpt-4o-audio-preview'),
1569
+ messages: [
1570
+ {
1571
+ role: 'user',
1572
+ content: [
1573
+ { type: 'text', text: 'What is the audio saying?' },
1574
+ {
1575
+ type: 'file',
1576
+ mediaType: 'audio/mpeg',
1577
+ data: readFileSync('./data/galileo.mp3'),
1578
+ },
1579
+ ],
1580
+ },
1581
+ ],
1582
+ });
1583
+ ```
1584
+
1585
+ ### Completion Models
1586
+
1587
+ You can create models that call the [OpenAI completions API](https://platform.openai.com/docs/api-reference/completions) using the `.completion()` factory method.
1588
+ The first argument is the model id.
1589
+ Currently only `gpt-3.5-turbo-instruct` is supported.
1590
+
1591
+ ```ts
1592
+ const model = openai.completion('gpt-3.5-turbo-instruct');
1593
+ ```
1594
+
1595
+ OpenAI completion models support also some model specific settings that are not part of the [standard call settings](/docs/ai-sdk-core/settings).
1596
+ You can pass them as an options argument:
1597
+
1598
+ ```ts
1599
+ const model = openai.completion('gpt-3.5-turbo-instruct');
1600
+
1601
+ await model.doGenerate({
1602
+ providerOptions: {
1603
+ openai: {
1604
+ echo: true, // optional, echo the prompt in addition to the completion
1605
+ logitBias: {
1606
+ // optional likelihood for specific tokens
1607
+ '50256': -100,
1608
+ },
1609
+ suffix: 'some text', // optional suffix that comes after a completion of inserted text
1610
+ user: 'test-user', // optional unique user identifier
1611
+ },
1612
+ },
1613
+ });
1614
+ ```
1615
+
1616
+ The following optional provider options are available for OpenAI completion models:
1617
+
1618
+ - **echo**: _boolean_
1619
+
1620
+ Echo back the prompt in addition to the completion.
1621
+
1622
+ - **logitBias** _Record&lt;number, number&gt;_
1623
+
1624
+ Modifies the likelihood of specified tokens appearing in the completion.
1625
+
1626
+ Accepts a JSON object that maps tokens (specified by their token ID in
1627
+ the GPT tokenizer) to an associated bias value from -100 to 100. You
1628
+ can use this tokenizer tool to convert text to token IDs. Mathematically,
1629
+ the bias is added to the logits generated by the model prior to sampling.
1630
+ The exact effect will vary per model, but values between -1 and 1 should
1631
+ decrease or increase likelihood of selection; values like -100 or 100
1632
+ should result in a ban or exclusive selection of the relevant token.
1633
+
1634
+ As an example, you can pass `{"50256": -100}` to prevent the &lt;|endoftext|&gt;
1635
+ token from being generated.
1636
+
1637
+ - **logprobs** _boolean | number_
1638
+
1639
+ Return the log probabilities of the tokens. Including logprobs will increase
1640
+ the response size and can slow down response times. However, it can
1641
+ be useful to better understand how the model is behaving.
1642
+
1643
+ Setting to true will return the log probabilities of the tokens that
1644
+ were generated.
1645
+
1646
+ Setting to a number will return the log probabilities of the top n
1647
+ tokens that were generated.
1648
+
1649
+ - **suffix** _string_
1650
+
1651
+ The suffix that comes after a completion of inserted text.
1652
+
1653
+ - **user** _string_
1654
+
1655
+ A unique identifier representing your end-user, which can help OpenAI to
1656
+ monitor and detect abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids).
1657
+
1658
+ ### Model Capabilities
1659
+
1660
+ | Model | Image Input | Audio Input | Object Generation | Tool Usage |
1661
+ | --------------------- | ------------------- | ------------------- | ------------------- | ------------------- |
1662
+ | `gpt-5.2-pro` | <Check size={18} /> | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> |
1663
+ | `gpt-5.2-chat-latest` | <Check size={18} /> | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> |
1664
+ | `gpt-5.2` | <Check size={18} /> | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> |
1665
+ | `gpt-5.1-codex-mini` | <Check size={18} /> | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> |
1666
+ | `gpt-5.1-codex` | <Check size={18} /> | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> |
1667
+ | `gpt-5.1-chat-latest` | <Check size={18} /> | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> |
1668
+ | `gpt-5.1` | <Check size={18} /> | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> |
1669
+ | `gpt-5-pro` | <Check size={18} /> | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> |
1670
+ | `gpt-5` | <Check size={18} /> | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> |
1671
+ | `gpt-5-mini` | <Check size={18} /> | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> |
1672
+ | `gpt-5-nano` | <Check size={18} /> | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> |
1673
+ | `gpt-5-codex` | <Check size={18} /> | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> |
1674
+ | `gpt-5-chat-latest` | <Check size={18} /> | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> |
1675
+ | `gpt-4.1` | <Check size={18} /> | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> |
1676
+ | `gpt-4.1-mini` | <Check size={18} /> | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> |
1677
+ | `gpt-4.1-nano` | <Check size={18} /> | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> |
1678
+ | `gpt-4o` | <Check size={18} /> | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> |
1679
+ | `gpt-4o-mini` | <Check size={18} /> | <Cross size={18} /> | <Check size={18} /> | <Check size={18} /> |
1680
+
1681
+ <Note>
1682
+ The table above lists popular models. Please see the [OpenAI
1683
+ docs](https://platform.openai.com/docs/models) for a full list of available
1684
+ models. The table above lists popular models. You can also pass any available
1685
+ provider model ID as a string if needed.
1686
+ </Note>
1687
+
1688
+ ## Embedding Models
1689
+
1690
+ You can create models that call the [OpenAI embeddings API](https://platform.openai.com/docs/api-reference/embeddings)
1691
+ using the `.embedding()` factory method.
1692
+
1693
+ ```ts
1694
+ const model = openai.embedding('text-embedding-3-large');
1695
+ ```
1696
+
1697
+ OpenAI embedding models support several additional provider options.
1698
+ You can pass them as an options argument:
1699
+
1700
+ ```ts
1701
+ import { openai } from '@ai-sdk/openai';
1702
+ import { embed } from 'ai';
1703
+
1704
+ const { embedding } = await embed({
1705
+ model: openai.embedding('text-embedding-3-large'),
1706
+ value: 'sunny day at the beach',
1707
+ providerOptions: {
1708
+ openai: {
1709
+ dimensions: 512, // optional, number of dimensions for the embedding
1710
+ user: 'test-user', // optional unique user identifier
1711
+ },
1712
+ },
1713
+ });
1714
+ ```
1715
+
1716
+ The following optional provider options are available for OpenAI embedding models:
1717
+
1718
+ - **dimensions**: _number_
1719
+
1720
+ The number of dimensions the resulting output embeddings should have.
1721
+ Only supported in text-embedding-3 and later models.
1722
+
1723
+ - **user** _string_
1724
+
1725
+ A unique identifier representing your end-user, which can help OpenAI to
1726
+ monitor and detect abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids).
1727
+
1728
+ ### Model Capabilities
1729
+
1730
+ | Model | Default Dimensions | Custom Dimensions |
1731
+ | ------------------------ | ------------------ | ------------------- |
1732
+ | `text-embedding-3-large` | 3072 | <Check size={18} /> |
1733
+ | `text-embedding-3-small` | 1536 | <Check size={18} /> |
1734
+ | `text-embedding-ada-002` | 1536 | <Cross size={18} /> |
1735
+
1736
+ ## Image Models
1737
+
1738
+ You can create models that call the [OpenAI image generation API](https://platform.openai.com/docs/api-reference/images)
1739
+ using the `.image()` factory method.
1740
+
1741
+ ```ts
1742
+ const model = openai.image('dall-e-3');
1743
+ ```
1744
+
1745
+ <Note>
1746
+ Dall-E models do not support the `aspectRatio` parameter. Use the `size`
1747
+ parameter instead.
1748
+ </Note>
1749
+
1750
+ ### Image Editing
1751
+
1752
+ OpenAI's `gpt-image-1` model supports powerful image editing capabilities. Pass input images via `prompt.images` to transform, combine, or edit existing images.
1753
+
1754
+ #### Basic Image Editing
1755
+
1756
+ Transform an existing image using text prompts:
1757
+
1758
+ ```ts
1759
+ const imageBuffer = readFileSync('./input-image.png');
1760
+
1761
+ const { images } = await generateImage({
1762
+ model: openai.image('gpt-image-1'),
1763
+ prompt: {
1764
+ text: 'Turn the cat into a dog but retain the style of the original image',
1765
+ images: [imageBuffer],
1766
+ },
1767
+ });
1768
+ ```
1769
+
1770
+ #### Inpainting with Mask
1771
+
1772
+ Edit specific parts of an image using a mask. Transparent areas in the mask indicate where the image should be edited:
1773
+
1774
+ ```ts
1775
+ const image = readFileSync('./input-image.png');
1776
+ const mask = readFileSync('./mask.png'); // Transparent areas = edit regions
1777
+
1778
+ const { images } = await generateImage({
1779
+ model: openai.image('gpt-image-1'),
1780
+ prompt: {
1781
+ text: 'A sunlit indoor lounge area with a pool containing a flamingo',
1782
+ images: [image],
1783
+ mask: mask,
1784
+ },
1785
+ });
1786
+ ```
1787
+
1788
+ #### Background Removal
1789
+
1790
+ Remove the background from an image by setting `background` to `transparent`:
1791
+
1792
+ ```ts
1793
+ const imageBuffer = readFileSync('./input-image.png');
1794
+
1795
+ const { images } = await generateImage({
1796
+ model: openai.image('gpt-image-1'),
1797
+ prompt: {
1798
+ text: 'do not change anything',
1799
+ images: [imageBuffer],
1800
+ },
1801
+ providerOptions: {
1802
+ openai: {
1803
+ background: 'transparent',
1804
+ output_format: 'png',
1805
+ },
1806
+ },
1807
+ });
1808
+ ```
1809
+
1810
+ #### Multi-Image Combining
1811
+
1812
+ Combine multiple reference images into a single output. `gpt-image-1` supports up to 16 input images:
1813
+
1814
+ ```ts
1815
+ const cat = readFileSync('./cat.png');
1816
+ const dog = readFileSync('./dog.png');
1817
+ const owl = readFileSync('./owl.png');
1818
+ const bear = readFileSync('./bear.png');
1819
+
1820
+ const { images } = await generateImage({
1821
+ model: openai.image('gpt-image-1'),
1822
+ prompt: {
1823
+ text: 'Combine these animals into a group photo, retaining the original style',
1824
+ images: [cat, dog, owl, bear],
1825
+ },
1826
+ });
1827
+ ```
1828
+
1829
+ <Note>
1830
+ Input images can be provided as `Buffer`, `ArrayBuffer`, `Uint8Array`, or
1831
+ base64-encoded strings. For `gpt-image-1`, each image should be a `png`,
1832
+ `webp`, or `jpg` file less than 50MB.
1833
+ </Note>
1834
+
1835
+ ### Model Capabilities
1836
+
1837
+ | Model | Sizes |
1838
+ | ------------------ | ------------------------------- |
1839
+ | `gpt-image-1.5` | 1024x1024, 1536x1024, 1024x1536 |
1840
+ | `gpt-image-1-mini` | 1024x1024, 1536x1024, 1024x1536 |
1841
+ | `gpt-image-1` | 1024x1024, 1536x1024, 1024x1536 |
1842
+ | `dall-e-3` | 1024x1024, 1792x1024, 1024x1792 |
1843
+ | `dall-e-2` | 256x256, 512x512, 1024x1024 |
1844
+
1845
+ You can pass optional `providerOptions` to the image model. These are prone to change by OpenAI and are model dependent. For example, the `gpt-image-1` model supports the `quality` option:
1846
+
1847
+ ```ts
1848
+ const { image, providerMetadata } = await generateImage({
1849
+ model: openai.image('gpt-image-1.5'),
1850
+ prompt: 'A salamander at sunrise in a forest pond in the Seychelles.',
1851
+ providerOptions: {
1852
+ openai: { quality: 'high' },
1853
+ },
1854
+ });
1855
+ ```
1856
+
1857
+ For more on `generateImage()` see [Image Generation](/docs/ai-sdk-core/image-generation).
1858
+
1859
+ OpenAI's image models return additional metadata in the response that can be
1860
+ accessed via `providerMetadata.openai`. The following OpenAI-specific metadata
1861
+ is available:
1862
+
1863
+ - **images** _Array&lt;object&gt;_
1864
+
1865
+ Array of image-specific metadata. Each image object may contain:
1866
+
1867
+ - `revisedPrompt` _string_ - The revised prompt that was actually used to generate the image (OpenAI may modify your prompt for safety or clarity)
1868
+ - `created` _number_ - The Unix timestamp (in seconds) of when the image was created
1869
+ - `size` _string_ - The size of the generated image. One of `1024x1024`, `1024x1536`, or `1536x1024`
1870
+ - `quality` _string_ - The quality of the generated image. One of `low`, `medium`, or `high`
1871
+ - `background` _string_ - The background parameter used for the image generation. Either `transparent` or `opaque`
1872
+ - `outputFormat` _string_ - The output format of the generated image. One of `png`, `webp`, or `jpeg`
1873
+
1874
+ For more information on the available OpenAI image model options, see the [OpenAI API reference](https://platform.openai.com/docs/api-reference/images/create).
1875
+
1876
+ ## Transcription Models
1877
+
1878
+ You can create models that call the [OpenAI transcription API](https://platform.openai.com/docs/api-reference/audio/transcribe)
1879
+ using the `.transcription()` factory method.
1880
+
1881
+ The first argument is the model id e.g. `whisper-1`.
1882
+
1883
+ ```ts
1884
+ const model = openai.transcription('whisper-1');
1885
+ ```
1886
+
1887
+ You can also pass additional provider-specific options using the `providerOptions` argument. For example, supplying the input language in ISO-639-1 (e.g. `en`) format will improve accuracy and latency.
1888
+
1889
+ ```ts highlight="6"
1890
+ import { experimental_transcribe as transcribe } from 'ai';
1891
+ import { openai } from '@ai-sdk/openai';
1892
+
1893
+ const result = await transcribe({
1894
+ model: openai.transcription('whisper-1'),
1895
+ audio: new Uint8Array([1, 2, 3, 4]),
1896
+ providerOptions: { openai: { language: 'en' } },
1897
+ });
1898
+ ```
1899
+
1900
+ To get word-level timestamps, specify the granularity:
1901
+
1902
+ ```ts highlight="8-9"
1903
+ import { experimental_transcribe as transcribe } from 'ai';
1904
+ import { openai } from '@ai-sdk/openai';
1905
+
1906
+ const result = await transcribe({
1907
+ model: openai.transcription('whisper-1'),
1908
+ audio: new Uint8Array([1, 2, 3, 4]),
1909
+ providerOptions: {
1910
+ openai: {
1911
+ //timestampGranularities: ['word'],
1912
+ timestampGranularities: ['segment'],
1913
+ },
1914
+ },
1915
+ });
1916
+
1917
+ // Access word-level timestamps
1918
+ console.log(result.segments); // Array of segments with startSecond/endSecond
1919
+ ```
1920
+
1921
+ The following provider options are available:
1922
+
1923
+ - **timestampGranularities** _string[]_
1924
+ The granularity of the timestamps in the transcription.
1925
+ Defaults to `['segment']`.
1926
+ Possible values are `['word']`, `['segment']`, and `['word', 'segment']`.
1927
+ Note: There is no additional latency for segment timestamps, but generating word timestamps incurs additional latency.
1928
+
1929
+ - **language** _string_
1930
+ The language of the input audio. Supplying the input language in ISO-639-1 format (e.g. 'en') will improve accuracy and latency.
1931
+ Optional.
1932
+
1933
+ - **prompt** _string_
1934
+ An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language.
1935
+ Optional.
1936
+
1937
+ - **temperature** _number_
1938
+ The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit.
1939
+ Defaults to 0.
1940
+ Optional.
1941
+
1942
+ - **include** _string[]_
1943
+ Additional information to include in the transcription response.
1944
+
1945
+ ### Model Capabilities
1946
+
1947
+ | Model | Transcription | Duration | Segments | Language |
1948
+ | ------------------------ | ------------------- | ------------------- | ------------------- | ------------------- |
1949
+ | `whisper-1` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
1950
+ | `gpt-4o-mini-transcribe` | <Check size={18} /> | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> |
1951
+ | `gpt-4o-transcribe` | <Check size={18} /> | <Cross size={18} /> | <Cross size={18} /> | <Cross size={18} /> |
1952
+
1953
+ ## Speech Models
1954
+
1955
+ You can create models that call the [OpenAI speech API](https://platform.openai.com/docs/api-reference/audio/speech)
1956
+ using the `.speech()` factory method.
1957
+
1958
+ The first argument is the model id e.g. `tts-1`.
1959
+
1960
+ ```ts
1961
+ const model = openai.speech('tts-1');
1962
+ ```
1963
+
1964
+ The `voice` argument can be set to one of OpenAI's available voices: `alloy`, `ash`, `coral`, `echo`, `fable`, `onyx`, `nova`, `sage`, or `shimmer`.
1965
+
1966
+ ```ts highlight="6"
1967
+ import { experimental_generateSpeech as generateSpeech } from 'ai';
1968
+ import { openai } from '@ai-sdk/openai';
1969
+
1970
+ const result = await generateSpeech({
1971
+ model: openai.speech('tts-1'),
1972
+ text: 'Hello, world!',
1973
+ voice: 'alloy', // OpenAI voice ID
1974
+ });
1975
+ ```
1976
+
1977
+ You can also pass additional provider-specific options using the `providerOptions` argument:
1978
+
1979
+ ```ts highlight="7-9"
1980
+ import { experimental_generateSpeech as generateSpeech } from 'ai';
1981
+ import { openai } from '@ai-sdk/openai';
1982
+
1983
+ const result = await generateSpeech({
1984
+ model: openai.speech('tts-1'),
1985
+ text: 'Hello, world!',
1986
+ voice: 'alloy',
1987
+ providerOptions: {
1988
+ openai: {
1989
+ speed: 1.2,
1990
+ },
1991
+ },
1992
+ });
1993
+ ```
1994
+
1995
+ - **instructions** _string_
1996
+ Control the voice of your generated audio with additional instructions e.g. "Speak in a slow and steady tone".
1997
+ Does not work with `tts-1` or `tts-1-hd`.
1998
+ Optional.
1999
+
2000
+ - **response_format** _string_
2001
+ The format to audio in.
2002
+ Supported formats are `mp3`, `opus`, `aac`, `flac`, `wav`, and `pcm`.
2003
+ Defaults to `mp3`.
2004
+ Optional.
2005
+
2006
+ - **speed** _number_
2007
+ The speed of the generated audio.
2008
+ Select a value from 0.25 to 4.0.
2009
+ Defaults to 1.0.
2010
+ Optional.
2011
+
2012
+ ### Model Capabilities
2013
+
2014
+ | Model | Instructions |
2015
+ | ----------------- | ------------------- |
2016
+ | `tts-1` | <Check size={18} /> |
2017
+ | `tts-1-hd` | <Check size={18} /> |
2018
+ | `gpt-4o-mini-tts` | <Check size={18} /> |