Skip to content

Add "Circuit Breaker" to Fallback model #3023

@ChuckJonas

Description

@ChuckJonas

Description

The Fallback model allows for requests to be sent to different model on the event of a request failure.

However, in the event the model is temporarily down (service outage or rate-limiting), pydantic-ai will continue to retry the failing model first on every request.

It would be nice if there were a built in way to "time out" a model (potentially using a circuit breaker pattern or just something simple):

def handle_fallback(e: Exception) -> bool:
    fallback = isinstance(e, (ModelHTTPError, ThrottlingException))
    return fallback

def curcuit_breaker(model, e: Exception):
   if isinstance(e, ThrottlingException)
     return {"state": "open", "expires": e.retry_after}

default_model = FallbackModel(
    primary_model, fallback_model, fallback_on=handle_fallback, circuit_breaker=curcuit_breaker
)

If a model state was set to "open" it would effectively be removed from the FallbackModel sequence until the expires.

A less opinioned interface might just be to allow for a enabled(model) -> boolean hook check before selecting the model. That would give the user flexibility to implement with whatever strategy they choose.

References

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions