Loading...
Loading...
Set up API keys, make your first chat completion, stream responses, and handle structured outputs
The OpenAI API lets you integrate GPT-4o, o3, and o4-mini into your applications. The Chat Completions API is the primary interface.
import os
from openai import OpenAI
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
response = client.chat.completions.create(
model='gpt-4o',
messages=[
{'role': 'system', 'content': 'You are a helpful coding assistant.'},
{'role': 'user', 'content': 'Write a Python function to find prime numbers up to n.'}
]
)
print(response.choices[0].message.content)The API uses a structured conversation with roles: system (behavior), user (human), assistant (model's prior responses).
For real-time apps, use streaming — tokens arrive as generated:
stream = client.chat.completions.create(
model='gpt-4o',
messages=[{'role': 'user', 'content': 'Write a short poem.'}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end='')| Code | Meaning | Fix |
|---|---|---|
| 401 | Invalid API key | Check your key |
| 429 | Rate limited | Add backoff |
| 500 | Server error | Retry |
Write a function with error handling:
import os, time
from openai import OpenAI
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))
def safe_chat(messages, model='gpt-4o', max_retries=3):
for attempt in range(max_retries):
try:
response = client.chat.completions.create(model=model, messages=messages)
return response.choices[0].message.content
except Exception as e:
print(f"Attempt {attempt+1} failed: {e}")
time.sleep(2 ** attempt)
return None