Sub topic 1
What a Provider SDK Gives You
Prerequisites
Assume you know basic Python, JSON, HTTP APIs, and have called a chat model before. You should also understand that retrieval-augmented generation uses retrieved text to give a model additional context.
What a provider SDK gives you
A provider SDK is a programming library for one model provider. It handles authentication, HTTP requests, request serialization, response parsing, and common API errors.
You still choose the model and construct the conversation. The SDK does not decide how to split documents, run tools, retry failed steps, or coordinate multiple model calls.
from openai import OpenAI
client = OpenAI() # Reads OPENAI_API_KEY from the environment.
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "user", "content": "Explain an SDK in one sentence."}
],
)
print(response.choices[0].message.content)The direct call is easy to inspect. You can see the exact messages, model, and returned value.
Where a bare SDK starts to hurt
As an application grows, the same low-level work appears repeatedly:
- Validating model output
- Describing and dispatching tools
- Retrying failed calls
- Preserving conversation state
- Connecting retrieval to generation
- Recording traces and token usage
- Supporting more than one provider
A framework packages some of these patterns. That reduces application code, but adds abstractions and dependencies. Debugging may require understanding both the framework and the provider API.
Structured output and validation with Pydantic
Structured output means asking the model for data with a known shape instead of free-form text. Pydantic is a Python library that validates data against a model you define.
from openai import OpenAI
from pydantic import BaseModel
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": "Return JSON with category and urgency."},
{"role": "user", "content": "The site is down for everyone."},
],
)
ticket = Ticket.model_validate_json(
response.choices[0].message.content
)
print(ticket)The SDK requests JSON, while Pydantic checks the fields and types. Your code must handle validation failures.
Tool calling with the SDK
Tool calling lets the model return a request for your function. The SDK does not run that function. Your application validates the arguments, executes the function, and sends the result back.
Exercise
Who executes a function requested through provider SDK tool calling?
Show answer
Your application executes it, checks its arguments, and returns the result to the model.