A production-focused collection of comprehensive OpenAI API examples built with Python. Learn scalable, maintainable, and production-ready AI systems using modern OpenAI APIs, structured engineering patterns, and real-world architecture best practices.
This repository is a comprehensive, production-grade reference for OpenAI API engineering. It provides:
- β 20+ fully implemented examples across all major OpenAI API categories
- β Production-ready patterns for authentication, error handling, and configuration
- β Structured examples showing how to build scalable AI systems
- β Real-world use cases including RAG, agents, realtime systems, and more
- β Best practices for security, cost optimization, and deployment
Perfect for:
- π AI engineering interview preparation
- π Learning OpenAI API fundamentals
- ποΈ Production AI architecture reference
- π€ Building AI SaaS products
- π§ Internal AI tooling and automation
Chaitanya Dasadiya
- π GitHub: @cdasadiya
- π― Focus Areas: AI Engineering, Python Development, OpenAI APIs, Production Systems
This repository aims to become a complete professional OpenAI engineering reference covering:
| Category | Coverage |
|---|---|
| Core Platform | Authentication, API keys, organizations, projects, usage, billing, rate limits |
| Text & Reasoning | Responses API, streaming, structured outputs, function calling, tool calling |
| Realtime Systems | WebSocket, live streaming, voice, transcription, interrupt handling |
| Audio & Vision | Speech-to-text, translation, TTS, image understanding, OCR |
| AI Agents | Multi-agent systems, orchestration, memory, MCP integration |
| Infrastructure | RAG pipelines, embeddings, fine-tuning, deployment, monitoring |
openai-api-playground/
βββ 01_core_platform/ # Authentication, billing, rate limits, tokens
βββ 02_responses_api/ # Chat, streaming, structured outputs, tool calling
βββ 03_realtime_apis/ # WebSocket, voice, low-latency systems
βββ 04_audio_apis/ # Transcription, translation, TTS (coming soon)
βββ 05_vision_apis/ # Image understanding, OCR (coming soon)
βββ utils/ # Shared OpenAI client and utilities
βββ requirements.txt # Python dependencies
βββ .env.example # Environment template
βββ README.md # This file
| Folder | Purpose |
|---|---|
01_core_platform/ |
Production foundations: authentication, API keys, organizations, projects, usage tracking, billing, rate limits, models, tokens, pricing optimization |
02_responses_api/ |
Complete text API examples: basic responses, structured output, streaming, function calling, tool calling, multi-turn conversations, reasoning models |
03_realtime_apis/ |
Realtime API examples: WebSocket connections, live streaming, voice systems, transcription, interrupt handling, low-latency architectures |
04_audio_apis/ |
Audio API examples: transcription, translation, text-to-speech, voice workflows (β³ coming soon) |
05_vision_apis/ |
Vision API examples: image understanding, OCR, multi-image analysis, reasoning (β³ coming soon) |
utils/ |
Shared configuration, OpenAI client factory, and helper utilities |
All Core Platform examples are fully implemented in 01_core_platform/:
β authentication.py - Secure authentication validation
β api_keys.py - Safe API key management
β organizations.py - Multi-organization scoping
β projects.py - Project-based isolation
β usage_tracking.py - Token usage analysis
β billing.py - Billing concepts and tracking
β rate_limits.py - Rate limit handling with exponential backoff
β models.py - Model discovery and metadata
β tokens.py - Token estimation and comparison
β pricing_optimization.py - Cost optimization strategies
All Responses API examples are fully implemented in 02_responses_api/:
β basic_response.py - Simple text generation
β structured_json_output.py - Structured outputs with schema
β system_prompting.py - System instructions
β streaming_responses.py - Real-time streaming
β function_calling.py - Function invocation
β tool_calling.py - Tool orchestration
β multi_turn_conversation.py - Conversational AI
β reasoning_models.py - Advanced reasoning models
All Realtime API examples are fully implemented in 03_realtime_apis/:
β websocket_connections.py - WebSocket fundamentals
β live_streaming.py - Audio stream handling
β realtime_voice.py - Voice interaction systems
β realtime_transcription.py - Real-time speech-to-text
β interrupt_handling.py - User interruption patterns
β low_latency_systems.py - Optimized latency architecture
β³ Speech-to-text
β³ Transcription
β³ Translation
β³ Text-to-speech
β³ Voice synthesis
β³ Audio generation
β³ Image understanding
β³ OCR
β³ Multi-image analysis
β³ Vision reasoning
- Python 3.12 or higher
- OpenAI API key from platform.openai.com
- Git and basic terminal knowledge
git clone https://github.com/cdasadiya/openai-api-playground.git
cd openai-api-playground# Create virtual environment
python3 -m venv .venv
source .venv/bin/activate # macOS/Linux
# or
.venv\Scripts\activate # Windows
# Install dependencies
pip install -r requirements.txt
# Create .env file
echo "OPENAI_API_KEY=sk-proj-your_api_key_here" > .env
# Run an example
python 02_responses_api/basic_response.py- Click Code β Codespaces β Create codespace on main
- Add GitHub Codespaces secret:
- Settings β Secrets and variables β Codespaces
- Name:
OPENAI_API_KEY - Value: Your OpenAI API key
- Grant access to
openai-api-playground
- Run:
python 02_responses_api/basic_response.py
# Run a simple Responses API example
python 02_responses_api/basic_response.py
# Or run a Core Platform example
python 01_core_platform/authentication.py# 1. Create .env file in repository root
OPENAI_API_KEY=sk-proj-your_api_key_here
# Optional: Set organization and project scope
OPENAI_ORG_ID=org-...
OPENAI_PROJECT_ID=proj-...Settings β Codespaces β Secrets β New repository secret
Name: OPENAI_API_KEY
Value: sk-proj-your_api_key_here
Repository access: openai-api-playground
β DO:
- βοΈ Store API keys in
.envfiles locally - βοΈ Use GitHub Codespaces secrets for cloud development
- βοΈ Use environment variables in production
- βοΈ Add
.envto.gitignore - βοΈ Rotate compromised keys immediately
β DON'T:
- β Commit API keys to GitHub
- β Hardcode keys in Python files
- β Push
.envfiles to repositories - β Expose keys in screenshots/logs
- β Store secrets in public repositories
# Environment
.env
.env.*
!.env.example
# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
# Virtual Environment
.venv/
venv/
ENV/
# IDE
.vscode/
.idea/
*.swp
*.swo- Core Platform β 10/10 topics covered
- Responses API β 8/8 topics covered
- Realtime APIs β 6/6 topics covered
- Audio APIs β 0/6 topics covered
- Vision APIs β 0/4 topics covered
- Image APIs β 0/5 topics covered
- Embeddings & Search β 0/5 topics covered
- Fine-Tuning β 0/5 topics covered
- Assistants & Agents β 0/6 topics covered
- Files & Data β 0/4 topics covered
- Safety & Moderation β 0/4 topics covered
- Production Engineering β 0/9 topics covered
- AI Architecture β 0/6 topics covered
- Deployment β 0/6 topics covered
- Ecosystem Integrations β 0/6 topics covered
- Advanced Systems β 0/9 topics covered
| Layer | Technologies |
|---|---|
| Language | Python 3.12+ |
| AI Platform | OpenAI API (latest) |
| Environment | Python Venv, Python-dotenv |
| Patterns | Async/await, Streaming, Structured outputs |
| Focus | Production-ready, secure, scalable |
This repository follows production-grade standards:
- π Secure Architecture β Centralized client, environment-based configuration
- π¦ Structured Outputs β Type-safe, schema-validated responses
- π Error Handling β Graceful failures, exponential backoff retry logic
- π Observability β Logging, debugging support, usage tracking
- β‘ Performance β Streaming support, async operations, caching patterns
- π Scalability β Reusable utilities, modular design
- π‘οΈ Security β Secret management, no hardcoded keys
- π³ Compatibility β Codespaces-ready, Docker-compatible
Follow this progression to master OpenAI APIs:
- Start Here β
01_core_platform/authentication.pyβ Validate setup - Basics β
02_responses_api/basic_response.pyβ Text generation - Structured β
02_responses_api/structured_json_output.pyβ Typed outputs - Real-time β
02_responses_api/streaming_responses.pyβ Live responses - Tools β
02_responses_api/function_calling.pyβ API integration - Advanced β
02_responses_api/tool_calling.pyβ Tool orchestration - Conversation β
02_responses_api/multi_turn_conversation.pyβ Memory - Reasoning β
02_responses_api/reasoning_models.pyβ Complex tasks - Voice β
03_realtime_apis/realtime_voice.pyβ Voice systems - Production β Scale to production with monitoring and optimization
This repository can accelerate development for:
- π Interview Prep β AI engineering interview preparation and problem solving
- π Learning β Understanding OpenAI API capabilities and best practices
- ποΈ Architecture β Reference implementations for production AI systems
- π SaaS β Building AI-powered SaaS products and platforms
- π§ Internal Tools β Creating internal AI tooling and automation
- π¨ Prototyping β Rapid AI prototyping and experimentation
- π€ Agents β Multi-agent systems and orchestration
- π¬ Multimodal β Combining text, voice, vision, and audio
Contributions are welcome and appreciated! We're looking for help in these areas:
- β¨ New Examples β Additional OpenAI API examples
- ποΈ Architecture β RAG systems, workflow patterns, agent orchestration
- π Realtime β Voice, video, and low-latency applications
- π€ Agents β Multi-agent systems, tool orchestration
- π Deployment β CI/CD, Docker, Kubernetes, serverless
- β‘ Performance β Optimization, benchmarking, caching
- π‘οΈ Production β Monitoring, logging, observability
- π Documentation β Improving guides and examples
See CONTRIBUTING.md for guidelines.
This repository is licensed under the MIT License.
See LICENSE file for complete details.
- π OpenAI API Documentation
- π Get Your API Key
- π¦ OpenAI on Twitter/X
- π OpenAI Blog
- π¬ OpenAI Community
Made with β€οΈ by Chaitanya Dasadiya
β If this helped you, please star this repository! β