Skip to content

fix(anthropic): JSON mode never forces a tool the newer Claude models refuse - #38

Closed
pseudotensor wants to merge 2 commits into
h2o-mainfrom
h2o/claude-structured-output-no-forced-tool
Closed

pseudotensor wants to merge 2 commits into
h2o-mainfrom
h2o/claude-structured-output-no-forced-tool

Conversation

@pseudotensor

Copy link
Copy Markdown
Collaborator

Claude Opus 5.5, and upstream says Fable 5.1 too, thinks on every request and refuses a forced tool:

400 tool_choice: type "tool" and "any" are not supported for this model.

This fork picks native structured output from a hard-coded name list that stops at opus-4-7 / sonnet-4-6. So JSON mode (response_format with a schema) on every newer Claude falls back to the forced JSON tool, and that request is refused. h2oGPTe CI hit exactly this error on 2026-09-29: 81 refusals from claude-opus-5-5 in one smoke run. The refused JSON sub-calls came back {} after two retries.

What changes

Everything below is taken from upstream LiteLLM main, where these fixes already landed.

  • Cherry-pick of upstream fix(anthropic): use native output capability BerriAI/litellm#33235. Native structured output is chosen from the model map's supports_native_structured_output, not a name list.
  • Model map, both copies:
    • Upstream's entries for claude-opus-5-5, claude-opus-5, claude-fable-5-1 and claude-mythos-5. Prices are upstream's. Fields this fork's code doesn't read are left out, so the map schema is unchanged apart from one new key.
    • supports_native_structured_output: true on claude-sonnet-5, claude-fable-5 and claude-haiku-4-5(-20251001), as upstream marks them.
    • supports_forced_tool_use: false on claude-opus-5-5 and claude-fable-5-1, as upstream marks them.
  • AnthropicModelInfo.forced_tool_use_unsupported. It is true only when the map explicitly says supports_forced_tool_use: false. JSON mode on such a model offers the JSON tool without forcing it. An unmapped model keeps today's behaviour.
  • _apply_forced_tool_choice. A caller's own forced tool_choice (required or a named tool) on such a model is downgraded to auto under drop_params, with a warning. Without drop_params it's refused client-side with a 400 that says what to do. This matches upstream.

Tests

New file: tests/test_litellm/llms/anthropic/chat/test_claude_json_mode_never_forces_a_tool_it_refuses.py. It covers:

  • native output with no forced tool for the eight newer models;
  • the two models mapped as refusing forced tools;
  • the guard alone, on a model with no native output;
  • the legacy path, unchanged for a model with neither flag;
  • the caller downgrade, the refusal, and the unchanged cases.

The model-map schema in tests/test_litellm/test_utils.py gains supports_forced_tool_use.

Local results:

  • Tests: 1754 passed across tests/test_litellm/llms/anthropic/, test_utils.py, test_claude_opus_4_8_config.py and tests/test_litellm/llms/bedrock/chat, run with LITELLM_LOCAL_MODEL_COST_MAP=True.
  • Environment-only failures: the Bedrock files tests and one Vertex partner test fail on unmodified h2o-main too, because vertexai isn't installed on that machine.
  • Lint: ruff check is clean on the changed code, and scripts/ruff_strict_gate.py --base h2oai/h2o-main passes.

Scope

  • This fixes the LiteLLM path. That covers h2oGPTe models configured through LiteLLM, and the router image that pins this fork (LITELLM_FORK_SHA, bumped after merge).
  • It does not fix h2oGPTe's CI failures on its own. Those come from h2oGPT's native inference_server: anthropic path, which forces tool_choice={"type":"tool","name":"JSON"} itself (h2ogpt_internal src/gpt_langchain.py). That's a separate fix.

🤖 Generated with Claude Code

krrish-berri-2 and others added 2 commits September 28, 2026 23:57
* fix(anthropic): route native structured output

Use model capability metadata so new native structured-output models do not require transformation allowlist changes.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(anthropic): pass provider to capability

Co-authored-by: Cursor <cursoragent@cursor.com>

* test(anthropic): cover dotted model IDs

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(anthropic): handle remote capability lag

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
(cherry picked from commit 477ef3a)
… refuse

Claude Opus 5.5 (and Fable 5.1) think on every request and refuse a forced tool --
`400 tool_choice: type "tool" and "any" are not supported for this model`. Measured in h2oGPTe CI
(2026-09-29): 81 such refusals in one smoke run, every JSON-mode sub-call (query rewrite,
classification) returning `{}` after two retries.

On top of the cherry-picked upstream BerriAI#33235 (native structured output chosen by the model map's
`supports_native_structured_output`, not a name list that stopped at opus-4-7 / sonnet-4-6):

* model map (both copies): the entries upstream has for claude-opus-5-5, claude-opus-5,
  claude-fable-5-1 and claude-mythos-5 -- prices unchanged from upstream, and without the fields
  this fork's code does not read -- and `supports_native_structured_output: true` on
  claude-sonnet-5, claude-fable-5 and claude-haiku-4-5(-20251001), as upstream marks them.
  `supports_forced_tool_use: false` on opus-5-5 and fable-5-1, as upstream.
* `AnthropicModelInfo.forced_tool_use_unsupported`: an explicit `supports_forced_tool_use: false`.
  JSON mode on such a model offers the JSON tool without forcing it.
* `_apply_forced_tool_choice`: a caller's own forced tool_choice on such a model is downgraded to
  `auto` under drop_params (with a warning), and otherwise refused client-side with a 400 that says
  how -- as upstream does.

Tests: tests/test_litellm/llms/anthropic/chat/test_claude_json_mode_never_forces_a_tool_it_refuses.py
(native output for the eight newer models, the guard alone, the unchanged legacy path, the caller
downgrade and refusal); the model-map schema gains `supports_forced_tool_use`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pseudotensor

Copy link
Copy Markdown
Collaborator Author

Superseded by #40, which syncs h2o-main to upstream v1.99.4 and carries this fix on top.

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.

2 participants