The Chat Completions API is the foundational interface for communicating with OpenAI's language models. Every interaction follows a simple request-response pattern: you send a list of messages with assigned roles, and the API returns a completion.
The message array is the heart of every request. Each message has a role and content. The available roles are:
- system / developer — High-level instructions shaping the model's behavior. Newer GPT-5.x models prefer
developer. - user — Messages from the human end-user.
- assistant — Previous model responses (used for multi-turn context).
- tool — Results from tool/function calls, referencing a
tool_call_id.
Request / Response Lifecycle
The basic flow is straightforward:
For streaming responses, you set stream: true and receive Server-Sent Events:
Minimal Python Example
The response includes a choices array (usually one element), each with a message object and a finish_reason indicating why generation stopped (stop, length, tool_calls, or content_filter). The usage object tells you exactly how many tokens were consumed.