@ai-sdk/openai 4.0.2 → 4.0.3

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.
@@ -54,6 +54,10 @@ You can use the following optional settings to customize the OpenAI provider ins
54
54
 
55
55
  Use a different URL prefix for API calls, e.g. to use proxy servers.
56
56
  The default prefix is `https://api.openai.com/v1`.
57
+ The default OpenAI model factory (`openai('model-id')`) uses the Responses
58
+ API. If your custom base URL only supports the Chat Completions API, create
59
+ chat models with `openai.chat('model-id')` instead, or use the
60
+ [OpenAI-compatible provider](/providers/openai-compatible-providers).
57
61
 
58
62
  - **apiKey** _string_
59
63
 
@@ -1721,6 +1725,8 @@ The metadata includes the following fields:
1721
1725
  You can create models that call the [OpenAI chat API](https://platform.openai.com/docs/api-reference/chat) using the `.chat()` factory method.
1722
1726
  The first argument is the model id, e.g. `gpt-4`.
1723
1727
  The OpenAI chat models support tool calls and some have multi-modal capabilities.
1728
+ Use this factory when a custom `baseURL` points at an OpenAI-compatible endpoint
1729
+ that implements Chat Completions but not the Responses API.
1724
1730
 
1725
1731
  ```ts
1726
1732
  const model = openai.chat('gpt-5');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/openai",
3
- "version": "4.0.2",
3
+ "version": "4.0.3",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -1199,8 +1199,18 @@ export class OpenAIResponsesLanguageModel implements LanguageModelV4 {
1199
1199
 
1200
1200
  // handle failed chunk parsing / validation:
1201
1201
  if (!chunk.success) {
1202
+ const error = isOpenAIChatCompletionChunk(chunk.rawValue)
1203
+ ? createOpenAIResponsesChatCompletionsMismatchError({
1204
+ value: chunk.rawValue,
1205
+ cause: chunk.error,
1206
+ url,
1207
+ requestBodyValues: body,
1208
+ responseHeaders,
1209
+ })
1210
+ : chunk.error;
1211
+
1202
1212
  finishReason = { unified: 'error', raw: undefined };
1203
- controller.enqueue({ type: 'error', error: chunk.error });
1213
+ controller.enqueue({ type: 'error', error });
1204
1214
  return;
1205
1215
  }
1206
1216
 
@@ -2235,6 +2245,50 @@ function isTextDeltaChunk(
2235
2245
  return chunk.type === 'response.output_text.delta';
2236
2246
  }
2237
2247
 
2248
+ function isOpenAIChatCompletionChunk(value: unknown): boolean {
2249
+ const chunk = asRecord(value);
2250
+
2251
+ return (
2252
+ chunk != null &&
2253
+ Array.isArray(chunk.choices) &&
2254
+ typeof chunk.type !== 'string'
2255
+ );
2256
+ }
2257
+
2258
+ function createOpenAIResponsesChatCompletionsMismatchError({
2259
+ value,
2260
+ cause,
2261
+ url,
2262
+ requestBodyValues,
2263
+ responseHeaders,
2264
+ }: {
2265
+ value: unknown;
2266
+ cause: unknown;
2267
+ url: string;
2268
+ requestBodyValues: unknown;
2269
+ responseHeaders?: Record<string, string>;
2270
+ }): APICallError {
2271
+ return new APICallError({
2272
+ message:
2273
+ 'Received a Chat Completions stream while using the OpenAI Responses API. ' +
2274
+ "The default OpenAI provider model uses the Responses API. If your custom baseURL targets a Chat Completions-compatible endpoint, use openai.chat('model-id') or createOpenAI(...).chat('model-id') instead. " +
2275
+ 'You can also use @ai-sdk/openai-compatible for OpenAI-compatible providers.',
2276
+ url,
2277
+ requestBodyValues,
2278
+ responseHeaders,
2279
+ responseBody: JSON.stringify(value),
2280
+ cause,
2281
+ data: value,
2282
+ isRetryable: false,
2283
+ });
2284
+ }
2285
+
2286
+ function asRecord(value: unknown): Record<string, unknown> | undefined {
2287
+ return typeof value === 'object' && value != null
2288
+ ? (value as Record<string, unknown>)
2289
+ : undefined;
2290
+ }
2291
+
2238
2292
  function isResponseOutputItemDoneChunk(
2239
2293
  chunk: OpenAIResponsesChunk,
2240
2294
  ): chunk is OpenAIResponsesChunk & { type: 'response.output_item.done' } {