@ai-sdk/openai-compatible 2.0.90 → 2.0.92
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 +22 -0
- package/dist/index.d.mts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +53 -19
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +53 -19
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +5 -2
- package/dist/internal/index.d.ts +5 -2
- package/dist/internal/index.js +2 -2
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +2 -2
- package/dist/internal/index.mjs.map +1 -1
- package/docs/index.mdx +22 -0
- package/package.json +2 -2
- package/src/chat/map-openai-compatible-finish-reason.ts +5 -1
- package/src/chat/openai-compatible-chat-language-model.ts +83 -17
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# @ai-sdk/openai-compatible
|
|
2
2
|
|
|
3
|
+
## 2.0.92
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [f0dee4b]
|
|
8
|
+
- @ai-sdk/provider-utils@4.0.65
|
|
9
|
+
|
|
10
|
+
## 2.0.91
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- 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.
|
|
15
|
+
- 98eb47b: fix(openai,openai-compatible): surface refusal logprobs and openai-compatible refusals
|
|
16
|
+
|
|
17
|
+
`@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`.
|
|
18
|
+
|
|
19
|
+
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.
|
|
20
|
+
|
|
21
|
+
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`.
|
|
22
|
+
|
|
23
|
+
`@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.
|
|
24
|
+
|
|
3
25
|
## 2.0.90
|
|
4
26
|
|
|
5
27
|
### 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
|
|
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
|
|
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
|
@@ -328,10 +328,10 @@ function getResponseMetadata({
|
|
|
328
328
|
}
|
|
329
329
|
|
|
330
330
|
// src/chat/map-openai-compatible-finish-reason.ts
|
|
331
|
-
function mapOpenAICompatibleFinishReason(finishReason) {
|
|
331
|
+
function mapOpenAICompatibleFinishReason(finishReason, { hasRefusal = false } = {}) {
|
|
332
332
|
switch (finishReason) {
|
|
333
333
|
case "stop":
|
|
334
|
-
return "stop";
|
|
334
|
+
return hasRefusal ? "content-filter" : "stop";
|
|
335
335
|
case "length":
|
|
336
336
|
return "length";
|
|
337
337
|
case "content_filter":
|
|
@@ -455,9 +455,9 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
455
455
|
var _a, _b, _c;
|
|
456
456
|
return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
|
|
457
457
|
}
|
|
458
|
-
transformRequestBody(args) {
|
|
458
|
+
transformRequestBody(args, warnings) {
|
|
459
459
|
var _a, _b, _c;
|
|
460
|
-
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;
|
|
461
461
|
}
|
|
462
462
|
convertUsage(usage) {
|
|
463
463
|
var _a, _b, _c;
|
|
@@ -582,7 +582,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
582
582
|
async doGenerate(options) {
|
|
583
583
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
584
584
|
const { args, warnings, metadataKey } = await this.getArgs({ ...options });
|
|
585
|
-
const transformedBody = this.transformRequestBody(args);
|
|
585
|
+
const transformedBody = this.transformRequestBody(args, warnings);
|
|
586
586
|
const body = JSON.stringify(transformedBody);
|
|
587
587
|
const {
|
|
588
588
|
responseHeaders,
|
|
@@ -618,6 +618,11 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
618
618
|
});
|
|
619
619
|
}
|
|
620
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
|
+
}
|
|
621
626
|
if (choice.message.tool_calls != null) {
|
|
622
627
|
for (const toolCall of choice.message.tool_calls) {
|
|
623
628
|
const thoughtSignature = (_c = (_b = toolCall.extra_content) == null ? void 0 : _b.google) == null ? void 0 : _c.thought_signature;
|
|
@@ -650,7 +655,9 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
650
655
|
return {
|
|
651
656
|
content,
|
|
652
657
|
finishReason: {
|
|
653
|
-
unified: mapOpenAICompatibleFinishReason(choice.finish_reason
|
|
658
|
+
unified: mapOpenAICompatibleFinishReason(choice.finish_reason, {
|
|
659
|
+
hasRefusal
|
|
660
|
+
}),
|
|
654
661
|
raw: (_h = choice.finish_reason) != null ? _h : void 0
|
|
655
662
|
},
|
|
656
663
|
usage: this.convertUsage(responseBody.usage),
|
|
@@ -667,12 +674,15 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
667
674
|
async doStream(options) {
|
|
668
675
|
var _a;
|
|
669
676
|
const { args, warnings, metadataKey } = await this.getArgs({ ...options });
|
|
670
|
-
const body = this.transformRequestBody(
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
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
|
+
);
|
|
676
686
|
const metadataExtractor = (_a = this.config.metadataExtractor) == null ? void 0 : _a.createStreamExtractor();
|
|
677
687
|
const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
|
|
678
688
|
url: this.config.url({
|
|
@@ -735,10 +745,12 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
735
745
|
}
|
|
736
746
|
};
|
|
737
747
|
let finishReason;
|
|
748
|
+
let rawFinishReason = void 0;
|
|
738
749
|
let usage = void 0;
|
|
739
750
|
let isFirstChunk = true;
|
|
740
751
|
let isActiveReasoning = false;
|
|
741
752
|
let isActiveText = false;
|
|
753
|
+
let hasRefusal = false;
|
|
742
754
|
const convertUsage = (usage2) => this.convertUsage(usage2);
|
|
743
755
|
return {
|
|
744
756
|
stream: response.pipeThrough(
|
|
@@ -759,7 +771,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
759
771
|
controller.enqueue({ type: "stream-start", warnings });
|
|
760
772
|
},
|
|
761
773
|
transform(chunk, controller) {
|
|
762
|
-
var _a2
|
|
774
|
+
var _a2;
|
|
763
775
|
if (options.includeRawChunks) {
|
|
764
776
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
765
777
|
}
|
|
@@ -790,10 +802,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
790
802
|
}
|
|
791
803
|
const choice = value.choices[0];
|
|
792
804
|
if ((choice == null ? void 0 : choice.finish_reason) != null) {
|
|
793
|
-
|
|
794
|
-
unified: mapOpenAICompatibleFinishReason(choice.finish_reason),
|
|
795
|
-
raw: (_a2 = choice.finish_reason) != null ? _a2 : void 0
|
|
796
|
-
};
|
|
805
|
+
rawFinishReason = choice.finish_reason;
|
|
797
806
|
}
|
|
798
807
|
if ((choice == null ? void 0 : choice.delta) == null) {
|
|
799
808
|
return;
|
|
@@ -835,7 +844,7 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
835
844
|
delta: textDelta
|
|
836
845
|
});
|
|
837
846
|
};
|
|
838
|
-
const reasoningContent = (
|
|
847
|
+
const reasoningContent = (_a2 = delta.reasoning_content) != null ? _a2 : delta.reasoning;
|
|
839
848
|
if (reasoningContent) {
|
|
840
849
|
enqueueReasoningDelta(reasoningContent);
|
|
841
850
|
}
|
|
@@ -848,6 +857,11 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
848
857
|
enqueueTextDelta(contentPart.text);
|
|
849
858
|
}
|
|
850
859
|
}
|
|
860
|
+
const refusal = delta.refusal;
|
|
861
|
+
if (refusal != null && refusal.length > 0) {
|
|
862
|
+
hasRefusal = true;
|
|
863
|
+
enqueueTextDelta(refusal);
|
|
864
|
+
}
|
|
851
865
|
if (delta.tool_calls != null && delta.tool_calls.length > 0) {
|
|
852
866
|
if (isActiveReasoning) {
|
|
853
867
|
controller.enqueue({
|
|
@@ -878,6 +892,14 @@ var OpenAICompatibleChatLanguageModel = class {
|
|
|
878
892
|
}
|
|
879
893
|
pendingToolCalls.clear();
|
|
880
894
|
toolCallTracker.flush();
|
|
895
|
+
if (rawFinishReason != null) {
|
|
896
|
+
finishReason = {
|
|
897
|
+
unified: mapOpenAICompatibleFinishReason(rawFinishReason, {
|
|
898
|
+
hasRefusal
|
|
899
|
+
}),
|
|
900
|
+
raw: rawFinishReason
|
|
901
|
+
};
|
|
902
|
+
}
|
|
881
903
|
if (finishReason == null) {
|
|
882
904
|
finishReason = { unified: "error", raw: void 0 };
|
|
883
905
|
controller.enqueue({
|
|
@@ -968,6 +990,12 @@ var OpenAICompatibleChatResponseSchema = (0, import_provider_utils2.lazySchema)(
|
|
|
968
990
|
message: import_v43.z.object({
|
|
969
991
|
role: import_v43.z.literal("assistant").nullish(),
|
|
970
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
|
+
),
|
|
971
999
|
reasoning_content: import_v43.z.string().nullish(),
|
|
972
1000
|
reasoning: import_v43.z.string().nullish(),
|
|
973
1001
|
tool_calls: import_v43.z.array(
|
|
@@ -1002,6 +1030,12 @@ var chunkBaseSchema = () => import_v43.z.looseObject({
|
|
|
1002
1030
|
delta: import_v43.z.object({
|
|
1003
1031
|
role: import_v43.z.enum(["assistant", ""]).nullish(),
|
|
1004
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
|
+
),
|
|
1005
1039
|
// Most openai-compatible models set `reasoning_content`, but some
|
|
1006
1040
|
// providers serving `gpt-oss` set `reasoning`. See #7866
|
|
1007
1041
|
reasoning_content: import_v43.z.string().nullish(),
|
|
@@ -1796,7 +1830,7 @@ async function fileToBlob(file) {
|
|
|
1796
1830
|
var import_provider_utils8 = require("@ai-sdk/provider-utils");
|
|
1797
1831
|
|
|
1798
1832
|
// src/version.ts
|
|
1799
|
-
var VERSION = true ? "2.0.
|
|
1833
|
+
var VERSION = true ? "2.0.92" : "0.0.0-test";
|
|
1800
1834
|
|
|
1801
1835
|
// src/openai-compatible-provider.ts
|
|
1802
1836
|
function createOpenAICompatible(options) {
|