Anthropic: support strict tool use via ToolInfo.options["strict"]#4442
Anthropic: support strict tool use via ToolInfo.options["strict"]#4442wuisabel-gif wants to merge 4 commits into
Conversation
Honour ToolInfo.options["strict"] = True in tool_params_for_tool_info: set strict on the ToolParam, strip extended JSON Schema validation keywords (which strict schemas reject, matching the response_schema path), and add the structured-outputs-2025-11-13 beta header when any tool is strict. This is the only way to get schema-guaranteed tool calls alongside thinking, where tool_choice cannot force a tool call. Fixes UKGovernmentBEIS#4432 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
… use Anthropic strict schemas require additionalProperties: false on all objects, nested included. The strict tool path only gets the top level (via the ToolParams default); nested object schemas omit the key.
Flat-schema tests pass (strict accepted, extended keywords stripped, schema-conformant call with and without thinking). Nested-object tests fail with 400 'additionalProperties must be explicitly set to false' — live repro of the nested additionalProperties gap.
|
@wuisabel-gif, thanks for the contribution. One gap unit tests can't catch: whether the API accepts what we send. We have live "slow" tests for exactly this (gated behind
I've pushed two test commits:
The fix should mirror the One design question: |
This PR contains:
What is the current behavior? (You can also link to an open issue here)
Closes #4432.
AnthropicAPI.tool_params_for_tool_infohardcodes theToolParamfields, so there is no way to setstrict: trueon a tool definition. When thinking is enabled,tool_choiceis (correctly) not sent — so there is currently no way to get both reasoning and a schema-guaranteed tool call on Anthropic.What is the new behavior?
Setting
ToolInfo.options["strict"] = True(e.g. viaToolDef(..., options={"strict": True})) now:strict: trueon the serialized tool definitionminimum,maximum,pattern, etc.) from the tool'sinput_schema, which strict schemas reject — same treatment as the existingresponse_schemapathstructured-outputs-2025-11-13beta header when any tool in the request is strictTools without the option are serialized exactly as before (no
strictkey, schema untouched).Does this PR introduce a breaking change? (What changes might users need to make in their application due to this PR?)
No.
Other information:
Unit tests cover default (non-strict) serialization, strict serialization/keyword stripping, and beta header presence/absence.
🤖 Generated with Claude Code