C.W.K.
Stream
Lesson 01 of 07 · published

Installation & Setup

~22 min · install, setup, venv

Level 0Tokenizer
0 XP0/54 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

The official openai Python package (v2.31.0+ as of April 2026) provides a typed, ergonomic interface to all OpenAI APIs. It handles authentication, retries, streaming, and error handling automatically.

Environment Setup

The SDK automatically reads OPENAI_API_KEY from the environment. You never need to pass it explicitly unless you want to override it. The package supports Python 3.8+ and uses httpx as its HTTP client under the hood.

Why pin and bump on a schedule

The openai SDK ships fast. Major versions occasionally break parameter shapes (max_tokens → max_completion_tokens is a recent example). An unpinned dependency means a Friday afternoon deploy can silently start failing on Monday because pip resolved a newer minor version.

The pattern that works: pin a known-good lower bound (e.g. openai>=1.78,<2) in requirements.txt. Lock the resolved version in a lockfile (uv.lock, poetry.lock, or pip-compile output). Bump the lower bound deliberately on a schedule, not by accident.

Code

Install in a fresh venv·bash
# Standard installation
pip install openai

# With aiohttp backend for better async concurrency
pip install openai[aiohttp]
Verify with a tiny call·bash
# Set your API key as an environment variable
export OPENAI_API_KEY="sk-proj-..."

# Optional: enable debug logging
export OPENAI_LOG=debug

External links

Exercise

Set up a fresh venv, pin openai>=1.78,<2, install, and verify with a 1-line script that lists 5 model IDs. Commit the requirements.txt.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.