Skip to content

Commit b140824

Browse files
fix(providers): remove unsupported keywords from strict tool schemas (#2439)
* fix(providers): remove unsupported keywords from strict tool schemas Some gateways send strict OpenAI tool schemas to Anthropic strict tool use. Anthropic strict mode rejects some keywords that OpenAI strict mode accepts, for example maxItems. The gateway then rejects the full request with "For 'array' type, property 'maxItems' is not supported". - Add stripUnsupportedStrictKeywords(). It removes unsupported constraints from strict schemas and adds them to the description. - Call it only on the strict path in convertToolsForOpenAI(). MCP tools and non-strict tools keep their declared schemas. - Remove maxItems from ask_followup_question and minimum from edit_file. The descriptions state the limits. - Limit follow-up suggestions to four at runtime. - Add a test that keeps all native tool schemas in the portable subset. Fixes #869 * fix(providers): set additionalProperties to false in strict schemas Strict mode requires additionalProperties: false on every object schema. stripUnsupportedStrictKeywords() removed a true or schema value, but it did not set false. convertToolSchemaForOpenAI() does not go into anyOf. Thus an object in anyOf had no additionalProperties value. - Set additionalProperties to false. Keep the removed value in the description. - Do not go into the additionalProperties value. The function always removes a schema value. - Add focused tests for each keyword list, each branch, and the description note. * test(tools): use strict schema sanitizer in native guard
1 parent 3b06b95 commit b140824

10 files changed

Lines changed: 639 additions & 8 deletions

File tree

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

Lines changed: 230 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,227 @@ describe("BaseProvider", () => {
338338
expect(result?.[0].function.parameters.required).toEqual(["path"])
339339
})
340340

341+
it("removes unsupported constraints from the reported follow-up schema", () => {
342+
const tools = [
343+
{
344+
type: "function",
345+
function: {
346+
name: "ask_followup_question",
347+
description: "Ask a question",
348+
parameters: {
349+
type: "object",
350+
properties: {
351+
follow_up: {
352+
type: "array",
353+
description: "Required list of 2-4 suggested responses",
354+
items: { type: "object", properties: { text: { type: "string" } } },
355+
minItems: 1,
356+
maxItems: 4,
357+
},
358+
},
359+
},
360+
},
361+
},
362+
]
363+
364+
const result = provider.testConvertToolsForOpenAI(tools)
365+
const followUp = result?.[0].function.parameters.properties.follow_up
366+
367+
expect(result?.[0].function.strict).toBe(true)
368+
expect(followUp.minItems).toBe(1)
369+
expect(followUp.maxItems).toBeUndefined()
370+
expect(followUp.description).toBe('Required list of 2-4 suggested responses\n\nConstraints: {"maxItems":4}')
371+
})
372+
373+
it("removes unsupported constraints recursively while keeping supported keywords", () => {
374+
const tools = [
375+
{
376+
type: "function",
377+
function: {
378+
name: "constraint_tool",
379+
parameters: {
380+
type: "object",
381+
properties: {
382+
count: { type: "number", minimum: 1, maximum: 9, multipleOf: 1 },
383+
name: {
384+
type: "string",
385+
minLength: 1,
386+
maxLength: 5,
387+
pattern: "^a",
388+
format: "email",
389+
},
390+
unsupportedFormat: { type: "string", format: "phone" },
391+
list: {
392+
type: "array",
393+
items: { type: "integer", exclusiveMinimum: 0 },
394+
minItems: 2,
395+
uniqueItems: true,
396+
contains: { type: "integer", minimum: 1 },
397+
},
398+
choice: { anyOf: [{ type: "string", maxLength: 3 }, { type: "null" }] },
399+
},
400+
$defs: { bounded: { type: "integer", maximum: 3 } },
401+
},
402+
},
403+
},
404+
]
405+
406+
const result = provider.testConvertToolsForOpenAI(tools)
407+
const parameters = result?.[0].function.parameters
408+
409+
expect(parameters.properties.count).toEqual({
410+
type: "number",
411+
description: 'Constraints: {"minimum":1,"maximum":9,"multipleOf":1}',
412+
})
413+
expect(parameters.properties.name).toMatchObject({ type: "string", pattern: "^a", format: "email" })
414+
expect(parameters.properties.unsupportedFormat.format).toBeUndefined()
415+
expect(parameters.properties.list.minItems).toBeUndefined()
416+
expect(parameters.properties.list.uniqueItems).toBeUndefined()
417+
expect(parameters.properties.list.contains).toBeUndefined()
418+
expect(parameters.properties.list.items.exclusiveMinimum).toBeUndefined()
419+
expect(parameters.properties.choice.anyOf[0].maxLength).toBeUndefined()
420+
expect(parameters.$defs.bounded.maximum).toBeUndefined()
421+
})
422+
423+
it.each([0, 1])("keeps supported minItems value %s", (minItems) => {
424+
const tools = [
425+
{
426+
type: "function",
427+
function: {
428+
name: "list_tool",
429+
parameters: {
430+
type: "object",
431+
properties: { values: { type: "array", items: { type: "string" }, minItems } },
432+
},
433+
},
434+
},
435+
]
436+
437+
const result = provider.testConvertToolsForOpenAI(tools)
438+
439+
expect(result?.[0].function.parameters.properties.values.minItems).toBe(minItems)
440+
})
441+
442+
it("processes tuple items and nested schema keywords", () => {
443+
const tools = [
444+
{
445+
type: "function",
446+
function: {
447+
name: "nested_schema_tool",
448+
parameters: {
449+
type: "object",
450+
properties: {
451+
values: {
452+
type: "array",
453+
items: [
454+
{ type: "string", maxLength: 2 },
455+
{ type: "integer", minimum: 0 },
456+
],
457+
},
458+
},
459+
additionalProperties: { type: "string", minLength: 1 },
460+
not: { type: "number", maximum: 10 },
461+
if: { type: "number", minimum: 1 },
462+
then: { type: "string", maxLength: 5 },
463+
else: { type: "string", minLength: 2 },
464+
},
465+
},
466+
},
467+
]
468+
469+
const result = provider.testConvertToolsForOpenAI(tools)
470+
const parameters = result?.[0].function.parameters
471+
472+
expect(parameters.properties.values.items[0].maxLength).toBeUndefined()
473+
expect(parameters.properties.values.items[1].minimum).toBeUndefined()
474+
expect(parameters.additionalProperties).toBe(false)
475+
expect(parameters.not.maximum).toBeUndefined()
476+
expect(parameters.if.minimum).toBeUndefined()
477+
expect(parameters.then.maxLength).toBeUndefined()
478+
expect(parameters.else.minLength).toBeUndefined()
479+
})
480+
481+
it("replaces additionalProperties values other than false in nested schemas", () => {
482+
const tools = [
483+
{
484+
type: "function",
485+
function: {
486+
name: "composed_object_tool",
487+
parameters: {
488+
type: "object",
489+
properties: {
490+
options: {
491+
anyOf: [
492+
{ type: "object", properties: {}, additionalProperties: true },
493+
{ type: "object", properties: {}, additionalProperties: { type: "string" } },
494+
],
495+
},
496+
},
497+
},
498+
},
499+
},
500+
]
501+
502+
const result = provider.testConvertToolsForOpenAI(tools)
503+
const variants = result?.[0].function.parameters.properties.options.anyOf
504+
505+
expect(variants[0].additionalProperties).toBe(false)
506+
expect(variants[1].additionalProperties).toBe(false)
507+
expect(variants[0].description).toBe('Constraints: {"additionalProperties":true}')
508+
expect(variants[1].description).toBe('Constraints: {"additionalProperties":{"type":"string"}}')
509+
})
510+
511+
it("keeps properties whose names match unsupported keywords", () => {
512+
const tools = [
513+
{
514+
type: "function",
515+
function: {
516+
name: "keyword_property_tool",
517+
parameters: {
518+
type: "object",
519+
properties: {
520+
maxItems: { type: "integer" },
521+
minimum: { type: "number" },
522+
},
523+
},
524+
},
525+
},
526+
]
527+
528+
const result = provider.testConvertToolsForOpenAI(tools)
529+
530+
expect(result?.[0].function.parameters.properties).toHaveProperty("maxItems")
531+
expect(result?.[0].function.parameters.properties).toHaveProperty("minimum")
532+
})
533+
534+
it("normalizes nullable object properties before stripping constraints", () => {
535+
const tools = [
536+
{
537+
type: "function",
538+
function: {
539+
name: "nullable_object_tool",
540+
parameters: {
541+
type: "object",
542+
properties: {
543+
options: {
544+
type: ["object", "null"],
545+
properties: { limit: { type: "integer", minimum: 1 } },
546+
},
547+
},
548+
},
549+
},
550+
},
551+
]
552+
553+
const result = provider.testConvertToolsForOpenAI(tools)
554+
const options = result?.[0].function.parameters.properties.options
555+
556+
expect(options.type).toBe("object")
557+
expect(options.required).toEqual(["limit"])
558+
expect(options.additionalProperties).toBe(false)
559+
expect(options.properties.limit.minimum).toBeUndefined()
560+
})
561+
341562
it("should not apply schema conversion to MCP tools in base-provider", () => {
342563
// Note: In base-provider, MCP tools are passed through unchanged
343564
// The openai-native provider has its own handling for MCP tools
@@ -350,8 +571,9 @@ describe("BaseProvider", () => {
350571
parameters: {
351572
type: "object",
352573
properties: {
353-
token: { type: "string" },
574+
token: { type: "string", maxLength: 20 },
354575
},
576+
maxItems: 4,
355577
required: ["token"],
356578
},
357579
},
@@ -362,6 +584,8 @@ describe("BaseProvider", () => {
362584

363585
// MCP tools pass through original parameters in base-provider
364586
expect(result?.[0].function.parameters.additionalProperties).toBeUndefined()
587+
expect(result?.[0].function.parameters.maxItems).toBe(4)
588+
expect(result?.[0].function.parameters.properties.token.maxLength).toBe(20)
365589
})
366590

367591
it("should sanitize lone UTF-16 surrogates in name, description, and parameters (#461)", () => {
@@ -453,9 +677,10 @@ describe("BaseProvider", () => {
453677
type: "object",
454678
properties: {
455679
path: { type: "string" },
456-
offset: { type: "integer" },
680+
offset: { type: "integer", minimum: 1 },
457681
},
458682
required: ["path"],
683+
maxItems: 4,
459684
},
460685
},
461686
},
@@ -478,7 +703,7 @@ describe("BaseProvider", () => {
478703
parameters: {
479704
type: "object",
480705
properties: {
481-
path: { type: ["string", "null"] },
706+
path: { type: ["string", "null"], maxLength: 20 },
482707
},
483708
required: ["path"],
484709
},
@@ -488,10 +713,12 @@ describe("BaseProvider", () => {
488713

489714
const strictResult = provider.testConvertToolsForOpenAI(tools, true)
490715
expect(strictResult?.[0].function.parameters.properties.path.type).toBe("string")
716+
expect(strictResult?.[0].function.parameters.properties.path.maxLength).toBeUndefined()
491717

492718
// Caller-owned schema is untouched, so a later non-strict request
493719
// can still send the original nullable type.
494720
expect(tools[0].function.parameters.properties.path.type).toEqual(["string", "null"])
721+
expect(tools[0].function.parameters.properties.path.maxLength).toBe(20)
495722

496723
const nonStrictResult = provider.testConvertToolsForOpenAI(tools, false)
497724
expect(nonStrictResult?.[0].function.parameters).toEqual(tools[0].function.parameters)

‎src/api/providers/__tests__/openai.spec.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,6 +1895,26 @@ describe("OpenAiHandler - strict tool schemas", () => {
18951895
},
18961896
},
18971897
]
1898+
const constrainedTools: OpenAI.Chat.ChatCompletionFunctionTool[] = [
1899+
{
1900+
type: "function",
1901+
function: {
1902+
name: "ask_followup_question",
1903+
description: "Ask a question",
1904+
parameters: {
1905+
type: "object",
1906+
properties: {
1907+
follow_up: {
1908+
type: "array",
1909+
items: { type: "string" },
1910+
minItems: 1,
1911+
maxItems: 4,
1912+
},
1913+
},
1914+
},
1915+
},
1916+
},
1917+
]
18981918
const baseOptions = () =>
18991919
makeApiHandlerOptions({
19001920
openAiApiKey: "test-api-key",
@@ -1919,6 +1939,22 @@ describe("OpenAiHandler - strict tool schemas", () => {
19191939
expect(request.tools[0].function.parameters.additionalProperties).toBe(false)
19201940
})
19211941

1942+
it.each([undefined, true])("removes unsupported constraints when strict schemas are %s", async (strict) => {
1943+
const options = strict === undefined ? baseOptions() : { ...baseOptions(), openAiStrictToolSchemas: strict }
1944+
const handler = new OpenAiHandler(options)
1945+
const stream = handler.createMessage(systemPrompt, messages, {
1946+
taskId: "test-task",
1947+
tools: constrainedTools,
1948+
})
1949+
await collectStream(stream)
1950+
1951+
const request = mockCreate.mock.calls[0][0]
1952+
const followUp = request.tools[0].function.parameters.properties.follow_up
1953+
expect(request.tools[0].function.strict).toBe(true)
1954+
expect(followUp.minItems).toBe(1)
1955+
expect(followUp.maxItems).toBeUndefined()
1956+
})
1957+
19221958
it("sends strict: false with the declared schema when openAiStrictToolSchemas is false", async () => {
19231959
const handler = new OpenAiHandler({ ...baseOptions(), openAiStrictToolSchemas: false })
19241960
const stream = handler.createMessage(systemPrompt, messages, { taskId: "test-task", tools })
@@ -1930,6 +1966,19 @@ describe("OpenAiHandler - strict tool schemas", () => {
19301966
expect(request.tools[0].function.parameters).toEqual(tools[0].function.parameters)
19311967
})
19321968

1969+
it("keeps unsupported constraints when strict schemas are disabled", async () => {
1970+
const handler = new OpenAiHandler({ ...baseOptions(), openAiStrictToolSchemas: false })
1971+
const stream = handler.createMessage(systemPrompt, messages, {
1972+
taskId: "test-task",
1973+
tools: constrainedTools,
1974+
})
1975+
await collectStream(stream)
1976+
1977+
const request = mockCreate.mock.calls[0][0]
1978+
expect(request.tools[0].function.strict).toBe(false)
1979+
expect(request.tools[0].function.parameters.properties.follow_up.maxItems).toBe(4)
1980+
})
1981+
19331982
it("propagates a gateway 400 that rejects strict tool schemas", async () => {
19341983
const strictRejection = Object.assign(
19351984
new Error("strict=true requires generated function arguments to satisfy the declared JSON Schema"),

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { DEFAULT_OPEN_AI_STRICT_TOOL_SCHEMAS, type ModelInfo } from "@roo-code/t
55
import type { ApiHandler, ApiHandlerCreateMessageMetadata, ModelCacheScope } from "../index"
66
import { ApiStream } from "../transform/stream"
77
import { sanitizeSurrogates, sanitizeSurrogatesDeep } from "../transform/sanitize-surrogates"
8+
import { stripUnsupportedStrictKeywords } from "../transform/strict-tool-schema"
89
import { countTokens } from "../../utils/countTokens"
910
import { isMcpTool } from "../../utils/mcp-name"
1011
import { detachOnAbort } from "./utils/abort-signal"
@@ -69,7 +70,7 @@ export abstract class BaseProvider implements ApiHandler {
6970
strict: useStrict,
7071
parameters: sanitizeSurrogatesDeep(
7172
useStrict
72-
? this.convertToolSchemaForOpenAI(tool.function.parameters)
73+
? stripUnsupportedStrictKeywords(this.convertToolSchemaForOpenAI(tool.function.parameters))
7374
: tool.function.parameters,
7475
),
7576
},

0 commit comments

Comments
 (0)