Home Knowledge Base OpenAI SDK

OpenAI SDK is the official Python and TypeScript client library for the OpenAI API — providing type-safe access to GPT models, DALL-E image generation, Whisper transcription, embeddings, and fine-tuning endpoints — with synchronous, asynchronous, and streaming interfaces that serve as the de facto standard for LLM API integration across the industry.

What Is the OpenAI SDK?

Why the OpenAI SDK Matters

Core Usage Patterns

Basic Chat Completion:

from openai import OpenAI

client = OpenAI()  # Uses OPENAI_API_KEY env variable

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Explain quantum entanglement simply."}
    ],
    max_tokens=500,
    temperature=0.7
)
print(response.choices[0].message.content)

Streaming Response:

with client.chat.completions.stream(model="gpt-4o", messages=[...]) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

Tool Calling (Function Calling):

tools = [{"type": "function", "function": {
    "name": "get_weather",
    "parameters": {"type": "object", "properties": {"city": {"type": "string"}}}
}}]
response = client.chat.completions.create(model="gpt-4o", messages=[...], tools=tools)
# Check response.choices[0].message.tool_calls for tool invocation

Async Usage:

from openai import AsyncOpenAI
import asyncio

async_client = AsyncOpenAI()
async def fetch(prompt):
    return await async_client.chat.completions.create(model="gpt-4o-mini", messages=[{"role":"user","content":prompt}])

Embeddings:

embedding = client.embeddings.create(model="text-embedding-3-small", input="Sample text")
vector = embedding.data[0].embedding  # 1536-dimensional float list

Key API Capabilities

SDK v0 vs v1 Migration

Old (v0)New (v1+)
openai.ChatCompletion.create()client.chat.completions.create()
openai.api_key = "sk-..."client = OpenAI(api_key="sk-...")
Dict responsesTyped Pydantic objects
No async clientAsyncOpenAI()

The OpenAI SDK is the lingua franca of LLM application development — mastering its patterns for streaming, tool calling, structured outputs, and async usage provides skills that transfer directly to Azure OpenAI, Groq, Together AI, and any other OpenAI-compatible provider, making it the most leveraged API investment in the AI engineering toolkit.

openai sdkpythontypescript

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.