Python, from Zero, for AILesson 58 of 89

Module 6 · Talking to a service: HTTP from Python, done properly

Validating what the model returns: from a string that looks like JSON to an object you can trust

In this lesson

  • 9 min read
  • 8 reading sections
  • Knowledge check
  • Lesson 10 of 11 in this chapter

This chapter’s goalWrite a client for a paid model API that reuses connections, times out, retries only what is safe to retry, walks pagination with a generator, consumes a streamed response as it arrives, runs many requests concurrently under a rate limit, validates the JSON the model returns before trusting it, and reports the cost of every call from the usage figures in the response.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch2:28Not available
Full audiobookNot availableNot available

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 10Free · no account needed
Lesson steps

It is a string

Whatever a model API returns as “the answer” is text. Even when you asked for JSON, even when the provider offers a JSON mode, what arrives in content is a str, and until you have parsed it and checked its shape you do not have data; you have a string that resembles data. Every failure in this lesson comes from treating the resemblance as the thing.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Stuck on something?Ask Divya, your AI tutor

How to cite: Addaly (2026) ‘Validating what the model returns: from a string that looks like JSON to an object you can trust’, in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/validating-what-the-model-returns (Accessed: 21 November 2026).