Connect to OpenAI from Python
What you’ll build
Section titled “What you’ll build”A working Python script that authenticates with the OpenAI API and returns a chat completion. By the end you will have the SDK installed, your API key stored safely, and two usage patterns — synchronous and streaming — that you can adapt for production.
Prerequisites
Section titled “Prerequisites”| Requirement | Notes |
|---|---|
| Python 3.9+ | With an active virtual environment |
| OpenAI API key | Create one here |
pip |
Comes with Python |
Step 1 — Install the SDK
Section titled “Step 1 — Install the SDK”python -m pip install openai python-dotenvThe openai package is the official OpenAI Python SDK. python-dotenv loads a .env file for local development. Run this inside your project’s virtual environment.
Step 2 — Store the API key
Section titled “Step 2 — Store the API key”Never hardcode the key in source files. Use an environment variable.
Current shell session:
# macOS / Linuxexport OPENAI_API_KEY="sk-proj-your-key-here"
# Windows PowerShell$env:OPENAI_API_KEY = "sk-proj-your-key-here".env file for local development (add .env to .gitignore):
OPENAI_API_KEY=sk-proj-your-key-hereFor production, store the key in AWS Secrets Manager, Azure Key Vault, or your platform’s secret store.
Step 3 — Make a first request
Section titled “Step 3 — Make a first request”Save as openai_test.py and run with python openai_test.py:
import osfrom dotenv import load_dotenvfrom openai import OpenAI
load_dotenv() # loads OPENAI_API_KEY from .env if present
client = OpenAI() # reads OPENAI_API_KEY from environment automatically
response = client.chat.completions.create( model="gpt-4o", messages=[ {"role": "system", "content": "You are a concise assistant."}, {"role": "user", "content": "What is retrieval-augmented generation?"}, ], max_tokens=200,)
print(response.choices[0].message.content)Expected output: a short explanation of RAG. If you see AuthenticationError, check that the environment variable is set correctly.
Step 4 — Streaming responses
Section titled “Step 4 — Streaming responses”For applications where you display text as it arrives (chat UIs, terminals):
from openai import OpenAI
client = OpenAI()
stream = client.chat.completions.create( model="gpt-4o", messages=[{"role": "user", "content": "List five Python best practices."}], stream=True,)
for chunk in stream: delta = chunk.choices[0].delta.content if delta: print(delta, end="", flush=True)
print() # final newlineStep 5 — Structured output
Section titled “Step 5 — Structured output”For applications that need a predictable JSON response, use response_format with a Pydantic model:
from pydantic import BaseModelfrom openai import OpenAI
client = OpenAI()
class OrderSummary(BaseModel): order_id: str customer: str total: float status: str
response = client.beta.chat.completions.parse( model="gpt-4o", messages=[ { "role": "user", "content": "Extract: Order ORD-001 for BensonCo, $149.99, shipped.", } ], response_format=OrderSummary,)
order = response.choices[0].message.parsedprint(order.order_id, order.total) # ORD-001 149.99Choosing a model
Section titled “Choosing a model”| Model | Characteristics | Good for |
|---|---|---|
gpt-4o |
Balanced, multimodal | General-purpose applications |
gpt-4o-mini |
Fast and cheap | High-volume extraction, routing |
o3, o4-mini |
Slow, deliberate reasoning | Complex logic, math, planning |
text-embedding-3-small |
Embedding vectors | Semantic search, RAG |
Model IDs change over time. See the OpenAI Models guide for the current list.
Common mistakes
Section titled “Common mistakes”- Hardcoding the API key. Never put it in source code or commit it to git. Use an environment variable.
- Using
model="gpt-3.5-turbo"in new code. This model is older and less capable thangpt-4o-miniat the same price tier. Use current model IDs. - Not handling
RateLimitError. Implement exponential backoff for production code that calls the API repeatedly. - Not setting
max_tokens. Without a limit, long responses can consume more credits than expected.
# Minimal retry with backoffimport timefrom openai import OpenAI, RateLimitError
client = OpenAI()
def call_with_retry(prompt: str, retries: int = 3) -> str: for attempt in range(retries): try: resp = client.chat.completions.create( model="gpt-4o", messages=[{"role": "user", "content": prompt}], max_tokens=500, ) return resp.choices[0].message.content except RateLimitError: if attempt < retries - 1: time.sleep(2 ** attempt) # 1s, 2s, 4s else: raise return ""Related pages
Section titled “Related pages”- OpenAI API key guide — creating and securing your key
- OpenAI overview — model tiers and platform capabilities
- Connect to Gemini from Python — same pattern for Google’s API
- Python AI packages — full AI/ML package ecosystem
Official references
Section titled “Official references”- OpenAI Python SDK —
openaipackage on GitHub - OpenAI API reference — REST endpoint documentation
- OpenAI Cookbook — production patterns and examples