Skip to content

Commit 5683367

Browse files
authored
fix(core): send unknown v1 openai-compatible model options in the request body (#54694)
1 parent 5b7bad2 commit 5683367

4 files changed

Lines changed: 74 additions & 7 deletions

File tree

‎packages/core/src/v1/config/migrate.ts‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function migrateStandardProvider(info: ConfigProviderV1.Info) {
120120
body: info.options && options.body,
121121
models:
122122
info.models &&
123-
Object.fromEntries(Object.entries(info.models).map(([name, model]) => [name, migrateModel(model)])),
123+
Object.fromEntries(Object.entries(info.models).map(([name, model]) => [name, migrateModel(model, info.npm)])),
124124
}
125125
}
126126

@@ -166,7 +166,7 @@ export function providerID(input: string) {
166166
return input
167167
}
168168

169-
function migrateModel(info: typeof ConfigProviderV1.Model.Type) {
169+
function migrateModel(info: typeof ConfigProviderV1.Model.Type, npm: string | undefined) {
170170
const disableThinkingBlockBinding =
171171
info.options?.thinking?.blockBinding === false || info.options?.reasoningConfig?.blockBinding === false
172172
const options = info.options && { ...info.options }
@@ -185,7 +185,13 @@ function migrateModel(info: typeof ConfigProviderV1.Model.Type) {
185185
}
186186
}
187187

188-
const settings = options && ConfigProviderOptionsV1.model(options)
188+
const compatible = (info.provider?.npm ?? npm) === "@ai-sdk/openai-compatible"
189+
const split = (input: Record<string, unknown>) =>
190+
compatible
191+
? ConfigProviderOptionsV1.compatibleModel(input)
192+
: { settings: ConfigProviderOptionsV1.model(input), body: undefined }
193+
const migrated = options && split(options)
194+
const settings = migrated?.settings
189195
const compatibility = Model.compatibility(info.interleaved)
190196
const costs = info.cost && [
191197
{
@@ -224,12 +230,10 @@ function migrateModel(info: typeof ConfigProviderV1.Model.Type) {
224230
settings: info.provider?.api ? { ...settings, baseURL: info.provider.api } : settings,
225231
capabilities,
226232
headers: info.headers,
233+
body: migrated?.body,
227234
variants:
228235
info.variants &&
229-
Object.entries(info.variants).map(([id, options]) => ({
230-
id,
231-
settings: ConfigProviderOptionsV1.model(options),
232-
})),
236+
Object.entries(info.variants).map(([id, options]) => ({ id, ...split(options) })),
233237
cost: costs,
234238
disabled: info.status === "deprecated" ? true : undefined,
235239
limit: info.limit && {

‎packages/core/src/v1/config/provider-options.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,30 @@ export function provider(options: Options): ProviderResult {
2929
export function model(options: Options) {
3030
return { ...options }
3131
}
32+
33+
// Model options read before the request is built; v1's `@ai-sdk/openai-compatible` sent every other one verbatim
34+
// in the request body.
35+
const COMPATIBLE_KEYS = new Set([
36+
"apiKey",
37+
"baseURL",
38+
"headers",
39+
"body",
40+
"extraBody",
41+
"timeout",
42+
"chunkTimeout",
43+
"headerTimeout",
44+
"disabled",
45+
"reasoningEffort",
46+
"textVerbosity",
47+
"user",
48+
"strictJsonSchema",
49+
])
50+
51+
export function compatibleModel(options: Options) {
52+
const entries = Object.entries(options)
53+
const body = Object.fromEntries(entries.filter(([key]) => !COMPATIBLE_KEYS.has(key)))
54+
return {
55+
settings: Object.fromEntries(entries.filter(([key]) => COMPATIBLE_KEYS.has(key))),
56+
body: Object.keys(body).length ? body : undefined,
57+
}
58+
}

‎packages/core/test/config/config.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,6 +1564,16 @@ describe("Config", () => {
15641564
},
15651565
},
15661566
},
1567+
litellm: {
1568+
npm: "@ai-sdk/openai-compatible",
1569+
options: { baseURL: "https://litellm.example/v1" },
1570+
models: {
1571+
"glm-5.3-flashx": {
1572+
options: { reasoningEffort: "high", allowed_openai_params: ["reasoning_effort"] },
1573+
variants: { thinking: { chat_template_kwargs: { enable_thinking: true } } },
1574+
},
1575+
},
1576+
},
15671577
},
15681578
compaction: { auto: true, tail_turns: 3, preserve_recent_tokens: 2000, reserved: 10000 },
15691579
experimental: { mcp_timeout: 5000 },
@@ -1641,6 +1651,11 @@ describe("Config", () => {
16411651
},
16421652
},
16431653
})
1654+
expect(documents[0]?.info.providers?.litellm?.models?.["glm-5.3-flashx"]).toMatchObject({
1655+
settings: { reasoningEffort: "high" },
1656+
body: { allowed_openai_params: ["reasoning_effort"] },
1657+
variants: [{ id: "thinking", settings: {}, body: { chat_template_kwargs: { enable_thinking: true } } }],
1658+
})
16441659
expect(documents[0]?.info.compaction).toEqual({
16451660
auto: true,
16461661
keep: { tokens: 2000 },

‎packages/core/test/config/provider-options.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,27 @@ describe("ConfigProviderOptionsV1", () => {
2424
})
2525
})
2626

27+
test("moves openai-compatible options the package does not read into the body", () => {
28+
expect(
29+
ConfigProviderOptionsV1.compatibleModel({
30+
reasoningEffort: "high",
31+
timeout: 1000,
32+
allowed_openai_params: ["reasoning_effort"],
33+
chat_template_kwargs: { enable_thinking: true },
34+
}),
35+
).toEqual({
36+
settings: { reasoningEffort: "high", timeout: 1000 },
37+
body: {
38+
allowed_openai_params: ["reasoning_effort"],
39+
chat_template_kwargs: { enable_thinking: true },
40+
},
41+
})
42+
expect(ConfigProviderOptionsV1.compatibleModel({ reasoningEffort: "low" })).toEqual({
43+
settings: { reasoningEffort: "low" },
44+
body: undefined,
45+
})
46+
})
47+
2748
test("keeps model and variant options unchanged", () => {
2849
expect(
2950
ConfigProviderOptionsV1.model({

0 commit comments

Comments
 (0)