Raise `ModelSchemaTooComplexError` for Anthropic's "Schema is too complex" 400, and attempt an inferred Native Output schema again in Tool Output

Merged
DO
8d ago
+1091-29

Anthropic compiles a Native Output schema into a grammar and can reject one that's within its documented limits with a 400 "Schema is too complex", after up to minutes of compiling. A bare structured output_type hits this whenever the request thinks, since Anthropic then infers Native Output. I reproduced it live on that path with a 13-field model: Claude Sonnet 5 (thinks by default), Haiku 4.5 with thinking=True, and Opus 5.5 each returned the 400 after about 50 seconds.

The run now recovers from this by itself, with nothing to add:

  • Typed error: AnthropicModel (direct API and Bedrock client) and BedrockConverseModel for Claude raise this 400 as a new ModelSchemaTooComplexError, a ModelRequestRejectedError that's also a ModelHTTPError, so except ModelHTTPError and status_code keep working. It's also accepted by ModelFallback's fallback_on in agent specs.
  • Attempted again in Tool Output: when the rejected attempt used Native Output that the model inferred, the run sends the request again in Tool Output on the same model, within the same step, through the attempt loop from #9348. The output tool isn't strict, so Anthropic doesn't compile it. The rejected attempt is recorded in failed_attempts, and the model never sees it.
  • Kept for the run: the run keeps Tool Output for that model for its later requests, by updating the output mode it settles on per model. That mode only moves from Native to Tool Output, so a request is attempted again this way at most once. Nothing is remembered across runs; the docs suggest wrapping a type that's known to be too complex in ToolOutput to skip the wait.
  • Before ModelFallback: the switch happens before any on_model_request_error hook, so ModelFallback only moves to another model once Tool Output on the same model has failed too.
  • Left alone: an output mode you chose, like NativeOutput(...), still raises the error, as does a Tool Output attempt that's rejected too.
  • Streaming: a streamed request whose output mode the model infers as Native Output now opens its stream before handing it over, as a stream already does when a capability handles request errors, so the rejection can be caught.
  • Durable execution: the error stays non-retryable in a Temporal activity, and the Tool Output attempt runs as a new activity. Covered with Temporal and DBOS.

The live tests record the rejected Native Output request and the Tool Output request that succeeds after it, on Sonnet 5 and on Bedrock Converse with Haiku 4.5. The other branches are covered offline with FunctionModel.

This replaces the opt-in OutputModeFallback capability this PR first added, per review.

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
  • PR title is fit for the release changelog.

šŸ¤– Generated with Claude Code