Skip to content
IT Vizag
← Python Meets AI

▶ Sign up free for the video, slides & prompts

The lesson below is open to all. Enter your email once to unlock every video, slide deck, prompt and code example on this device.

Checking…

Checking AI output in code

Lesson 8 of 10 · 2 min read

Validate answers and handle failures and retries.

🎬 Video version coming soon — read it now

AI is powerful but not reliable in the way 2 + 2 is reliable. Production-quality AI code expects failures: network errors, rate limits, odd answers. It checks, and it recovers.

Network and API errors

The SDK already retries temporary problems such as dropped connections, rate limits (HTTP 429) and server errors (5xx), with increasing waits between tries, twice by default. You can raise that:

🔒 Code example: sign up free (above) to unlock it.

Then catch what's left, most specific first:

🔒 Code example: sign up free (above) to unlock it.

These are the try/except skills from Functions & Clean Code, applied to real-world failures. Use the SDK's exception classes; don't match on error message text.

Check why it stopped

🔒 Code example: sign up free (above) to unlock it.

Validate the content

Even a well-formed answer can be wrong for your purpose. Add checks in code:

🔒 Code example: sign up free (above) to unlock it.

For extracted facts, compare against the source where you can. If the model says the job is in "Vizag", does the post actually mention Vizag?

🔒 Code example: sign up free (above) to unlock it.

Humans in the loop

For anything that affects people, such as grades, hiring, medical or money decisions, AI output should be reviewed by a person. Code can route the uncertain cases: anything flagged goes to a "check" list instead of being acted on automatically.

Keep a log

🔒 Code example: sign up free (above) to unlock it.

When a user says "the bot told me something wrong", the log lets you find out exactly what happened.

Tip: Write down 10 test inputs, including tricky ones, and their correct answers. Re-run them every time you change the prompt or model. That's a mini evaluation suite.

In one line

Let the SDK retry temporary errors, catch the rest by type, check stop_reason, validate answers in code, and route uncertain cases to a human.

Saved on this device. Sign in to keep progress everywhere.

Share with a friend