fix: enable failover during stream iteration#279
Open
kachelle wants to merge 2 commits intolaravel:0.xfrom
Open
fix: enable failover during stream iteration#279kachelle wants to merge 2 commits intolaravel:0.xfrom
kachelle wants to merge 2 commits intolaravel:0.xfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Streaming failover doesn't work because
withModelFailover()only catchesFailoverableExceptionduring the synchronousStreamableAgentResponsecreation, not during the lazy generator iteration where HTTP errorsactually occur.
Problem
Promptable::stream()callswithModelFailover(), which wraps a try-catch around:This succeeds instantly because StreamableAgentResponse uses a lazy generator - no HTTP call is made until the stream is iterated. By the time a provider returns
429or529(during foreach iteration), execution isoutside the failover try-catch.
Passing provider:
[Lab::Mistral, Lab::Anthropic]tostream()never actually fails over.Solution
When multiple providers are configured,
stream()now builds aStreamableAgentResponsewith a failover-aware generator. The generator uses yield from to iterate each provider's stream, catchingFailoverableExceptionduring iteration and trying the next provider.
Single-provider calls use a direct path with no wrapping overhead.
Tests
test_stream_fails_over_to_next_provider_when_primary_is_rate_limited- verifies failover works andAgentFailedOverevent is dispatchedtest_stream_throws_last_exception_when_all_providers_fail- verifies the last exception is thrown when no provider succeedsThis is only my 2nd PR to this project, happy to adjust the approach if needed.