Skip to content

Commit 2baac5e

Browse files
easonLiangWorldedtecheasonliang28easonLiangWorldedtechclaude
authored
fix(api): add openAiStrictToolSchemas setting for OpenAI-compatible gateways (Zoo-Code-Org#1828)
* fix(api): add openAiStrictToolSchemas setting for OpenAI-compatible gateways Some OpenAI-compatible gateways reject tool definitions with strict: true (e.g. NInfer returns HTTP 400: "strict=true requires generated function arguments to satisfy the declared JSON Schema"). Add the optional openAiStrictToolSchemas setting to the OpenAI Compatible provider (default true, preserving current behavior). When false, tools are sent with strict: false and the declared JSON schema is passed through unchanged, matching the existing MCP tool precedent. Refs Zoo-Code-Org#1827 * fix(i18n): complete openAiStrictToolSchemas strings for all locales * test(visual): update OpenAI Compatible baselines for strict tool schemas checkbox * fix(api): clone tool schema properties before strict normalization Strict conversion previously rewrote nullable types on the caller-owned property objects, so a tool array reused across a strict request and a later openAiStrictToolSchemas: false request would send the already-normalized schema. Clone each property before normalizing and add a regression test for nullable reuse. Also add request-path coverage for non-streaming and O3-family requests with the strict setting (CodeRabbit findings). * chore: trigger CodeRabbit re-review / label reconcile * Describe strict mode as optional for Chat Completions Chat Completions does not require strict: true - strict mode is off unless the function definition sets it. The description claimed it is required by OpenAI and limited disabling to endpoints that reject strict schemas, which is not what the setting does: it is a choice, and the default is strict. Reworded all 18 locales to match the English wording. * Regenerate the OpenAI visual snapshots for the reworded description The provider settings story renders the description, so the committed snapshots no longer match after the wording change. Regenerated with the visual docker runner (the same image CI uses) so the baselines are not host-rendered. * Fix grammatical agreement in the Russian description нестрогое вызов -> нестрогий вызов: вызов is masculine, so the adjective must agree. * Fix Korean and Polish wording in the strict-schemas description Korean: "스키마 준엄격 준수" is a garbled calque of "strict schema adherence"; it now reads as natural Korean ("스키마를 엄격하게 준수"). Polish: "niesurowego" means "not raw", not "non-strict"; corrected to "nieścisłego", which matches the English "non-strict". * Re-run CI after a flaky visual snapshot failure The extension-host-visual job failed on the sidebar snapshot (1324 pixels) at 6c71575 while the same job passed at the previous head. The only change was two locale strings, which the extension-host visual test does not render. This empty commit re-runs the job. * Cover the strict-schemas save round trip at the form layer The component test only checked that the mocked setter received false, so nothing proved the value reaches the persisted payload or comes back from a loaded configuration. Add the round trip in SettingsView: a stored false is restored instead of being replaced by the display default, a change is buffered until Save, and Save carries the value through upsertApiConfiguration. Saving an unrelated field must not materialise the display default for a config that never stored the setting. Co-Authored-By: Claude <[email protected]> * test(openai): cover the strict flag on the O3-family streaming path The streaming O3 branch builds its own request options, so the strict argument was only asserted on the non-streaming path. Add the streaming case with openAiStrictToolSchemas disabled: dropping the argument at the streaming call now fails this test (verified by removing it and re-running). * test(webview-ui): assert a toggled-on value reaches the upsert payload The round-trip tests covered a stored value, persistence on Save, and the unset case, but not the value the user sets during the session. Add the case where the control is toggled on from a stored false and saved: the upsertApiConfiguration payload must carry true. * test(webview-ui): type the round-trip helpers instead of any The round-trip tests introduced three any annotations. Use ProviderSettings for the configuration, Pick<ApiOptionsProps, ...> for the mocked component props, and keep the posted message as WebviewMessage so the payload access stays typed. * test(visual): regenerate the OpenAI Compatible baselines for the merged tree The binary conflict against main could not be combined: Zoo-Code-Org#1847 changed the sane-default values shown in this section (no max-token default, image support off by default) and this PR adds the strict-tool-schemas checkbox. The baselines here are the render of the merged tree, taken from the visual run artifacts at this head, so they carry both changes. --------- Co-authored-by: Eason Liang <[email protected]> Co-authored-by: easonLiangWorldedtech <[email protected]> Co-authored-by: Claude <[email protected]>
1 parent 842b37e commit 2baac5e

33 files changed

Lines changed: 596 additions & 25 deletions

‎packages/types/src/provider-settings.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { z } from "zod"
33
import { providerDefinitionList, type ProviderDefinition } from "./provider-settings/index.js"
44
import { API_PROVIDER_FIELD, SETTINGS_SHAPE_FIELD } from "./provider-settings/common.js"
55
export {
6+
DEFAULT_OPEN_AI_STRICT_TOOL_SCHEMAS,
67
OPEN_AI_CODEX_SERVICE_TIER_KEY,
78
parseOpenAiExtraBody,
89
kimiCodeAuthMethodSchema,

‎packages/types/src/provider-settings/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { basetenProviderDefinition } from "./baseten.js"
3737
import type { ProviderDefinition } from "./common.js"
3838

3939
export { OPEN_AI_CODEX_SERVICE_TIER_KEY } from "./openai-codex.js"
40-
export { parseOpenAiExtraBody } from "./openai.js"
40+
export { parseOpenAiExtraBody, DEFAULT_OPEN_AI_STRICT_TOOL_SCHEMAS } from "./openai.js"
4141
export { kimiCodeAuthMethodSchema, type KimiCodeAuthMethod } from "./kimi-code.js"
4242
export { zaiApiLineSchema, type ZaiApiLine } from "./zai.js"
4343
export {

‎packages/types/src/provider-settings/openai.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ import { baseProviderSettingsShape, createModelIdAccessor, createProviderDefinit
66

77
export const OPEN_AI_MODEL_ID_FIELD = "openAiModelId"
88

9+
/**
10+
* Default for the OpenAI `openAiStrictToolSchemas` setting.
11+
* When true (default), non-MCP tools are sent with strict: true and their
12+
* schemas are normalized for OpenAI strict mode. Some OpenAI-compatible
13+
* proxies reject strict: true; set this to false for those endpoints.
14+
*/
15+
export const DEFAULT_OPEN_AI_STRICT_TOOL_SCHEMAS = true
16+
917
const OPENAI_EXTRA_BODY_RESERVED_KEYS = [
1018
// Prototype-pollution defenses; remaining keys are request-owned, including tool-call protocol controls.
1119
"__proto__",
@@ -83,6 +91,7 @@ export const openAiProviderDefinition = createProviderDefinition({
8391
openAiBaseUrl: z.string().optional(),
8492
openAiApiKey: z.string().optional(),
8593
openAiR1FormatEnabled: z.boolean().optional(),
94+
openAiStrictToolSchemas: z.boolean().optional(),
8695
[OPEN_AI_MODEL_ID_FIELD]: z.string().optional(),
8796
openAiCustomModelInfo: modelInfoSchema.nullish(),
8897
openAiUseAzure: z.boolean().optional(),

‎src/api/providers/__tests__/base-provider.spec.ts‎

Lines changed: 170 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ class TestProvider extends BaseProvider {
2929
}
3030

3131
// Expose protected method for testing
32-
public testConvertToolsForOpenAI(tools: any[] | undefined): any[] | undefined {
33-
return this.convertToolsForOpenAI(tools)
32+
public testConvertToolsForOpenAI(tools: any[] | undefined, strict?: boolean): any[] | undefined {
33+
return this.convertToolsForOpenAI(tools, strict)
3434
}
3535
}
3636

@@ -179,6 +179,96 @@ describe("BaseProvider", () => {
179179
})
180180
})
181181

182+
it("leaves property entries that are not objects unchanged", () => {
183+
const schema = {
184+
type: "object",
185+
properties: {
186+
broken: null,
187+
list: ["string", "null"],
188+
note: "plain",
189+
},
190+
}
191+
192+
const result = provider.testConvertToolSchemaForOpenAI(schema)
193+
194+
// The clone guard only normalises object entries, so a null entry, an
195+
// array-valued entry and a plain string entry are passed through as declared.
196+
expect(result.properties.broken).toBe(null)
197+
expect(result.properties.list).toEqual(["string", "null"])
198+
expect(result.properties.note).toBe("plain")
199+
})
200+
201+
it("keeps a nullable type with more than one non-null type as an array", () => {
202+
const schema = {
203+
type: "object",
204+
properties: {
205+
value: { type: ["string", "null", "number"] },
206+
},
207+
}
208+
209+
const result = provider.testConvertToolSchemaForOpenAI(schema)
210+
211+
expect(result.properties.value.type).toEqual(["string", "number"])
212+
})
213+
214+
it("does not treat a plain property as an array of objects", () => {
215+
const schema = {
216+
type: "object",
217+
properties: {
218+
path: { type: "string" },
219+
},
220+
}
221+
222+
const result = provider.testConvertToolSchemaForOpenAI(schema)
223+
224+
// Only array properties get an items conversion, so a plain property
225+
// keeps exactly the keys it declared.
226+
expect(Object.keys(result.properties.path)).toEqual(["type"])
227+
})
228+
229+
it("does not convert items on a non-array property that carries object items", () => {
230+
const schema = {
231+
type: "object",
232+
properties: {
233+
weird: {
234+
type: "string",
235+
items: { type: "object", properties: { a: { type: "string" } } },
236+
},
237+
},
238+
}
239+
240+
const result = provider.testConvertToolSchemaForOpenAI(schema)
241+
242+
expect(result.properties.weird.items.additionalProperties).toBeUndefined()
243+
})
244+
245+
it("leaves an array property without items unchanged", () => {
246+
const schema = {
247+
type: "object",
248+
properties: {
249+
tags: { type: "array" },
250+
},
251+
}
252+
253+
const result = provider.testConvertToolSchemaForOpenAI(schema)
254+
255+
expect(Object.keys(result.properties.tags)).toEqual(["type"])
256+
})
257+
258+
it("leaves a property whose type is only null unchanged", () => {
259+
const schema = {
260+
type: "object",
261+
properties: {
262+
only: { type: "null" },
263+
},
264+
}
265+
266+
const result = provider.testConvertToolSchemaForOpenAI(schema)
267+
268+
// The nullable branch only handles array types; a bare null type is not a
269+
// list to filter.
270+
expect(result.properties.only.type).toBe("null")
271+
})
182272
describe("convertToolsForOpenAI", () => {
183273
it("should return undefined for undefined input", () => {
184274
const result = provider.testConvertToolsForOpenAI(undefined)
@@ -346,6 +436,84 @@ describe("BaseProvider", () => {
346436
expect(result?.[0].function.name).toBe("read\uFFFDfile")
347437
})
348438

439+
it("should set strict: false and preserve declared schema when strict is disabled", () => {
440+
const tools = [
441+
{
442+
type: "function",
443+
function: {
444+
name: "read_file",
445+
description: "Read a file",
446+
parameters: {
447+
type: "object",
448+
properties: {
449+
path: { type: "string" },
450+
offset: { type: "integer" },
451+
},
452+
required: ["path"],
453+
},
454+
},
455+
},
456+
]
457+
458+
const result = provider.testConvertToolsForOpenAI(tools, false)
459+
460+
expect(result?.[0].function.strict).toBe(false)
461+
// Declared schema preserved: original required array, no additionalProperties coercion
462+
expect(result?.[0].function.parameters).toEqual(tools[0].function.parameters)
463+
})
464+
465+
it("should not mutate caller-owned schemas during strict normalization", () => {
466+
const tools = [
467+
{
468+
type: "function",
469+
function: {
470+
name: "nullable_tool",
471+
description: "Tool with a nullable property",
472+
parameters: {
473+
type: "object",
474+
properties: {
475+
path: { type: ["string", "null"] },
476+
},
477+
required: ["path"],
478+
},
479+
},
480+
},
481+
]
482+
483+
const strictResult = provider.testConvertToolsForOpenAI(tools, true)
484+
expect(strictResult?.[0].function.parameters.properties.path.type).toBe("string")
485+
486+
// Caller-owned schema is untouched, so a later non-strict request
487+
// can still send the original nullable type.
488+
expect(tools[0].function.parameters.properties.path.type).toEqual(["string", "null"])
489+
490+
const nonStrictResult = provider.testConvertToolsForOpenAI(tools, false)
491+
expect(nonStrictResult?.[0].function.parameters).toEqual(tools[0].function.parameters)
492+
})
493+
494+
it("should still set strict: false for MCP tools when strict is disabled", () => {
495+
const tools = [
496+
{
497+
type: "function",
498+
function: {
499+
name: "mcp--github--get_me",
500+
description: "Get current user",
501+
parameters: {
502+
type: "object",
503+
properties: {
504+
token: { type: "string" },
505+
},
506+
required: ["token"],
507+
},
508+
},
509+
},
510+
]
511+
512+
const result = provider.testConvertToolsForOpenAI(tools, false)
513+
514+
expect(result?.[0].function.strict).toBe(false)
515+
expect(result?.[0].function.parameters).toEqual(tools[0].function.parameters)
516+
})
349517
it("should preserve non-function tools unchanged", () => {
350518
const tools = [
351519
{

0 commit comments

Comments
 (0)