@ai-sdk/openai 4.0.101 → 4.0.102

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.
@@ -1955,6 +1955,12 @@ const result = await generateText({
1955
1955
  });
1956
1956
  ```
1957
1957
 
1958
+ For non-streaming JSON output with `generateText`, when a response contains
1959
+ messages marked with `phase: 'final_answer'`, the SDK parses only the last such
1960
+ message into `result.output`. This prevents commentary or repeated final answers
1961
+ from being joined into invalid JSON. The returned `text`, `content`, response
1962
+ messages, and token usage still include the complete response as before.
1963
+
1958
1964
  #### Typed providerMetadata in Text Parts
1959
1965
 
1960
1966
  When using the OpenAI Responses API, the SDK attaches OpenAI-specific metadata to output parts via `providerMetadata`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/openai",
3
- "version": "4.0.101",
3
+ "version": "4.0.102",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -35,8 +35,8 @@
35
35
  }
36
36
  },
37
37
  "dependencies": {
38
- "@ai-sdk/provider": "4.0.33",
39
- "@ai-sdk/provider-utils": "5.0.67"
38
+ "@ai-sdk/provider": "4.0.34",
39
+ "@ai-sdk/provider-utils": "5.0.68"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@ai-sdk/test-server": "2.0.4",
@@ -926,6 +926,8 @@ export class OpenAIResponsesLanguageModel implements LanguageModelV4 {
926
926
  let hasFunctionCall = false;
927
927
  const hostedToolSearchCallIds: string[] = [];
928
928
 
929
+ let structuredOutputText: string | undefined;
930
+
929
931
  // map response content to content array
930
932
  for (const part of response.output) {
931
933
  switch (part.type) {
@@ -1086,6 +1088,17 @@ export class OpenAIResponsesLanguageModel implements LanguageModelV4 {
1086
1088
  }
1087
1089
 
1088
1090
  case 'message': {
1091
+ // Parse only the last final answer for JSON output, while preserving
1092
+ // every message and its metadata in the generated content.
1093
+ if (
1094
+ options.responseFormat?.type === 'json' &&
1095
+ part.phase === 'final_answer'
1096
+ ) {
1097
+ structuredOutputText = part.content
1098
+ .map(contentPart => contentPart.text)
1099
+ .join('');
1100
+ }
1101
+
1089
1102
  for (const contentPart of part.content) {
1090
1103
  if (
1091
1104
  options.providerOptions?.[providerOptionsName]?.logprobs &&
@@ -1516,6 +1529,7 @@ export class OpenAIResponsesLanguageModel implements LanguageModelV4 {
1516
1529
 
1517
1530
  return {
1518
1531
  content,
1532
+ ...(structuredOutputText !== undefined && { structuredOutputText }),
1519
1533
  finishReason: {
1520
1534
  unified: mapOpenAIResponseFinishReason({
1521
1535
  finishReason: response.incomplete_details?.reason,