Add a `first_event_timeout` model setting so a stream that never starts can fall back
- Closes #11574
What
ModelSettings.first_event_timeout: a deadline, in seconds, from sending a streamed request until its first Pydantic AI stream event (a part start or delta, thinking included), enforced by Pydantic AI for every model. When it runs out, the request is cancelled and ModelRequestTimeout is raised. That lets a stream that never starts fall back, while long reasoning keeps running once output has begun. Non-streamed requests aren't affected.
[!WARNING]
Compatibility impact: Code that matches exhaustively on ModelSettingName, a Literal of every ModelSettings field name, now sees one more name, 'first_event_timeout', so a type checker may flag a match or a mapping keyed by it as incomplete.
Why this can ship in a minor release: it only adds a name for a new optional setting; every existing name and its meaning are unchanged, as when citations was added.
Migration: handle 'first_event_timeout' wherever you match on every ModelSettingName.
How
The deadline sits in the same RequestDeadline that enforces request_timeout (models/_request_timeout.py):
enforce()cancels at whichever deadline passes first.iterate()pulls the stream's first event under both deadlines, and the rest underrequest_timeoutonly.
Because this is enforced on the stream itself, it applies:
- Without fallback: a plain agent gets
ModelRequestTimeouttoo, including on a lazily opened stream with no hooks. - With fallback:
FallbackModelandModelFallback(whose primed stream opens under the deadline) fall back on it by default, and each candidate gets a fresh deadline. - Across retries: provider SDK and transport retries are covered, because opening the stream happens under the deadline.
- On continued responses: a paused or background response that hasn't produced output yet keeps the same deadline across the continuation chain.
FallbackModelcarries it on the suspended response's metadata next torequest_deadline, asNoneonce output exists. - On Pydantic AI events only: raw provider events such as OpenAI's
response.createddon't count.
For durable execution, a streamed segment is consumed inside the activity or step, so enforcing this on the workflow side would time out healthy long responses. The workflow side keeps request_timeout only. The durable unit enforces the first-event deadline and carries it across segment units. The deprecated TemporalAgent/DBOSAgent/PrefectAgent models get the same treatment.
ModelRequestTimeout gains setting: Literal['request_timeout', 'first_event_timeout'], defaulting to 'request_timeout'. It's pickled through __reduce__, so it survives the Temporal error transport. The message reads "...timed out after N seconds without producing any output".
Decisions for review
ModelRequestTimeout.settingis a field rather than a subclass, to keep new public surface small.- When the clock starts: where
request_timeout's does, when the request starts. On a lazily opened stream, time the consumer takes before its first pull counts, as it does forrequest_timeout. - Under Temporal, a first-event timeout inside an activity is retried by the activity's retry policy, like other model errors. The docs say to list
ModelRequestTimeoutinnon_retryable_model_errorsto fall back straight away. The alternative is making it non-retryable by default. - The deprecated durable wrapper agents' models are updated too, so the setting doesn't kill long streams there.
API compatibility
ModelSettingName gains 'first_event_timeout'. It has a waiver against v2.63.0 in .github/api-compatibility-allowlist.json, the same kind as the citations addition (#7434), and every existing name is unchanged.
Tests
tests/test_request_timeout.py, using the moved-clock pattern, covers:
- a plain agent timing out before its first event (through
run_streamandevent_stream_handler), including the message, the field and a pickle round trip; - a long gap after the first event not timing out;
- non-streamed requests being unaffected;
direct.model_request_stream;- interplay with
request_timeout, in three cases; FallbackModelandModelFallbackfalling back;- paused and background continuations, with and without
FallbackModel; - SDK retries being covered;
- OpenAI
response.creatednot counting; - in-process durable units carrying the deadline across segments.
There's also a real Temporal workflow test in tests/durable_exec/temporal/test_durability.py.
Docs: a "Waiting for the first event" section in docs/timeouts.md, written around the motivating scenario (Bedrock degraded with slow first tokens, falling back to OpenAI after 20 s). There are also notes in docs/retries.md, docs/models/overview.md and docs/durable_execution/temporal.md, plus the agent skill.
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver. (Additive
ModelSettingNameentry only; waiver included.) - PR title is fit for the release changelog.
š¤ Generated with Claude Code