@ai-sdk/openai-compatible 2.0.89 → 2.0.91

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/CHANGELOG.md CHANGED
@@ -1,5 +1,30 @@
1
1
  # @ai-sdk/openai-compatible
2
2
 
3
+ ## 2.0.91
4
+
5
+ ### Patch Changes
6
+
7
+ - 14c196a: Avoid empty structured-output answers from Baseten's DeepSeek V4 Pro 0813 when reasoning is enabled. Fall back to JSON prompt instructions while keeping reasoning and SDK output validation, and report the fallback through request transformation warnings.
8
+ - 98eb47b: fix(openai,openai-compatible): surface refusal logprobs and openai-compatible refusals
9
+
10
+ `@ai-sdk/openai`: Chat Completions responses that contain a refusal can return `logprobs.refusal` next to `logprobs.content`. The provider previously read only `logprobs.content`, so `providerMetadata.openai.logprobs` was empty or missing for refusals. Refusal logprobs are now included in emission order (content logprobs followed by refusal logprobs) in both `generateText` and `streamText`.
11
+
12
+ Streaming logprobs are now collected across all chunks in `streamText`: previously only the logprobs of the last streamed chunk were reported. This applies to content logprobs as well, so `providerMetadata.openai.logprobs` now contains the logprobs of every streamed token instead of only the last chunk's tokens. Explicitly empty logprobs (`logprobs: { content: [], refusal: null }`) are reported as an empty array, as before.
13
+
14
+ A refusal that arrives in the same streamed chunk as the `finish_reason` is now taken into account: such a response is reported with a `content-filter` finish reason instead of `stop`.
15
+
16
+ `@ai-sdk/openai-compatible`: Chat Completions responses that carry `message.refusal` (non-streaming) or `delta.refusal` (streaming) were ignored, so providers such as OpenAI served through `createOpenAICompatible` lost the refusal text and reported an empty `stop` response. Refusals are now returned as text content after any content text, and a non-empty refusal with a `stop` finish reason is reported as a `content-filter` finish reason. This also applies when the refusal and the finish reason arrive in the same streamed chunk. Non-string refusals (for example `refusal: false` from servers that use booleans) are ignored instead of failing the response.
17
+
18
+ ## 2.0.90
19
+
20
+ ### Patch Changes
21
+
22
+ - d900e58: fix(openai-compatible): preserve string tool call arguments and safely serialize BigInt and circular structures in tool calls and outputs
23
+ - Updated dependencies [d900e58]
24
+ - Updated dependencies [d900e58]
25
+ - Updated dependencies [d900e58]
26
+ - @ai-sdk/provider-utils@4.0.64
27
+
3
28
  ## 2.0.89
4
29
 
5
30
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { SharedV3ProviderMetadata, LanguageModelV3, LanguageModelV3Usage, LanguageModelV3CallOptions, LanguageModelV3GenerateResult, LanguageModelV3StreamResult, EmbeddingModelV3, ImageModelV3, ProviderV3 } from '@ai-sdk/provider';
1
+ import { SharedV3ProviderMetadata, LanguageModelV3, SharedV3Warning, LanguageModelV3Usage, LanguageModelV3CallOptions, LanguageModelV3GenerateResult, LanguageModelV3StreamResult, EmbeddingModelV3, ImageModelV3, ProviderV3 } from '@ai-sdk/provider';
2
2
  import * as _ai_sdk_provider_utils from '@ai-sdk/provider-utils';
3
3
  import { FetchFunction, InferSchema } from '@ai-sdk/provider-utils';
4
4
  import { ZodType, z } from 'zod/v4';
@@ -93,8 +93,9 @@ type OpenAICompatibleChatConfig = {
93
93
  * Optional function to transform the request body before sending it to the API.
94
94
  * This is useful for proxy providers that may require a different request format
95
95
  * than the official OpenAI API.
96
+ * The optional warnings array can be used to report request transformations.
96
97
  */
97
- transformRequestBody?: (args: Record<string, any>) => Record<string, any>;
98
+ transformRequestBody?: (args: Record<string, any>, warnings?: SharedV3Warning[]) => Record<string, any>;
98
99
  /**
99
100
  * Optional usage converter for OpenAI-compatible providers with different
100
101
  * token accounting semantics.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { SharedV3ProviderMetadata, LanguageModelV3, LanguageModelV3Usage, LanguageModelV3CallOptions, LanguageModelV3GenerateResult, LanguageModelV3StreamResult, EmbeddingModelV3, ImageModelV3, ProviderV3 } from '@ai-sdk/provider';
1
+ import { SharedV3ProviderMetadata, LanguageModelV3, SharedV3Warning, LanguageModelV3Usage, LanguageModelV3CallOptions, LanguageModelV3GenerateResult, LanguageModelV3StreamResult, EmbeddingModelV3, ImageModelV3, ProviderV3 } from '@ai-sdk/provider';
2
2
  import * as _ai_sdk_provider_utils from '@ai-sdk/provider-utils';
3
3
  import { FetchFunction, InferSchema } from '@ai-sdk/provider-utils';
4
4
  import { ZodType, z } from 'zod/v4';
@@ -93,8 +93,9 @@ type OpenAICompatibleChatConfig = {
93
93
  * Optional function to transform the request body before sending it to the API.
94
94
  * This is useful for proxy providers that may require a different request format
95
95
  * than the official OpenAI API.
96
+ * The optional warnings array can be used to report request transformations.
96
97
  */
97
- transformRequestBody?: (args: Record<string, any>) => Record<string, any>;
98
+ transformRequestBody?: (args: Record<string, any>, warnings?: SharedV3Warning[]) => Record<string, any>;
98
99
  /**
99
100
  * Optional usage converter for OpenAI-compatible providers with different
100
101
  * token accounting semantics.
package/dist/index.js CHANGED
@@ -121,6 +121,17 @@ function getAudioFormat(mediaType) {
121
121
  return null;
122
122
  }
123
123
  }
124
+ function serializeToolCallArguments(input) {
125
+ if (typeof input === "string") {
126
+ try {
127
+ (0, import_provider_utils.secureJsonParse)(input);
128
+ return input;
129
+ } catch (e) {
130
+ return JSON.stringify(input);
131
+ }
132
+ }
133
+ return (0, import_provider_utils.stringifyWithBigIntAndCycles)(input);
134
+ }
124
135
  function convertToOpenAICompatibleChatMessages(prompt) {
125
136
  var _a, _b, _c;
126
137
  const messages = [];
@@ -238,7 +249,7 @@ function convertToOpenAICompatibleChatMessages(prompt) {
238
249
  type: "function",
239
250
  function: {
240
251
  name: part.toolName,
241
- arguments: JSON.stringify(part.input)
252
+ arguments: serializeToolCallArguments(part.input)
242
253
  },
243
254
  ...partMetadata,
244
255
  // Include extra_content for Google Gemini thought signatures
@@ -281,7 +292,7 @@ function convertToOpenAICompatibleChatMessages(prompt) {
281
292
  case "content":
282
293
  case "json":
283
294
  case "error-json":
284
- contentValue = JSON.stringify(output.value);
295
+ contentValue = (0, import_provider_utils.stringifyWithBigIntAndCycles)(output.value);
285
296
  break;
286
297
  }
287
298
  const toolResponseMetadata = getOpenAIMetadata(toolResponse);
@@ -317,10 +328,10 @@ function getResponseMetadata({
317
328
  }
318
329
 
319
330
  // src/chat/map-openai-compatible-finish-reason.ts
320
- function mapOpenAICompatibleFinishReason(finishReason) {
331
+ function mapOpenAICompatibleFinishReason(finishReason, { hasRefusal = false } = {}) {
321
332
  switch (finishReason) {
322
333
  case "stop":
323
- return "stop";
334
+ return hasRefusal ? "content-filter" : "stop";
324
335
  case "length":
325
336
  return "length";
326
337
  case "content_filter":
@@ -444,9 +455,9 @@ var OpenAICompatibleChatLanguageModel = class {
444
455
  var _a, _b, _c;
445
456
  return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
446
457
  }
447
- transformRequestBody(args) {
458
+ transformRequestBody(args, warnings) {
448
459
  var _a, _b, _c;
449
- return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
460
+ return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args, warnings)) != null ? _c : args;
450
461
  }
451
462
  convertUsage(usage) {
452
463
  var _a, _b, _c;
@@ -571,7 +582,7 @@ var OpenAICompatibleChatLanguageModel = class {
571
582
  async doGenerate(options) {
572
583
  var _a, _b, _c, _d, _e, _f, _g, _h;
573
584
  const { args, warnings, metadataKey } = await this.getArgs({ ...options });
574
- const transformedBody = this.transformRequestBody(args);
585
+ const transformedBody = this.transformRequestBody(args, warnings);
575
586
  const body = JSON.stringify(transformedBody);
576
587
  const {
577
588
  responseHeaders,
@@ -607,6 +618,11 @@ var OpenAICompatibleChatLanguageModel = class {
607
618
  });
608
619
  }
609
620
  content.push(...convertOpenAICompatibleContent(choice.message.content));
621
+ const refusal = choice.message.refusal;
622
+ const hasRefusal = refusal != null && refusal.length > 0;
623
+ if (hasRefusal) {
624
+ content.push({ type: "text", text: refusal });
625
+ }
610
626
  if (choice.message.tool_calls != null) {
611
627
  for (const toolCall of choice.message.tool_calls) {
612
628
  const thoughtSignature = (_c = (_b = toolCall.extra_content) == null ? void 0 : _b.google) == null ? void 0 : _c.thought_signature;
@@ -639,7 +655,9 @@ var OpenAICompatibleChatLanguageModel = class {
639
655
  return {
640
656
  content,
641
657
  finishReason: {
642
- unified: mapOpenAICompatibleFinishReason(choice.finish_reason),
658
+ unified: mapOpenAICompatibleFinishReason(choice.finish_reason, {
659
+ hasRefusal
660
+ }),
643
661
  raw: (_h = choice.finish_reason) != null ? _h : void 0
644
662
  },
645
663
  usage: this.convertUsage(responseBody.usage),
@@ -656,12 +674,15 @@ var OpenAICompatibleChatLanguageModel = class {
656
674
  async doStream(options) {
657
675
  var _a;
658
676
  const { args, warnings, metadataKey } = await this.getArgs({ ...options });
659
- const body = this.transformRequestBody({
660
- ...args,
661
- stream: true,
662
- // only include stream_options when in strict compatibility mode:
663
- stream_options: this.config.includeUsage ? { include_usage: true } : void 0
664
- });
677
+ const body = this.transformRequestBody(
678
+ {
679
+ ...args,
680
+ stream: true,
681
+ // only include stream_options when in strict compatibility mode:
682
+ stream_options: this.config.includeUsage ? { include_usage: true } : void 0
683
+ },
684
+ warnings
685
+ );
665
686
  const metadataExtractor = (_a = this.config.metadataExtractor) == null ? void 0 : _a.createStreamExtractor();
666
687
  const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
667
688
  url: this.config.url({
@@ -724,10 +745,12 @@ var OpenAICompatibleChatLanguageModel = class {
724
745
  }
725
746
  };
726
747
  let finishReason;
748
+ let rawFinishReason = void 0;
727
749
  let usage = void 0;
728
750
  let isFirstChunk = true;
729
751
  let isActiveReasoning = false;
730
752
  let isActiveText = false;
753
+ let hasRefusal = false;
731
754
  const convertUsage = (usage2) => this.convertUsage(usage2);
732
755
  return {
733
756
  stream: response.pipeThrough(
@@ -748,7 +771,7 @@ var OpenAICompatibleChatLanguageModel = class {
748
771
  controller.enqueue({ type: "stream-start", warnings });
749
772
  },
750
773
  transform(chunk, controller) {
751
- var _a2, _b;
774
+ var _a2;
752
775
  if (options.includeRawChunks) {
753
776
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
754
777
  }
@@ -779,10 +802,7 @@ var OpenAICompatibleChatLanguageModel = class {
779
802
  }
780
803
  const choice = value.choices[0];
781
804
  if ((choice == null ? void 0 : choice.finish_reason) != null) {
782
- finishReason = {
783
- unified: mapOpenAICompatibleFinishReason(choice.finish_reason),
784
- raw: (_a2 = choice.finish_reason) != null ? _a2 : void 0
785
- };
805
+ rawFinishReason = choice.finish_reason;
786
806
  }
787
807
  if ((choice == null ? void 0 : choice.delta) == null) {
788
808
  return;
@@ -824,7 +844,7 @@ var OpenAICompatibleChatLanguageModel = class {
824
844
  delta: textDelta
825
845
  });
826
846
  };
827
- const reasoningContent = (_b = delta.reasoning_content) != null ? _b : delta.reasoning;
847
+ const reasoningContent = (_a2 = delta.reasoning_content) != null ? _a2 : delta.reasoning;
828
848
  if (reasoningContent) {
829
849
  enqueueReasoningDelta(reasoningContent);
830
850
  }
@@ -837,6 +857,11 @@ var OpenAICompatibleChatLanguageModel = class {
837
857
  enqueueTextDelta(contentPart.text);
838
858
  }
839
859
  }
860
+ const refusal = delta.refusal;
861
+ if (refusal != null && refusal.length > 0) {
862
+ hasRefusal = true;
863
+ enqueueTextDelta(refusal);
864
+ }
840
865
  if (delta.tool_calls != null && delta.tool_calls.length > 0) {
841
866
  if (isActiveReasoning) {
842
867
  controller.enqueue({
@@ -867,6 +892,14 @@ var OpenAICompatibleChatLanguageModel = class {
867
892
  }
868
893
  pendingToolCalls.clear();
869
894
  toolCallTracker.flush();
895
+ if (rawFinishReason != null) {
896
+ finishReason = {
897
+ unified: mapOpenAICompatibleFinishReason(rawFinishReason, {
898
+ hasRefusal
899
+ }),
900
+ raw: rawFinishReason
901
+ };
902
+ }
870
903
  if (finishReason == null) {
871
904
  finishReason = { unified: "error", raw: void 0 };
872
905
  controller.enqueue({
@@ -957,6 +990,12 @@ var OpenAICompatibleChatResponseSchema = (0, import_provider_utils2.lazySchema)(
957
990
  message: import_v43.z.object({
958
991
  role: import_v43.z.literal("assistant").nullish(),
959
992
  content: openAICompatibleContentSchema(),
993
+ // set instead of `content` when the model refuses to answer. Some
994
+ // OpenAI-compatible servers send non-string values (e.g. `false`),
995
+ // which are ignored instead of failing the whole response:
996
+ refusal: import_v43.z.unknown().nullish().transform(
997
+ (value) => typeof value === "string" ? value : void 0
998
+ ),
960
999
  reasoning_content: import_v43.z.string().nullish(),
961
1000
  reasoning: import_v43.z.string().nullish(),
962
1001
  tool_calls: import_v43.z.array(
@@ -991,6 +1030,12 @@ var chunkBaseSchema = () => import_v43.z.looseObject({
991
1030
  delta: import_v43.z.object({
992
1031
  role: import_v43.z.enum(["assistant", ""]).nullish(),
993
1032
  content: openAICompatibleContentSchema(),
1033
+ // set instead of `content` when the model refuses to answer. Some
1034
+ // OpenAI-compatible servers send non-string values (e.g. `false`),
1035
+ // which are ignored instead of failing the whole response:
1036
+ refusal: import_v43.z.unknown().nullish().transform(
1037
+ (value) => typeof value === "string" ? value : void 0
1038
+ ),
994
1039
  // Most openai-compatible models set `reasoning_content`, but some
995
1040
  // providers serving `gpt-oss` set `reasoning`. See #7866
996
1041
  reasoning_content: import_v43.z.string().nullish(),
@@ -1785,7 +1830,7 @@ async function fileToBlob(file) {
1785
1830
  var import_provider_utils8 = require("@ai-sdk/provider-utils");
1786
1831
 
1787
1832
  // src/version.ts
1788
- var VERSION = true ? "2.0.89" : "0.0.0-test";
1833
+ var VERSION = true ? "2.0.91" : "0.0.0-test";
1789
1834
 
1790
1835
  // src/openai-compatible-provider.ts
1791
1836
  function createOpenAICompatible(options) {