Repository navigation
fix(tools): preserve the strict flag when converting tool definitions #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,18 +32,18 @@ function convertAnthropicToolsToOpenRouter(anthropicTools) { | |
| // provider before routing picked this one, and re-wrapping strips the | ||
| // name ({type:"function", function:{name:undefined}}). | ||
| if (tool?.type === "function" && tool.function) return tool; | ||
| return { | ||
| type: "function", | ||
| function: { | ||
| name: tool.name, | ||
| description: tool.description || "", | ||
| parameters: tool.input_schema || { | ||
| type: "object", | ||
| properties: {}, | ||
| required: [] | ||
| } | ||
| const fn = { | ||
| name: tool.name, | ||
| description: tool.description || "", | ||
| parameters: tool.input_schema || { | ||
| type: "object", | ||
| properties: {}, | ||
| required: [] | ||
| } | ||
| }; | ||
|
Comment on lines
+35
to
43
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| // Keep the strict-schema flag so the upstream enforces the schema. | ||
| if (typeof tool.strict === "boolean") fn.strict = tool.strict; | ||
| return { type: "function", function: fn }; | ||
| }); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -259,6 +259,7 @@ function ensureAnthropicToolFormat(tools) { | |
| name: tool.function.name, | ||
| description: tool.function.description, | ||
| input_schema: tool.function.parameters, | ||
| ...(typeof tool.function.strict === "boolean" ? { strict: tool.function.strict } : {}), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| }; | ||
| } | ||
| // Ensure input_schema has required 'type' field | ||
|
|
@@ -272,6 +273,7 @@ function ensureAnthropicToolFormat(tools) { | |
| name: tool.name || "unnamed_tool", | ||
| description: tool.description || tool.name || "No description provided", | ||
| input_schema, | ||
| ...(typeof tool.strict === "boolean" ? { strict: tool.strict } : {}), | ||
| }; | ||
| }); | ||
| } | ||
|
|
@@ -3338,6 +3340,8 @@ function toolResultCompressionAllowed(payload) { | |
|
|
||
| module.exports = { | ||
| processMessage, | ||
| // Exported for unit testing of tool-format normalisation. | ||
| ensureAnthropicToolFormat, | ||
| // Exported for unit testing of the client-profile gate on tool-result compression. | ||
| toolResultCompressionAllowed, | ||
| // Exported for unit testing of sanitizePayload tool handling. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| const assert = require("assert"); | ||
| const { describe, it } = require("node:test"); | ||
|
|
||
| process.env.MODEL_PROVIDER = process.env.MODEL_PROVIDER || "databricks"; | ||
| process.env.DATABRICKS_API_KEY = process.env.DATABRICKS_API_KEY || "test-key"; | ||
| process.env.DATABRICKS_API_BASE = process.env.DATABRICKS_API_BASE || "http://test.com"; | ||
|
|
||
| const { convertAnthropicToolsToOpenRouter } = require("../src/clients/openrouter-utils"); | ||
| const { convertOpenAIToAnthropic } = require("../src/clients/openai-format"); | ||
| const { ensureAnthropicToolFormat } = require("../src/orchestrator"); | ||
|
|
||
| const schema = { type: "object", properties: { a: { type: "string" } }, required: ["a"], additionalProperties: false }; | ||
|
|
||
| // Regression tests for the tool `strict` flag being dropped by tool rebuilds (issue #116). | ||
| describe("tool strict flag preservation", () => { | ||
| it("convertAnthropicToolsToOpenRouter keeps strict true and false", () => { | ||
| const out = convertAnthropicToolsToOpenRouter([ | ||
| { name: "t1", description: "d", strict: true, input_schema: schema }, | ||
| { name: "t2", description: "d", strict: false, input_schema: schema }, | ||
| ]); | ||
| assert.strictEqual(out[0].function.strict, true); | ||
| assert.strictEqual(out[1].function.strict, false); | ||
| }); | ||
|
|
||
| it("convertAnthropicToolsToOpenRouter omits strict when unset", () => { | ||
| const [tool] = convertAnthropicToolsToOpenRouter([{ name: "t", input_schema: schema }]); | ||
| assert.ok(!("strict" in tool.function)); | ||
| }); | ||
|
|
||
| it("convertOpenAIToAnthropic keeps strict", () => { | ||
| const out = convertOpenAIToAnthropic({ | ||
| model: "m", | ||
| messages: [{ role: "user", content: "hi" }], | ||
| tools: [{ type: "function", function: { name: "t", strict: true, parameters: schema } }], | ||
| }); | ||
| assert.strictEqual(out.tools[0].strict, true); | ||
| assert.deepStrictEqual(out.tools[0].input_schema, schema); | ||
| }); | ||
|
|
||
| it("convertOpenAIToAnthropic omits strict when unset", () => { | ||
| const out = convertOpenAIToAnthropic({ | ||
| model: "m", | ||
| messages: [{ role: "user", content: "hi" }], | ||
| tools: [{ type: "function", function: { name: "t", parameters: schema } }], | ||
| }); | ||
| assert.ok(!("strict" in out.tools[0])); | ||
| }); | ||
|
|
||
| it("ensureAnthropicToolFormat keeps strict from both tool shapes", () => { | ||
| const out = ensureAnthropicToolFormat([ | ||
| { type: "function", function: { name: "t1", strict: true, parameters: schema } }, | ||
| { name: "t2", description: "d", strict: true, input_schema: schema }, | ||
| { name: "t3", description: "d", input_schema: schema }, | ||
| ]); | ||
| assert.strictEqual(out[0].strict, true); | ||
| assert.strictEqual(out[1].strict, true); | ||
| assert.ok(!("strict" in out[2])); | ||
| }); | ||
|
|
||
| it("OpenAI Chat tool survives the full round trip to the OpenAI-format upstream", () => { | ||
| const anthropic = convertOpenAIToAnthropic({ | ||
| model: "m", | ||
| messages: [{ role: "user", content: "hi" }], | ||
| tools: [{ type: "function", function: { name: "t", strict: true, parameters: schema } }], | ||
| }); | ||
| const [forwarded] = convertAnthropicToolsToOpenRouter(ensureAnthropicToolFormat(anthropic.tools)); | ||
| assert.strictEqual(forwarded.function.strict, true); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition
typeof tool.function.strict === "boolean"does not protect against a null/undefinedtool.functionin case the earlier.filterlogic changes ortoolstructure varies. Accessingtool.function.strictdirectly risks a TypeError iftool.functionis missing.Suggestion: