Raise right away when the token limit cuts off `NativeOutput` or `PromptedOutput` text that fails validation, instead of retrying
Posted by Claude Code on Douwe's behalf.
Part of #12399
Why
NativeOutput/PromptedOutput JSON cut off at max_tokens (finish_reason='length') failed validation, so run() re-prompted the same model with the same limit until the output retries ran out (Exceeded maximum output retries), and run_stream() raised Output validation failed during streaming.... Neither error named the token limit. Precedent: #3504, where a length response with no output stopped re-prompting.
New public surface
None. AgentStream gets a private _max_tokens_getter field.
User-visible behavior
agent = Agent(model, output_type=NativeOutput(Cities), model_settings={'max_tokens': 20})
await agent.run('List the 20 largest cities in the world.')
-# 2 requests, then UnexpectedModelBehavior: Exceeded maximum output retries (1)
+# 1 request, then UnexpectedModelBehavior: Model token limit (20) exceeded while generating structured output,
+# resulting in output that failed validation. Increase the `max_tokens` model setting, or simplify the prompt ...
The error is chained to the ValidationError, in run(), iter() and run_stream(). Unchanged: cut-off JSON that still validates is the output, an output validator's ModelRetry is still retried, and plain text output that was cut off is still returned. A new docs/output.md section says how to detect that (result.response.finish_reason == 'length'). docs/troubleshooting.md gets an entry for the Model token limit (...) exceeded errors.
Verification
tests/models/test_openai.py::test_openai_native_output_cut_off_by_max_tokens[False|True]: recordedgpt-4.1-miniresponses cut off atmax_tokens=20. Each cassette has a single request.tests/test_agent.py:test_truncated_structured_output_raises_without_retry(native and prompted, one model call),test_structured_output_that_validates_is_accepted_despite_finish_reason_length,test_output_validator_retry_with_finish_reason_length_is_retried,test_truncated_text_output_is_returned.
What changes for existing users
Cut-off structured text output that fails validation now raises right away, with a message naming the token limit, instead of after the output retries. It is still UnexpectedModelBehavior. A follow-up PR adds OutputTokenLimitExceeded; whichever of the two PRs lands second switches this raise to it.
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
Summary by cubic
NativeOutput/PromptedOutput text cut off at max_tokens (finish_reason='length') into JSON that fails parsing now raises UnexpectedModelBehavior immediately, naming the token limit, instead of re-prompting the model with the same limit until the output retries run out.
The error is raised in run(), iter(), and run_stream(), and is chained to the ValidationError. The immediate raise is limited to JSON the token limit left incomplete: cut-off output that parses is handled as usual ā accepted when valid, retried when the schema or an output validator rejects it or raises ModelRetry, and a TextOutput function's own ValidationError keeps its retry even when cut off. Cut-off plain text output is still returned. Tool output cut off mid-arguments is still re-prompted, surfacing as IncompleteToolCall. docs/output.md and docs/troubleshooting.md now explain how to detect the cut-off (result.response.finish_reason == 'length').