Warn when `FallbackModel` response handlers are skipped on a streamed request

Merged
DO
2mo ago
+66-3
  • Refs #4140 (doesn't close it: response handlers still don't run on streams)

Why

FallbackModel response handlers in fallback_on only run on non-streamed requests, and on a streamed request they were skipped silently. Streaming isn't only run_stream(): agent.run() streams under the hood once anything consumes the run's events, such as an event_stream_handler, an agent.on_event listener or a Hooks event hook. So adding an event listener silently turned off response-based fallback:

fallback = FallbackModel(primary, backup, fallback_on=reject_bad_response)
agent = Agent(fallback)
await agent.run('hi')                                # bad response rejected, backup answers
await agent.run('hi', event_stream_handler=handler)  # bad response accepted, no signal

Applying the handlers to streams is a design question: buffer the stream, or emit and then retract it. #5321 and #9348 are discussing it. This PR does the minimum that is clearly right: it makes the gap visible.

New public surface

None.

User-visible behavior

When fallback_on includes a response handler, FallbackModel.request_stream() now emits a UserWarning saying the handlers aren't applied and naming the ways a request ends up streamed. It doesn't warn on a pinned continuation, because non-streamed request() doesn't apply response handlers there either. The "Non-streaming only" note in docs/models/overview.md now lists every way a request is streamed, not only run_stream().

Verification

  • test_response_handler_not_applied_to_streamed_requests_warns in tests/models/test_fallback.py: plain run() rejects the response. run() with an event_stream_handler, run() with an on_event listener, and run_stream() each warn and accept it.

What changes for existing users

Users who stream with a FallbackModel response handler configured now get a UserWarning. Nothing changes for anyone else.

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.
  • PR title is fit for the release changelog.

šŸ¤– Generated with Claude Code