Lesson 17 of 33 ยท Part 1: API Basics

When Things Go Wrong: Errors & Retries

Catch API errors, wait and retry, and give friendly messages instead of crashes.

Last time: A few example user/assistant pairs (few-shot) taught the AI a new task and answer style.
Today: Our program learns to catch errors, wait, retry the right ones, and give friendly messages.
Source Code
1# part1/errors_retries.py
2import time
3from openai import OpenAI, RateLimitError, APIConnectionError, AuthenticationError
4
5client = OpenAI(max_retries=0)  # we handle retries ourselves in this lesson
6
7def ask(question, tries=3):
8    for attempt in range(1, tries + 1):
9        try:
10            response = client.chat.completions.create(
11                model="gpt-4o-mini",
12                messages=[{"role": "user", "content": question}],
13            )
14            return response.choices[0].message.content
15
16        # A wrong key stays wrong, so trying again won't help.
17        except AuthenticationError:
18            return "Oops! The API key was not accepted. Check OPENAI_API_KEY in your .env file."
19
20        # 429 = "too many requests right now"... or "no credit left".
21        except RateLimitError as error:
22            if error.code == "insufficient_quota":
23                return "Your account is out of credit. Add some on the billing page."
24            print("The server is busy (too many requests).")
25
26        # No internet, or the server can't be reached.
27        except APIConnectionError:
28            print("Can't reach the server. Is the internet on?")
29
30        # Still here? It may fix itself: wait, then retry.
31        if attempt < tries:
32            wait = 2 ** (attempt - 1)  # 1s, 2s, 4s... doubles every time
33            print(f"Waiting {wait}s before try {attempt + 1}...")
34            time.sleep(wait)
35
36    return "Sorry, the AI is not answering right now. Please try again later."
37
38print(ask("Give me a fun fact about penguins."))

No output yet...

โ† Prev
1/0
Next: Challenge โ†’