Sub topic 2
Where a bare SDK starts to hurt
This chapter assumes you can write basic Python, have called a chat completion API at least once, and know what JSON and HTTP APIs are. It covers where calling a provider SDK directly becomes tedious or risky. Later chapters show how frameworks address each problem.
What the SDK does well
A provider SDK is a client library for one vendor's API. It handles authentication, request formatting, retries on some errors, and response parsing. For a single call, it is hard to beat:
from openai import OpenAI
client = OpenAI() # reads OPENAI_API_KEY from the environment
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "Explain what an API client library is in one sentence."},
],
)
print(response.choices[0].message.content)Nothing is hidden here. The pain starts when the application grows past one call.
Pain point 1: boilerplate
Every feature around the call is your code. The SDK does not do these things for you:
- Keep conversation history. You append each message to a list and resend the whole list on every turn.
- Trim history when it nears the context window limit.
- Parse and validate the model's reply when you need more than free text.
- Run tools and feed results back to the model.
- Load, split, embed, and search documents for retrieval.
Each item is a small amount of code. Together they become a hand-built mini-framework that you must test and maintain. Teams often find the same helper functions copied across several services.
Pain point 2: provider lock-in
Message shapes, parameter names, and response objects differ between vendors. The code above depends on client.chat.completions.create, on response.choices[0].message.content, and on OpenAI-style roles. Many other providers offer OpenAI-compatible endpoints, which reduces the problem. Compatibility is rarely complete, though. Tool-calling formats, JSON modes, and streaming events often differ in small ways.
Switching providers therefore means touching every call site. The more the SDK's types leak into your business logic, the more that switch costs.
Pain point 3: unreliable output
The model returns text. If your code needs a category and an urgency number, you must ask for JSON, parse it, and check it. JSON mode guarantees syntactically valid JSON. It does not guarantee the fields you wanted:
from openai import OpenAI
from pydantic import BaseModel, ValidationError
class Ticket(BaseModel):
category: str
urgency: int
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4o-mini",
response_format={"type": "json_object"},
messages=[
{"role": "system", "content": "Reply with JSON like {\"category\": \"billing\", \"urgency\": 3}. Urgency is an integer from 1 to 5."},
{"role": "user", "content": "The site is down for all customers."},
],
)
try:
ticket = Ticket.model_validate_json(response.choices[0].message.content)
except ValidationError as err:
ticket = None # a real app would retry or report the error
print(err)
print(ticket)The ValidationError branch is the part people forget. Note that Ticket also accepts urgency=9, because nothing enforces the 1 to 5 range yet. Retry logic, repair prompts, and range checks are all yours to write.
Pain point 4: debugging and testing
With a bare SDK you see exactly what you send, which is an advantage. But once you add history trimming, tool loops, and retries, you need your own logging to see the final prompt. Tests also need a fake client, since each test otherwise makes a paid network call.
Summary of the costs
| Problem | What you write yourself | Risk if you skip it |
|---|---|---|
| History management | Message list, trimming | Context overflow errors |
| Output validation | Parsing, retries | Crashes on malformed replies |
| Provider differences | Adapter layer | Rewrites when switching vendors |
| Tool and retrieval plumbing | Loops, chunking, search | Duplicated, inconsistent code |
Exercise
Your service calls the SDK in 12 places, and each call reads response.choices[0].message.content. Name two costs you will pay if you must move to a provider with a different response shape.
Show answer
First, you must edit every call site, or write a wrapper after the fact. Second, you must retest each feature, because differences in tool-calling, JSON mode, and streaming behavior can change results. Wrapping the call in one function early would have limited both costs.