3.2 OpenAI & Anthropic SDKs
AI-generated content may contain errors. Always verify against official sources.
3.2 OpenAI & Anthropic SDKs
Key Concepts: Python SDK setup · Messages API · Streaming responses
Official Docs: OpenAI Python SDK · Anthropic Python SDK
OpenAI Python SDK
pip install openai python-dotenv
Basic Chat Completion
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
client = OpenAI() # reads OPENAI_API_KEY from environment
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain recursion in 2 sentences."}
],
temperature=0.3,
max_tokens=256,
)
print(response.choices[0].message.content)
print(f"Tokens used: {response.usage.total_tokens}")
Streaming Responses
Streaming returns tokens as they are generated — essential for chat UIs where you want text to appear immediately.
with client.chat.completions.stream(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Write a haiku about Python."}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
print() # newline after stream ends
Accessing the Full Response After Streaming
with client.chat.completions.stream(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Tell me a joke."}],
) as stream:
full_response = stream.get_final_completion()
print(full_response.choices[0].message.content)
print(f"Total tokens: {full_response.usage.total_tokens}")
Anthropic Python SDK
pip install anthropic python-dotenv
Basic Message
import anthropic
from dotenv import load_dotenv
load_dotenv()
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from environment
message = client.messages.create(
model="claude-3-5-haiku-20241022",
max_tokens=256,
system="You are a helpful assistant.",
messages=[
{"role": "user", "content": "Explain recursion in 2 sentences."}
],
)
print(message.content[0].text)
print(f"Input tokens: {message.usage.input_tokens}")
print(f"Output tokens: {message.usage.output_tokens}")
Streaming with Anthropic
with client.messages.stream(
model="claude-3-5-haiku-20241022",
max_tokens=256,
messages=[{"role": "user", "content": "Write a haiku about the ocean."}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
print()
SDK Comparison
| Feature | OpenAI SDK | Anthropic SDK |
|---|---|---|
system message | Inside messages array | Separate system parameter |
| Response text | response.choices[0].message.content | message.content[0].text |
| Token usage | response.usage.total_tokens | message.usage.input_tokens + output_tokens |
| Streaming class | client.chat.completions.stream() | client.messages.stream() |
| Structured output | client.beta.chat.completions.parse() | Tool use / XML tags |
Common Mistakes
- Anthropic
systemin the wrong place — Anthropic takessystemas a top-level parameter, not inside themessagesarray. - Not flushing stdout during streaming — always use
flush=Trueinprint()when streaming to terminal. - Accessing
response.contentdirectly (Anthropic) — always usemessage.content[0].text; content is a list even for single responses. - Using deprecated
openai.ChatCompletion.create— the new SDK usesclient = OpenAI(); client.chat.completions.create(...). The old global function was removed in SDK v1.0.
Quick Quiz
Q1. In the Anthropic SDK, where does the system prompt go?
A1. As a top-level system="..." parameter in client.messages.create(), NOT inside the messages list.
Q2. How do you access the reply text from an OpenAI streaming response after it finishes?
A2. Call stream.get_final_completion() inside the with block to get the full ChatCompletion object.
Q3. Which OpenAI SDK version removed the old openai.ChatCompletion.create() global function?
A3. SDK v1.0 (released November 2023). Always use client = OpenAI(); client.chat.completions.create(...).
Student Exercise
Exercise 3.2 — Streaming chat
Build a terminal chat loop using streaming. The user types a message, the assistant streams its response token-by-token, and the conversation continues until the user types exit. Maintain conversation history.