Skip to content

Anthropic: support strict tool use via ToolInfo.options["strict"]#4442

Open
wuisabel-gif wants to merge 4 commits into
UKGovernmentBEIS:mainfrom
wuisabel-gif:anthropic-strict-tools
Open

Anthropic: support strict tool use via ToolInfo.options["strict"]#4442
wuisabel-gif wants to merge 4 commits into
UKGovernmentBEIS:mainfrom
wuisabel-gif:anthropic-strict-tools

Conversation

@wuisabel-gif

Copy link
Copy Markdown

This PR contains:

  • New features
  • Changes to dev-tools e.g. CI config / github tooling
  • Docs
  • Bug fixes
  • Code refactor

What is the current behavior? (You can also link to an open issue here)

Closes #4432.

AnthropicAPI.tool_params_for_tool_info hardcodes the ToolParam fields, so there is no way to set strict: true on a tool definition. When thinking is enabled, tool_choice is (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. via ToolDef(..., options={"strict": True})) now:

  • sets strict: true on the serialized tool definition
  • strips extended JSON Schema validation keywords (minimum, maximum, pattern, etc.) from the tool's input_schema, which strict schemas reject — same treatment as the existing response_schema path
  • adds the structured-outputs-2025-11-13 beta header when any tool in the request is strict

Tools without the option are serialized exactly as before (no strict key, 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

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>
epatey added 3 commits July 13, 2026 09:04
… 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.
@epatey

epatey commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

@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 --runapi + ANTHROPIC_API_KEY, so not run in the normal CI — please run them yourself):

pytest tests/model/test_extended_json_schema.py -k anthropic_strict --runapi

I've pushed two test commits:

  1. Failing unit test: strict schemas require additionalProperties: false on every object. The top level gets it from the ToolParams default, but nested object params omit it.
  2. Live tests: flat strict tools pass (stripping, beta header, and the thinking case all work ✅); nested-object strict tools fail with 400: "For 'object' type, 'additionalProperties' must be explicitly set to false".

The fix should mirror the response_schema path: deep-copy tool.parameters and run set_additional_properties_false() on it when strict.

One design question: options["strict"] is a bare key, but existing provider options are namespaced (options["openai"], options["grok"], options["mistral"], …). Should this be options["anthropic"] = {"strict": True}, or documented as a cross-provider generic? As-is, other providers silently ignore it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Anthropic provider: no way to set strict: true on tool schemas (needed for reliable tool calls with adaptive thinking)

2 participants