@ai-sdk/openai 2.0.0-beta.4 → 2.0.0-beta.6

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.
package/dist/index.mjs CHANGED
@@ -294,7 +294,7 @@ var openaiProviderOptions = z.object({
294
294
  /**
295
295
  * Whether to use strict JSON schema validation.
296
296
  *
297
- * @default true
297
+ * @default false
298
298
  */
299
299
  strictJsonSchema: z.boolean().optional()
300
300
  });
@@ -1883,6 +1883,9 @@ var openaiTranscriptionResponseSchema = z12.object({
1883
1883
  });
1884
1884
 
1885
1885
  // src/responses/openai-responses-language-model.ts
1886
+ import {
1887
+ APICallError
1888
+ } from "@ai-sdk/provider";
1886
1889
  import {
1887
1890
  combineHeaders as combineHeaders6,
1888
1891
  createEventSourceResponseHandler as createEventSourceResponseHandler3,
@@ -2016,7 +2019,7 @@ async function convertToOpenAIResponsesMessages({
2016
2019
  const summaryParts = [];
2017
2020
  if (part.text.length > 0) {
2018
2021
  summaryParts.push({ type: "summary_text", text: part.text });
2019
- } else {
2022
+ } else if (existingReasoningMessage !== void 0) {
2020
2023
  warnings.push({
2021
2024
  type: "other",
2022
2025
  message: `Cannot append empty reasoning part to existing reasoning sequence. Skipping reasoning part: ${JSON.stringify(part)}.`
@@ -2336,15 +2339,16 @@ var OpenAIResponsesLanguageModel = class {
2336
2339
  async doGenerate(options) {
2337
2340
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
2338
2341
  const { args: body, warnings } = await this.getArgs(options);
2342
+ const url = this.config.url({
2343
+ path: "/responses",
2344
+ modelId: this.modelId
2345
+ });
2339
2346
  const {
2340
2347
  responseHeaders,
2341
2348
  value: response,
2342
2349
  rawValue: rawResponse
2343
2350
  } = await postJsonToApi5({
2344
- url: this.config.url({
2345
- path: "/responses",
2346
- modelId: this.modelId
2347
- }),
2351
+ url,
2348
2352
  headers: combineHeaders6(this.config.headers(), options.headers),
2349
2353
  body,
2350
2354
  failedResponseHandler: openaiFailedResponseHandler,
@@ -2352,6 +2356,10 @@ var OpenAIResponsesLanguageModel = class {
2352
2356
  z14.object({
2353
2357
  id: z14.string(),
2354
2358
  created_at: z14.number(),
2359
+ error: z14.object({
2360
+ code: z14.string(),
2361
+ message: z14.string()
2362
+ }).nullish(),
2355
2363
  model: z14.string(),
2356
2364
  output: z14.array(
2357
2365
  z14.discriminatedUnion("type", [
@@ -2410,6 +2418,17 @@ var OpenAIResponsesLanguageModel = class {
2410
2418
  abortSignal: options.abortSignal,
2411
2419
  fetch: this.config.fetch
2412
2420
  });
2421
+ if (response.error) {
2422
+ throw new APICallError({
2423
+ message: response.error.message,
2424
+ url,
2425
+ requestBodyValues: body,
2426
+ statusCode: 400,
2427
+ responseHeaders,
2428
+ responseBody: rawResponse,
2429
+ isRetryable: false
2430
+ });
2431
+ }
2413
2432
  const content = [];
2414
2433
  for (const part of response.output) {
2415
2434
  switch (part.type) {
@@ -2557,6 +2576,7 @@ var OpenAIResponsesLanguageModel = class {
2557
2576
  let responseId = null;
2558
2577
  const ongoingToolCalls = {};
2559
2578
  let hasToolCalls = false;
2579
+ const activeReasoning = {};
2560
2580
  return {
2561
2581
  stream: response.pipeThrough(
2562
2582
  new TransformStream({
@@ -2564,7 +2584,7 @@ var OpenAIResponsesLanguageModel = class {
2564
2584
  controller.enqueue({ type: "stream-start", warnings });
2565
2585
  },
2566
2586
  transform(chunk, controller) {
2567
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
2587
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
2568
2588
  if (options.includeRawChunks) {
2569
2589
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
2570
2590
  }
@@ -2610,10 +2630,14 @@ var OpenAIResponsesLanguageModel = class {
2610
2630
  type: "text-start",
2611
2631
  id: value.item.id
2612
2632
  });
2613
- } else if (value.item.type === "reasoning") {
2633
+ } else if (isResponseOutputItemAddedReasoningChunk(value)) {
2634
+ activeReasoning[value.item.id] = {
2635
+ encryptedContent: value.item.encrypted_content,
2636
+ summaryParts: [0]
2637
+ };
2614
2638
  controller.enqueue({
2615
2639
  type: "reasoning-start",
2616
- id: value.item.id,
2640
+ id: `${value.item.id}:0`,
2617
2641
  providerMetadata: {
2618
2642
  openai: {
2619
2643
  reasoning: {
@@ -2691,19 +2715,23 @@ var OpenAIResponsesLanguageModel = class {
2691
2715
  type: "text-end",
2692
2716
  id: value.item.id
2693
2717
  });
2694
- } else if (value.item.type === "reasoning") {
2695
- controller.enqueue({
2696
- type: "reasoning-end",
2697
- id: value.item.id,
2698
- providerMetadata: {
2699
- openai: {
2700
- reasoning: {
2701
- id: value.item.id,
2702
- encryptedContent: (_b = value.item.encrypted_content) != null ? _b : null
2718
+ } else if (isResponseOutputItemDoneReasoningChunk(value)) {
2719
+ const activeReasoningPart = activeReasoning[value.item.id];
2720
+ for (const summaryIndex of activeReasoningPart.summaryParts) {
2721
+ controller.enqueue({
2722
+ type: "reasoning-end",
2723
+ id: `${value.item.id}:${summaryIndex}`,
2724
+ providerMetadata: {
2725
+ openai: {
2726
+ reasoning: {
2727
+ id: value.item.id,
2728
+ encryptedContent: (_b = value.item.encrypted_content) != null ? _b : null
2729
+ }
2703
2730
  }
2704
2731
  }
2705
- }
2706
- });
2732
+ });
2733
+ }
2734
+ delete activeReasoning[value.item.id];
2707
2735
  }
2708
2736
  } else if (isResponseFunctionCallArgumentsDeltaChunk(value)) {
2709
2737
  const toolCall = ongoingToolCalls[value.output_index];
@@ -2728,30 +2756,57 @@ var OpenAIResponsesLanguageModel = class {
2728
2756
  id: value.item_id,
2729
2757
  delta: value.delta
2730
2758
  });
2759
+ } else if (isResponseReasoningSummaryPartAddedChunk(value)) {
2760
+ if (value.summary_index > 0) {
2761
+ (_c = activeReasoning[value.item_id]) == null ? void 0 : _c.summaryParts.push(
2762
+ value.summary_index
2763
+ );
2764
+ controller.enqueue({
2765
+ type: "reasoning-start",
2766
+ id: `${value.item_id}:${value.summary_index}`,
2767
+ providerMetadata: {
2768
+ openai: {
2769
+ reasoning: {
2770
+ id: value.item_id,
2771
+ encryptedContent: (_e = (_d = activeReasoning[value.item_id]) == null ? void 0 : _d.encryptedContent) != null ? _e : null
2772
+ }
2773
+ }
2774
+ }
2775
+ });
2776
+ }
2731
2777
  } else if (isResponseReasoningSummaryTextDeltaChunk(value)) {
2732
2778
  controller.enqueue({
2733
2779
  type: "reasoning-delta",
2734
- id: value.item_id,
2735
- delta: value.delta
2780
+ id: `${value.item_id}:${value.summary_index}`,
2781
+ delta: value.delta,
2782
+ providerMetadata: {
2783
+ openai: {
2784
+ reasoning: {
2785
+ id: value.item_id
2786
+ }
2787
+ }
2788
+ }
2736
2789
  });
2737
2790
  } else if (isResponseFinishedChunk(value)) {
2738
2791
  finishReason = mapOpenAIResponseFinishReason({
2739
- finishReason: (_c = value.response.incomplete_details) == null ? void 0 : _c.reason,
2792
+ finishReason: (_f = value.response.incomplete_details) == null ? void 0 : _f.reason,
2740
2793
  hasToolCalls
2741
2794
  });
2742
2795
  usage.inputTokens = value.response.usage.input_tokens;
2743
2796
  usage.outputTokens = value.response.usage.output_tokens;
2744
2797
  usage.totalTokens = value.response.usage.input_tokens + value.response.usage.output_tokens;
2745
- usage.reasoningTokens = (_e = (_d = value.response.usage.output_tokens_details) == null ? void 0 : _d.reasoning_tokens) != null ? _e : void 0;
2746
- usage.cachedInputTokens = (_g = (_f = value.response.usage.input_tokens_details) == null ? void 0 : _f.cached_tokens) != null ? _g : void 0;
2798
+ usage.reasoningTokens = (_h = (_g = value.response.usage.output_tokens_details) == null ? void 0 : _g.reasoning_tokens) != null ? _h : void 0;
2799
+ usage.cachedInputTokens = (_j = (_i = value.response.usage.input_tokens_details) == null ? void 0 : _i.cached_tokens) != null ? _j : void 0;
2747
2800
  } else if (isResponseAnnotationAddedChunk(value)) {
2748
2801
  controller.enqueue({
2749
2802
  type: "source",
2750
2803
  sourceType: "url",
2751
- id: (_j = (_i = (_h = self.config).generateId) == null ? void 0 : _i.call(_h)) != null ? _j : generateId2(),
2804
+ id: (_m = (_l = (_k = self.config).generateId) == null ? void 0 : _l.call(_k)) != null ? _m : generateId2(),
2752
2805
  url: value.annotation.url,
2753
2806
  title: value.annotation.title
2754
2807
  });
2808
+ } else if (isErrorChunk(value)) {
2809
+ controller.enqueue({ type: "error", error: value });
2755
2810
  }
2756
2811
  },
2757
2812
  flush(controller) {
@@ -2784,6 +2839,13 @@ var textDeltaChunkSchema = z14.object({
2784
2839
  item_id: z14.string(),
2785
2840
  delta: z14.string()
2786
2841
  });
2842
+ var errorChunkSchema = z14.object({
2843
+ type: z14.literal("error"),
2844
+ code: z14.string(),
2845
+ message: z14.string(),
2846
+ param: z14.string().nullish(),
2847
+ sequence_number: z14.number()
2848
+ });
2787
2849
  var responseFinishedChunkSchema = z14.object({
2788
2850
  type: z14.enum(["response.completed", "response.incomplete"]),
2789
2851
  response: z14.object({
@@ -2810,13 +2872,7 @@ var responseOutputItemAddedSchema = z14.object({
2810
2872
  z14.object({
2811
2873
  type: z14.literal("reasoning"),
2812
2874
  id: z14.string(),
2813
- encrypted_content: z14.string().nullish(),
2814
- summary: z14.array(
2815
- z14.object({
2816
- type: z14.literal("summary_text"),
2817
- text: z14.string()
2818
- })
2819
- )
2875
+ encrypted_content: z14.string().nullish()
2820
2876
  }),
2821
2877
  z14.object({
2822
2878
  type: z14.literal("function_call"),
@@ -2848,13 +2904,7 @@ var responseOutputItemDoneSchema = z14.object({
2848
2904
  z14.object({
2849
2905
  type: z14.literal("reasoning"),
2850
2906
  id: z14.string(),
2851
- encrypted_content: z14.string().nullish(),
2852
- summary: z14.array(
2853
- z14.object({
2854
- type: z14.literal("summary_text"),
2855
- text: z14.string()
2856
- })
2857
- )
2907
+ encrypted_content: z14.string().nullish()
2858
2908
  }),
2859
2909
  z14.object({
2860
2910
  type: z14.literal("function_call"),
@@ -2890,9 +2940,15 @@ var responseAnnotationAddedSchema = z14.object({
2890
2940
  title: z14.string()
2891
2941
  })
2892
2942
  });
2943
+ var responseReasoningSummaryPartAddedSchema = z14.object({
2944
+ type: z14.literal("response.reasoning_summary_part.added"),
2945
+ item_id: z14.string(),
2946
+ summary_index: z14.number()
2947
+ });
2893
2948
  var responseReasoningSummaryTextDeltaSchema = z14.object({
2894
2949
  type: z14.literal("response.reasoning_summary_text.delta"),
2895
2950
  item_id: z14.string(),
2951
+ summary_index: z14.number(),
2896
2952
  delta: z14.string()
2897
2953
  });
2898
2954
  var openaiResponsesChunkSchema = z14.union([
@@ -2903,8 +2959,10 @@ var openaiResponsesChunkSchema = z14.union([
2903
2959
  responseOutputItemDoneSchema,
2904
2960
  responseFunctionCallArgumentsDeltaSchema,
2905
2961
  responseAnnotationAddedSchema,
2962
+ responseReasoningSummaryPartAddedSchema,
2906
2963
  responseReasoningSummaryTextDeltaSchema,
2907
- z14.object({ type: z14.string() }).passthrough()
2964
+ errorChunkSchema,
2965
+ z14.object({ type: z14.string() }).loose()
2908
2966
  // fallback for unknown chunks
2909
2967
  ]);
2910
2968
  function isTextDeltaChunk(chunk) {
@@ -2913,6 +2971,9 @@ function isTextDeltaChunk(chunk) {
2913
2971
  function isResponseOutputItemDoneChunk(chunk) {
2914
2972
  return chunk.type === "response.output_item.done";
2915
2973
  }
2974
+ function isResponseOutputItemDoneReasoningChunk(chunk) {
2975
+ return isResponseOutputItemDoneChunk(chunk) && chunk.item.type === "reasoning";
2976
+ }
2916
2977
  function isResponseFinishedChunk(chunk) {
2917
2978
  return chunk.type === "response.completed" || chunk.type === "response.incomplete";
2918
2979
  }
@@ -2925,12 +2986,21 @@ function isResponseFunctionCallArgumentsDeltaChunk(chunk) {
2925
2986
  function isResponseOutputItemAddedChunk(chunk) {
2926
2987
  return chunk.type === "response.output_item.added";
2927
2988
  }
2989
+ function isResponseOutputItemAddedReasoningChunk(chunk) {
2990
+ return isResponseOutputItemAddedChunk(chunk) && chunk.item.type === "reasoning";
2991
+ }
2928
2992
  function isResponseAnnotationAddedChunk(chunk) {
2929
2993
  return chunk.type === "response.output_text.annotation.added";
2930
2994
  }
2995
+ function isResponseReasoningSummaryPartAddedChunk(chunk) {
2996
+ return chunk.type === "response.reasoning_summary_part.added";
2997
+ }
2931
2998
  function isResponseReasoningSummaryTextDeltaChunk(chunk) {
2932
2999
  return chunk.type === "response.reasoning_summary_text.delta";
2933
3000
  }
3001
+ function isErrorChunk(chunk) {
3002
+ return chunk.type === "error";
3003
+ }
2934
3004
  function getResponsesModelConfig(modelId) {
2935
3005
  if (modelId.startsWith("o") || modelId.startsWith("codex-") || modelId.startsWith("computer-use")) {
2936
3006
  if (modelId.startsWith("o1-mini") || modelId.startsWith("o1-preview")) {