Python, from Zero, for AILesson 54 of 89

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

A provider’s SDK: what it does for you, what it hides, and how to read it

In this lesson

  • 8 min read
  • 7 reading sections
  • Knowledge check
  • Lesson 6 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 9Free · no account needed
Lesson steps

Two ways to make the same call

With requests:

Python
r = session.post(
    "https://api.openai.com/v1/chat/completions",
    headers={"Authorization": f"Bearer {key}"},
    json={"model": "gpt-4o-mini", "messages": [{"role": "user", "content": "Hi"}]},
    timeout=(5, 120),
)
r.raise_for_status()
text = r.json()["choices"][0]["message"]["content"]

With the provider’s SDK:

Python
from openai import OpenAI

client = OpenAI()          # reads OPENAI_API_KEY from the environment
resp = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hi"}],
)
text = resp.choices[0].message.content

The second is shorter, and it is worth being exact about what the shortness bought.

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) ‘A provider’s SDK: what it does for you, what it hides, and how to read it’, in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/sdks-versus-raw-requests (Accessed: 21 November 2026).