How to create OpenAI client in python
Direct answer
Create an OpenAI client in Python by importing
OpenAI from openai and initializing it with your API key from os.environ like client = OpenAI(api_key=os.environ["OPENAI_API_KEY"]).Setup
Install
pip install openai Env vars
OPENAI_API_KEY Imports
import os
from openai import OpenAI Examples
inCreate client and send a simple chat message 'Hello'
outclient = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = client.chat.completions.create(model="gpt-4o", messages=[{"role": "user", "content": "Hello"}])
print(response.choices[0].message.content)
inInitialize client and request a chat completion with model 'gpt-4o-mini'
outclient = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = client.chat.completions.create(model="gpt-4o-mini", messages=[{"role": "user", "content": "What is AI?"}])
print(response.choices[0].message.content)
inCreate client and handle empty message list error
outclient = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
try:
response = client.chat.completions.create(model="gpt-4o", messages=[])
except Exception as e:
print(f"Error: {e}")
Integration steps
- Install the OpenAI Python SDK with pip.
- Set your OpenAI API key in the environment variable OPENAI_API_KEY.
- Import
OpenAIfrom theopenaipackage andos. - Initialize the client using
OpenAI(api_key=os.environ["OPENAI_API_KEY"]). - Use the client to call API methods such as
chat.completions.createwith the desired model and messages. - Extract the response text from
response.choices[0].message.content.
Full code
import os
from openai import OpenAI
# Initialize OpenAI client with API key from environment
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Create a chat completion request
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello, how do I create an OpenAI client in Python?"}]
)
# Print the assistant's reply
print("Response:", response.choices[0].message.content) output
Response: To create an OpenAI client in Python, import OpenAI from the openai package and initialize it with your API key from the environment variable OPENAI_API_KEY.
API trace
Request
{"model": "gpt-4o", "messages": [{"role": "user", "content": "Hello, how do I create an OpenAI client in Python?"}]} Response
{"choices": [{"message": {"content": "To create an OpenAI client in Python, import OpenAI from the openai package and initialize it with your API key from the environment variable OPENAI_API_KEY."}}], "usage": {"total_tokens": 30}} Extract
response.choices[0].message.contentVariants
Streaming chat completion ›
Use streaming to display partial results immediately for long or interactive responses.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
# Stream response for better UX on long outputs
response_stream = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Explain streaming in OpenAI Python SDK."}],
stream=True
)
for chunk in response_stream:
print(chunk.choices[0].delta.get('content', ''), end='')
print() Async client usage ›
Use async when integrating OpenAI calls into asynchronous Python applications for concurrency.
import os
import asyncio
from openai import OpenAI
async def main():
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = await client.chat.completions.acreate(
model="gpt-4o",
messages=[{"role": "user", "content": "Async client example."}]
)
print(response.choices[0].message.content)
asyncio.run(main()) Use smaller model for cost efficiency ›
Use smaller models like gpt-4o-mini to reduce cost and latency for simpler tasks.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "What is the weather today?"}]
)
print(response.choices[0].message.content) Performance
Latency~800ms for gpt-4o non-streaming calls
Cost~$0.002 per 500 tokens for gpt-4o
Rate limitsTier 1: 500 requests per minute / 30,000 tokens per minute
- Keep messages concise to reduce token usage.
- Use smaller models like gpt-4o-mini for cheaper calls.
- Cache frequent responses to avoid repeated calls.
| Approach | Latency | Cost/call | Best for |
|---|---|---|---|
| Standard client | ~800ms | ~$0.002 per 500 tokens | General purpose chat completions |
| Streaming client | Starts immediately, total ~800ms | Same as standard | Long responses with better UX |
| Async client | ~800ms | ~$0.002 per 500 tokens | Concurrent calls in async apps |
| Smaller model (gpt-4o-mini) | ~400ms | ~$0.0005 per 500 tokens | Cost-sensitive or simple tasks |
Quick tip
Always load your API key from environment variables to keep credentials secure and avoid accidental leaks.
Common mistake
Beginners often forget to use the latest SDK v1+ pattern and try deprecated methods like openai.ChatCompletion.create(), which no longer work.