Skip to content

Connect to OpenAI from Python

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.

Requirement Notes
Python 3.9+ With an active virtual environment
OpenAI API key Create one here
pip Comes with Python
Terminal window
python -m pip install openai python-dotenv

The 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.

Never hardcode the key in source files. Use an environment variable.

Current shell session:

Terminal window
# macOS / Linux
export 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-here

For production, store the key in AWS Secrets Manager, Azure Key Vault, or your platform’s secret store.

Save as openai_test.py and run with python openai_test.py:

import os
from dotenv import load_dotenv
from 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.

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 newline

For applications that need a predictable JSON response, use response_format with a Pydantic model:

from pydantic import BaseModel
from 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.parsed
print(order.order_id, order.total) # ORD-001 149.99
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.

  • 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 than gpt-4o-mini at 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 backoff
import time
from 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 ""