Repository navigation
fix(tools): preserve the strict flag when converting tool definitions - #138
Conversation
Three tool rebuilds dropped the `strict` flag, so a strict-schema tool sent by a client reached the upstream as a non-strict tool: - convertOpenAIToAnthropic (OpenAI Chat -> Anthropic) built tools from name/description/parameters only. - ensureAnthropicToolFormat (orchestrator) did the same when unwrapping OpenAI-shaped tools and when normalising Anthropic ones. - convertAnthropicToolsToOpenRouter (Anthropic -> OpenAI-format upstreams) never emitted function.strict. Each now carries a boolean strict through, and omits it when the client did not set one. Adds regression tests, including a full OpenAI Chat round trip, and registers them in test:unit. Refs Fast-Editor#116
|
🔍 OpenCodeReview found 3 issue(s) in this PR.
|
| } | ||
| }; | ||
| // Keep the strict-schema flag so it survives the round trip. | ||
| if (typeof tool.function.strict === "boolean") converted.strict = tool.function.strict; |
There was a problem hiding this comment.
The condition typeof tool.function.strict === "boolean" does not protect against a null/undefined tool.function in case the earlier .filter logic changes or tool structure varies. Accessing tool.function.strict directly risks a TypeError if tool.function is missing.
Suggestion:
| if (typeof tool.function.strict === "boolean") converted.strict = tool.function.strict; | |
| if (tool.function && typeof tool.function.strict === "boolean") converted.strict = tool.function.strict; |
| const fn = { | ||
| name: tool.name, | ||
| description: tool.description || "", | ||
| parameters: tool.input_schema || { | ||
| type: "object", | ||
| properties: {}, | ||
| required: [] | ||
| } | ||
| }; |
There was a problem hiding this comment.
Similar to the previous file, tool.strict is accessed without ensuring tool itself is valid (though unlikely given the earlier checks). More importantly, the new fn object construction duplicates the schema fallback logic (type: 'object', properties: {}, required: []) already present in openai-format.js. Consider extracting this schema fallback into a shared utility for consistency.
| name: tool.function.name, | ||
| description: tool.function.description, | ||
| input_schema: tool.function.parameters, | ||
| ...(typeof tool.function.strict === "boolean" ? { strict: tool.function.strict } : {}), |
Refs #116
Follow-up to the remaining part of #116 (see the comment from 24 Sep): the schema compressor was fixed, but three other tool rebuilds still drop
strict, so a strict tool arrives upstream as non-strict.convertOpenAIToAnthropic(src/clients/openai-format.js)ensureAnthropicToolFormat(src/orchestrator/index.js), both for OpenAI-shaped and Anthropic-shaped toolsconvertAnthropicToolsToOpenRouter(src/clients/openrouter-utils.js)A boolean
strictis now carried through each; it is omitted when the client did not set it, so existing payloads are unchanged.Tests: new
test/tool-strict-preservation.test.js(added totest:unit) covers each converter plus an OpenAI Chat round trip. 4 of its 6 tests fail without the change. Ran it together with the format-conversion, schema-compression, ingress and azure format tests: 65 pass, 0 fail.