Zenaique

Walk through the shape of an OpenAI compatible chat completions request and response.

Flashcard·Easy·4.0 · 0·~30s·Asked atAi4bharatCitadelKrutrim·Relevant atOpenAI
Attempt it
TL;DR

Request carries model + role-tagged messages + sampling knobs; response carries assistant content + finish_reason + a usage block; streaming swaps the JSON body for SSE delta chunks.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

Picture sending a structured letter to an assistant. The envelope says which assistant to deliver it to (the model name). Inside the envelope, every paragraph is labeled with who is speaking: system rules first, then a back and forth between user and assistant. You also include a sticky note with knobs like 'do not exceed 500 words' and 'be a little creative'. The assistant writes a reply with three things: the actual text, a stamp saying why she stopped writing, and a meter showing how many words you used in total. If you ticked the streaming box, instead of one finished letter you get a stack of postcards arriving one piece at a time.

Concept explanation~2 min read

Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.

The chat completions endpoint is the lingua franca of hosted LLM serving in 2026. OpenAI's original shape, introduced in 2023, became the de facto industry standard because every client SDK and agent framework was written against it. Every serious provider (Anthropic, Google, Mistral, DeepSeek) now ships either a native OpenAI-compatible endpoint or a thin adapter, and every open-source serving stack (vLLM, TensorRT-LLM, SGLang) ships the front door so self-hosted deployments slot into existing client code with one base-URL change.

This deep dive walks through the request and response schemas in detail, the role of finish_reason as a dispatch contract, how streaming changes the wire format, and the production concerns that distinguish a working implementation from a correct one.

The request schema in detail

A chat completions request body has three components.

  • model, a string identifying which model to use. Hosted APIs accept names like gpt-5.5, claude-sonnet-4-6, gemini-3.1-pro. Self-hosted vLLM accepts the model path or alias configured at startup.
  • messages, an array of message objects, each with a role and content. The four roles are system (instructions and persona), user (input from the caller), assistant (prior model output, used for multi-turn context), and tool (results from function calls being fed back). Content is text by default; for multimodal endpoints it is an array of typed parts (text, image, audio).
  • Sampling and shaping parameters, temperature (rescales logits), top_p (nucleus truncation), max_tokens (hard cap on output length), stop (sequences that terminate generation), presence_penalty and frequency_penalty (discourage repetition), seed (best-effort reproducibility), and tools / tool_choice for function calling.

Multimodal content arrays

When content is an array, each entry has a type field (text, image_url, input_audio). GPT-5.5 and Claude Opus 4.7 both accept this shape. Image parts can carry a base64 data URL or a remote URL; vision models tokenize the image into a fixed number of vision tokens (often around 1024 for high-res images) which are billed in the usage block.

The response schema and finish_reason as a contract
Streaming: SSE framing and TTFT
Why every stack speaks OpenAI shape in 2026
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.

Real products, models, and research that use this idea.

  • OpenAI's GPT-5.5 chat completions endpoint defines the canonical schema; every other provider adopts it or supplies an adapter.
  • Anthropic's Messages API uses an OpenAI-compatible structure with role-tagged messages and adds explicit cache_creation / cache_read fields in the usage block.
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QHow does the chat completions contract handle multimodal inputs in 2026?
A

The content field per message becomes an array of typed parts: text parts, image parts (with base64 or URL), and increasingly audio parts. GPT-5.5, Claude Opus 4.7, and Gemini 3.1 Pro all use this shape. Tokenization for image parts is provider-specific and billed at separate rates.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Conflating the legacy completions endpoint with chat completions. Chat completions is structured around role-tagged messages, not a single prompt string, and finish_reason matters for tool-call dispatch.

Sign in to see all red flags and common mistakes.

60 second bullets to scan on the way to the call.

  • Required request fields: model, messages, optional sampling knobs

  • The four message roles (system, user, assistant, tool)

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
What is the KV cache in transformer inference?
Flashcard·Easy