Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback Model Support #516

Open
sydney-runkle opened this issue Dec 20, 2024 · 6 comments
Open

Fallback Model Support #516

sydney-runkle opened this issue Dec 20, 2024 · 6 comments
Assignees
Labels
new models Support for new model(s)

Comments

@sydney-runkle
Copy link
Member

sydney-runkle commented Dec 20, 2024

To mitigate issues with unpredictable LLM downtime: we'd like to add a new model that supports a list of models to use as fallbacks if the first doesn't work.

@sydney-runkle sydney-runkle added the new models Support for new model(s) label Dec 20, 2024
@samuelcolvin samuelcolvin added the good first issue Good for newcomers label Dec 20, 2024
The-CodeINN added a commit to The-CodeINN/pydantic-ai that referenced this issue Dec 23, 2024
Fixes pydantic#516

Add support for fallback models in the `Agent` class.

* **Agent Class Changes:**
  - Add `fallback_models` attribute to store a list of fallback models.
  - Modify `run`, `run_sync`, and `run_stream` methods to iterate through `fallback_models` if the primary model fails.
  - Update `_get_model` method to handle fallback models.

* **Model and AgentModel Classes Changes:**
  - Add `fallback_models` attribute to `Model` and `AgentModel` classes.

* **Tests:**
  - Add tests in `tests/test_agent.py` to verify the functionality of fallback models.
@samuelcolvin
Copy link
Member

samuelcolvin commented Jan 2, 2025

I think the API can be as Simple as

model = FallbackModel('openai:gpt-4o', 'claude-3-5-haiku-latest', GeminiModel(...))
agent = Agent(model, ...)

E.g. the signature of FallbackModel is something like

class FallbackModel:
    def __init__(self, default_model: models.Model | models.KnownModelName, *fallback_models: models.Model | models.KnownModelName):
        ...

@samuelcolvin samuelcolvin removed the good first issue Good for newcomers label Jan 2, 2025
@phil65
Copy link

phil65 commented Jan 3, 2025

Implemented a pydantic-ai FallbackModel some time ago here, in case there is interest:
https://github.com/phil65/LLMling-models
Cheers!

@samuelcolvin
Copy link
Member

Another advantage of having a consistent way to detect 503 etc. responses, is we can use it to mark live tests as xfail when the underlying model fails.

@phil65
Copy link

phil65 commented Jan 7, 2025

It´s actually quite funny what kind of things all can be done at model level. I now added a model to
to stream from another remote pydantic-ai model using FastAPI . 😄 (code might be sub-optimal though, just learning FastAPI a bit)

@sydney-runkle sydney-runkle self-assigned this Jan 8, 2025
@sydney-runkle
Copy link
Member Author

Will be starting work on this later today. PR to come soon 👍

@sydney-runkle
Copy link
Member Author

Implemented a pydantic-ai FallbackModel some time ago here, in case there is interest:
https://github.com/phil65/LLMling-models
Cheers!

Awesome, thanks so much @phil65!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new models Support for new model(s)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants