| layout | default |
|---|---|
| title | Chapter 4: Agents and Assistants |
| nav_order | 4 |
| parent | OpenAI Python SDK Tutorial |
Welcome to Chapter 4: Agents and Assistants. In this part of OpenAI Python SDK Tutorial: Production API Patterns, you will build an intuitive mental model first, then move into concrete implementation details and practical production tradeoffs.
This chapter focuses on transition strategy: operate existing assistants safely while moving toward current agent-platform patterns.
- Assistants API is still usable in many systems.
- OpenAI platform docs indicate a target sunset timeline around August 26, 2026.
- New projects should evaluate Responses API + Agents patterns first.
from openai import OpenAI
client = OpenAI()
assistant = client.beta.assistants.create(
model="gpt-5.2",
name="Ops Assistant",
instructions="Help with reliability planning and incident response."
)
thread = client.beta.threads.create()
client.beta.threads.messages.create(
thread_id=thread.id,
role="user",
content="Draft a rollback checklist for a risky deployment."
)- catalog Assistants API usage and tool dependencies
- extract shared prompt/tool contracts
- rebuild core flows on Responses/Agents primitives
- run side-by-side output comparisons
- cut over service by service
- avoid broad rewrites in one release
- pin SDK versions per service
- keep rollback path to known-good behavior
- monitor quality regressions with fixed eval sets
You can now manage assistant-era systems while executing a controlled migration plan.
Next: Chapter 5: Batch Processing
The DynamicValue class in examples/parsing_tools.py handles a key part of this chapter's functionality:
class DynamicValue(BaseModel):
column_name: str
class Condition(BaseModel):
column: str
operator: Operator
value: Union[str, int, DynamicValue]
class Query(BaseModel):
table_name: Table
columns: List[Column]
conditions: List[Condition]
order_by: OrderBy
client = OpenAI()
completion = client.chat.completions.parse(
model="gpt-4o-2024-08-06",
messages=[
{
"role": "system",
"content": "You are a helpful assistant. The current date is August 6, 2024. You help users query for the data they are looking for by calling the query function.",
},
{
"role": "user",
"content": "look up all my orders in november of last year that were fulfilled but not delivered on time",
},This class is important because it defines how OpenAI Python SDK Tutorial: Production API Patterns implements the patterns covered in this chapter.
The Condition class in examples/parsing_tools.py handles a key part of this chapter's functionality:
class Condition(BaseModel):
column: str
operator: Operator
value: Union[str, int, DynamicValue]
class Query(BaseModel):
table_name: Table
columns: List[Column]
conditions: List[Condition]
order_by: OrderBy
client = OpenAI()
completion = client.chat.completions.parse(
model="gpt-4o-2024-08-06",
messages=[
{
"role": "system",
"content": "You are a helpful assistant. The current date is August 6, 2024. You help users query for the data they are looking for by calling the query function.",
},
{
"role": "user",
"content": "look up all my orders in november of last year that were fulfilled but not delivered on time",
},
],
tools=[
openai.pydantic_function_tool(Query),
],This class is important because it defines how OpenAI Python SDK Tutorial: Production API Patterns implements the patterns covered in this chapter.
The Query class in examples/parsing_tools.py handles a key part of this chapter's functionality:
class Query(BaseModel):
table_name: Table
columns: List[Column]
conditions: List[Condition]
order_by: OrderBy
client = OpenAI()
completion = client.chat.completions.parse(
model="gpt-4o-2024-08-06",
messages=[
{
"role": "system",
"content": "You are a helpful assistant. The current date is August 6, 2024. You help users query for the data they are looking for by calling the query function.",
},
{
"role": "user",
"content": "look up all my orders in november of last year that were fulfilled but not delivered on time",
},
],
tools=[
openai.pydantic_function_tool(Query),
],
)
tool_call = (completion.choices[0].message.tool_calls or [])[0]
rich.print(tool_call.function)
assert isinstance(tool_call.function.parsed_arguments, Query)
print(tool_call.function.parsed_arguments.table_name)This class is important because it defines how OpenAI Python SDK Tutorial: Production API Patterns implements the patterns covered in this chapter.
flowchart TD
A[DynamicValue]
B[Condition]
C[Query]
A --> B
B --> C