Start learning free

LESSON 107 OF 129 · AI ENGINEERING: PRODUCTION

Fall Back to Another Model in Python

Practice

Sometimes a model is overloaded: it answers status 529 to every call, and it can stay that way for minutes. Retrying doesn't help then. But a model is not the only model: each one runs on its own, and when one is overloaded another is often fine. Answering with a second model when the first one can't is called a fallback.

The shape is always the same: try the first choice, and when it fails, try the second. Here it is with two services that give an exchange rate:

try:
    rate = get_rate("bank-a")
except TimeoutError:
    rate = get_rate("bank-b")

Two details matter. The second choice can fail too, so catch that as well and keep an apology for the case when both fail. And fall back only when the model is the problem: status 400 says the request is wrong, and a wrong request is wrong for every model.

Four customers write to the shop. The main model answers the first one and is overloaded for the second. Both models are overloaded for the third. The fourth sends an empty message. Your program must print this (the last line comes from the lesson's check and lists the models your program asked):

Answered by claude-haiku-5-5
Yes, we ship to Canada. It costs a flat $15 and takes 7 to 10 business days.
claude-haiku-5-5 failed with status 529
Answered by claude-sonnet-5-5
Lamps have a 2-year warranty, teapots and mugs a 1-year warranty.
claude-haiku-5-5 failed with status 529
claude-sonnet-5-5 failed with status 529
Sorry, we are busy right now. Please try again in a minute.
claude-haiku-5-5 failed with status 400
Sorry, we could not answer that question.
Models asked, in order: haiku, haiku, sonnet, haiku, sonnet, haiku

Your task: the program already asks claude-haiku-5-5 and apologizes when the call fails. Add a second model, claude-sonnet-5-5, that gets the same question when the main model answers 529. Apologize only when both models fail, print which model wrote each answer, and don't fall back when the status is not 529.


Assignment
Follow the Coding Tutorial to add the fallback model, catch its errors, print the model that answered and skip the fallback for a wrong request.


Hint
The second attempt goes after the first try/except and before the last return. Every reply names its model in reply.model, and every error carries its status in error.status_code.


STEP 1 OF 8
    Ctrl + Enter to check

    Ai Fallback Model in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free