@ai-sdk/openai-compatible 1.0.60 → 1.0.62

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,34 @@
1
1
  # @ai-sdk/openai-compatible
2
2
 
3
+ ## 1.0.62
4
+
5
+ ### Patch Changes
6
+
7
+ - db18e4f: 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
+ - ce27f15: 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
+ - Updated dependencies [49c554d]
19
+ - Updated dependencies [b753c03]
20
+ - @ai-sdk/provider-utils@3.0.45
21
+
22
+ ## 1.0.61
23
+
24
+ ### Patch Changes
25
+
26
+ - 0a069bd: fix(openai-compatible): preserve string tool call arguments and safely serialize BigInt and circular structures in tool calls and outputs
27
+ - Updated dependencies [0a069bd]
28
+ - Updated dependencies [0a069bd]
29
+ - Updated dependencies [0a069bd]
30
+ - @ai-sdk/provider-utils@3.0.44
31
+
3
32
  ## 1.0.60
4
33
 
5
34
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { SharedV2ProviderMetadata, LanguageModelV2, LanguageModelV2Usage, EmbeddingModelV2, ImageModelV2, ProviderV2 } from '@ai-sdk/provider';
1
+ import { SharedV2ProviderMetadata, LanguageModelV2, LanguageModelV2CallWarning, LanguageModelV2Usage, EmbeddingModelV2, ImageModelV2, ProviderV2 } 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 { z, ZodType } from 'zod/v4';
@@ -92,8 +92,9 @@ type OpenAICompatibleChatConfig = {
92
92
  * Optional function to transform the request body before sending it to the API.
93
93
  * This is useful for proxy providers that may require a different request format
94
94
  * than the official OpenAI API.
95
+ * The optional warnings array can be used to report request transformations.
95
96
  */
96
- transformRequestBody?: (args: Record<string, any>) => Record<string, any>;
97
+ transformRequestBody?: (args: Record<string, any>, warnings?: LanguageModelV2CallWarning[]) => Record<string, any>;
97
98
  /**
98
99
  * Optional usage converter for OpenAI-compatible providers with different
99
100
  * token accounting semantics.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { SharedV2ProviderMetadata, LanguageModelV2, LanguageModelV2Usage, EmbeddingModelV2, ImageModelV2, ProviderV2 } from '@ai-sdk/provider';
1
+ import { SharedV2ProviderMetadata, LanguageModelV2, LanguageModelV2CallWarning, LanguageModelV2Usage, EmbeddingModelV2, ImageModelV2, ProviderV2 } 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 { z, ZodType } from 'zod/v4';
@@ -92,8 +92,9 @@ type OpenAICompatibleChatConfig = {
92
92
  * Optional function to transform the request body before sending it to the API.
93
93
  * This is useful for proxy providers that may require a different request format
94
94
  * than the official OpenAI API.
95
+ * The optional warnings array can be used to report request transformations.
95
96
  */
96
- transformRequestBody?: (args: Record<string, any>) => Record<string, any>;
97
+ transformRequestBody?: (args: Record<string, any>, warnings?: LanguageModelV2CallWarning[]) => Record<string, any>;
97
98
  /**
98
99
  * Optional usage converter for OpenAI-compatible providers with different
99
100
  * token accounting semantics.
package/dist/index.js CHANGED
@@ -110,7 +110,7 @@ function convertToOpenAICompatibleChatMessages(prompt) {
110
110
  type: "function",
111
111
  function: {
112
112
  name: part.toolName,
113
- arguments: JSON.stringify(part.input)
113
+ arguments: serializeToolCallArguments(part.input)
114
114
  },
115
115
  ...partMetadata
116
116
  });
@@ -139,7 +139,7 @@ function convertToOpenAICompatibleChatMessages(prompt) {
139
139
  case "content":
140
140
  case "json":
141
141
  case "error-json":
142
- contentValue = JSON.stringify(output.value);
142
+ contentValue = (0, import_provider_utils.stringifyWithBigIntAndCycles)(output.value);
143
143
  break;
144
144
  }
145
145
  const toolResponseMetadata = getOpenAIMetadata(toolResponse);
@@ -160,6 +160,17 @@ function convertToOpenAICompatibleChatMessages(prompt) {
160
160
  }
161
161
  return messages;
162
162
  }
163
+ function serializeToolCallArguments(input) {
164
+ if (typeof input === "string") {
165
+ try {
166
+ (0, import_provider_utils.secureJsonParse)(input);
167
+ return input;
168
+ } catch (e) {
169
+ return JSON.stringify(input);
170
+ }
171
+ }
172
+ return (0, import_provider_utils.stringifyWithBigIntAndCycles)(input);
173
+ }
163
174
 
164
175
  // src/chat/get-response-metadata.ts
165
176
  function getResponseMetadata({
@@ -175,10 +186,10 @@ function getResponseMetadata({
175
186
  }
176
187
 
177
188
  // src/chat/map-openai-compatible-finish-reason.ts
178
- function mapOpenAICompatibleFinishReason(finishReason) {
189
+ function mapOpenAICompatibleFinishReason(finishReason, { hasRefusal = false } = {}) {
179
190
  switch (finishReason) {
180
191
  case "stop":
181
- return "stop";
192
+ return hasRefusal ? "content-filter" : "stop";
182
193
  case "length":
183
194
  return "length";
184
195
  case "content_filter":
@@ -305,9 +316,9 @@ var OpenAICompatibleChatLanguageModel = class {
305
316
  var _a, _b, _c;
306
317
  return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
307
318
  }
308
- transformRequestBody(args) {
319
+ transformRequestBody(args, warnings) {
309
320
  var _a, _b, _c;
310
- return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
321
+ return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args, warnings)) != null ? _c : args;
311
322
  }
312
323
  convertUsage(usage) {
313
324
  var _a, _b, _c;
@@ -403,7 +414,7 @@ var OpenAICompatibleChatLanguageModel = class {
403
414
  async doGenerate(options) {
404
415
  var _a, _b, _c, _d, _e;
405
416
  const { args, warnings } = await this.getArgs({ ...options });
406
- const transformedArgs = this.transformRequestBody(args);
417
+ const transformedArgs = this.transformRequestBody(args, warnings);
407
418
  const body = JSON.stringify(transformedArgs);
408
419
  const {
409
420
  responseHeaders,
@@ -435,6 +446,11 @@ var OpenAICompatibleChatLanguageModel = class {
435
446
  if (text != null && text.length > 0) {
436
447
  content.push({ type: "text", text });
437
448
  }
449
+ const refusal = choice.message.refusal;
450
+ const hasRefusal = refusal != null && refusal.length > 0;
451
+ if (hasRefusal) {
452
+ content.push({ type: "text", text: refusal });
453
+ }
438
454
  const reasoning = (_a = choice.message.reasoning_content) != null ? _a : choice.message.reasoning;
439
455
  if (reasoning != null && reasoning.length > 0) {
440
456
  content.push({
@@ -467,7 +483,9 @@ var OpenAICompatibleChatLanguageModel = class {
467
483
  }
468
484
  return {
469
485
  content,
470
- finishReason: mapOpenAICompatibleFinishReason(choice.finish_reason),
486
+ finishReason: mapOpenAICompatibleFinishReason(choice.finish_reason, {
487
+ hasRefusal
488
+ }),
471
489
  usage: this.convertUsage(responseBody.usage),
472
490
  providerMetadata,
473
491
  request: { body },
@@ -482,12 +500,15 @@ var OpenAICompatibleChatLanguageModel = class {
482
500
  async doStream(options) {
483
501
  var _a;
484
502
  const { args, warnings } = await this.getArgs({ ...options });
485
- const body = this.transformRequestBody({
486
- ...args,
487
- stream: true,
488
- // only include stream_options when in strict compatibility mode:
489
- stream_options: this.config.includeUsage ? { include_usage: true } : void 0
490
- });
503
+ const body = this.transformRequestBody(
504
+ {
505
+ ...args,
506
+ stream: true,
507
+ // only include stream_options when in strict compatibility mode:
508
+ stream_options: this.config.includeUsage ? { include_usage: true } : void 0
509
+ },
510
+ warnings
511
+ );
491
512
  const metadataExtractor = (_a = this.config.metadataExtractor) == null ? void 0 : _a.createStreamExtractor();
492
513
  const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
493
514
  url: this.config.url({
@@ -505,6 +526,8 @@ var OpenAICompatibleChatLanguageModel = class {
505
526
  });
506
527
  let toolCallTracker;
507
528
  let finishReason = "unknown";
529
+ let rawFinishReason = void 0;
530
+ let hasRefusal = false;
508
531
  const usage = {
509
532
  completionTokens: void 0,
510
533
  completionTokensDetails: {
@@ -581,9 +604,7 @@ var OpenAICompatibleChatLanguageModel = class {
581
604
  }
582
605
  }
583
606
  if ((choice == null ? void 0 : choice.finish_reason) != null) {
584
- finishReason = mapOpenAICompatibleFinishReason(
585
- choice.finish_reason
586
- );
607
+ rawFinishReason = choice.finish_reason;
587
608
  }
588
609
  if ((choice == null ? void 0 : choice.delta) == null) {
589
610
  return;
@@ -615,6 +636,19 @@ var OpenAICompatibleChatLanguageModel = class {
615
636
  delta: delta.content
616
637
  });
617
638
  }
639
+ const refusal = delta.refusal;
640
+ if (refusal != null && refusal.length > 0) {
641
+ hasRefusal = true;
642
+ if (!isActiveText) {
643
+ controller.enqueue({ type: "text-start", id: "txt-0" });
644
+ isActiveText = true;
645
+ }
646
+ controller.enqueue({
647
+ type: "text-delta",
648
+ id: "txt-0",
649
+ delta: refusal
650
+ });
651
+ }
618
652
  if (delta.tool_calls != null) {
619
653
  for (const [
620
654
  fallbackIndex,
@@ -635,6 +669,11 @@ var OpenAICompatibleChatLanguageModel = class {
635
669
  controller.enqueue({ type: "text-end", id: "txt-0" });
636
670
  }
637
671
  toolCallTracker.flush();
672
+ if (rawFinishReason != null) {
673
+ finishReason = mapOpenAICompatibleFinishReason(rawFinishReason, {
674
+ hasRefusal
675
+ });
676
+ }
638
677
  const providerMetadata = {
639
678
  [providerOptionsName]: {},
640
679
  ...metadataExtractor == null ? void 0 : metadataExtractor.buildMetadata()
@@ -693,6 +732,12 @@ var OpenAICompatibleChatResponseSchema = (0, import_provider_utils2.lazySchema)(
693
732
  message: import_v43.z.object({
694
733
  role: import_v43.z.literal("assistant").nullish(),
695
734
  content: import_v43.z.string().nullish(),
735
+ // set instead of `content` when the model refuses to answer. Some
736
+ // OpenAI-compatible servers send non-string values (e.g. `false`),
737
+ // which are ignored instead of failing the whole response:
738
+ refusal: import_v43.z.unknown().nullish().transform(
739
+ (value) => typeof value === "string" ? value : void 0
740
+ ),
696
741
  reasoning_content: import_v43.z.string().nullish(),
697
742
  reasoning: import_v43.z.string().nullish(),
698
743
  tool_calls: import_v43.z.array(
@@ -722,6 +767,12 @@ var createOpenAICompatibleChatChunkSchema = (errorSchema) => import_v43.z.union(
722
767
  delta: import_v43.z.object({
723
768
  role: import_v43.z.enum(["assistant"]).nullish(),
724
769
  content: import_v43.z.string().nullish(),
770
+ // set instead of `content` when the model refuses to answer. Some
771
+ // OpenAI-compatible servers send non-string values (e.g. `false`),
772
+ // which are ignored instead of failing the whole response:
773
+ refusal: import_v43.z.unknown().nullish().transform(
774
+ (value) => typeof value === "string" ? value : void 0
775
+ ),
725
776
  // Most openai-compatible models set `reasoning_content`, but some
726
777
  // providers serving `gpt-oss` set `reasoning`. See #7866
727
778
  reasoning_content: import_v43.z.string().nullish(),
@@ -1358,7 +1409,7 @@ var openaiCompatibleImageResponseSchema = (0, import_provider_utils7.lazySchema)
1358
1409
  var import_provider_utils8 = require("@ai-sdk/provider-utils");
1359
1410
 
1360
1411
  // src/version.ts
1361
- var VERSION = true ? "1.0.60" : "0.0.0-test";
1412
+ var VERSION = true ? "1.0.62" : "0.0.0-test";
1362
1413
 
1363
1414
  // src/openai-compatible-provider.ts
1364
1415
  function createOpenAICompatible(options) {