Skip to main content
Use OpenAI Chat Completions as one interface for text models from OpenAI, Google Gemini, DeepSeek, Qwen, and other vendors. Send messages and read the reply from choices[0].message.content. Optional parameters and tool support vary by model. If your application is built on Anthropic Messages or Gemini generateContent request structures, use the Claude protocol or Gemini protocol page instead. The Text generation API guide compares the three formats.

Make a request

Create an API key in Token management in the default group, with its billing mode set to Usage first (按量优先, recommended) or Usage-based (按量计费), and export it in your terminal:
Requests use Authorization: Bearer authentication and a JSON body containing model and messages. The examples use gpt-5.4-mini.
The reply to this prompt should be connected. Replace the prompt with your application’s message once the client is configured.

Choose a model

Pick the model in the model catalog. Before you change the model value, check three things in the model’s row:
  • Endpoints includes OpenAI-compatible. A model that lists only Responses, such as the o3-pro series, needs /v1/responses instead; see the Responses API examples.
  • Token groups includes the group of your API key. Use a default-group key for this endpoint: Claude Code-group keys are for the Claude protocol only, and features such as tool calls are not guaranteed to work when they send Chat Completions requests.
  • Input, Output, and Cache read prices per 1M tokens fit your workload.
Common text models in the catalog include:
  • gpt-5.4-mini and gpt-5.4
  • gemini-3.8-flash, gemini-3.6-flash, and gemini-3.5-flash-lite
Copy the exact ID from the catalog or console rather than building it from a product name. The Text generation API guide lists the formats and prices of these models, and OpenAI models shows how to call the Responses-only o3-pro. A general ranking will not tell you which model suits your application. Run a sample of your own prompts through two or three candidates and compare answer quality, instruction following, structured output, tool-call success if you use tools, latency, and the token usage recorded for each request. Pin the chosen model ID in your configuration so that a model change is a deliberate update.

Request fields

Start with model and messages. Optional fields and accepted values depend on the model; a request rejected for an unsupported parameter needs a field change before you retry it. See the official Chat Completions reference for upstream field definitions.

Messages and conversation history

Each message has a role and content. Use user for user input and assistant for previous model replies. Send the history your application needs with each request:
Instruction roles such as system or developer, tool messages, and multimodal content depend on the selected model. To send images to a vision model, see the Image understanding API.

Read the response

A text response contains a choices array. This shortened example shows the fields to read:
Read message.content for the answer and finish_reason to see why generation ended. A length finish reason means the reply reached an output or context limit; shorten the input or adjust the model’s supported output limit if you need a complete answer. For a model that supports tool calls, the reply may contain message.tool_calls instead of ordinary text. Preserve those calls and their matching results when continuing the conversation. Token counts may appear in usage; use call logs to review account charges.

Stream a response

Add "stream": true to receive the reply in pieces as the model writes it. The response is a stream of server-sent events:
  • Each data: line carries a chat.completion.chunk object.
  • The new text is in choices[0].delta.content.
  • The stream ends with a data: [DONE] line.
-N turns off cURL’s output buffering so that each chunk prints as soon as it arrives.
The raw stream looks like this, shortened to the fields you need; real chunks also carry fields such as id and model:
When you read the stream:
  • Append each delta.content in order to rebuild the full reply.
  • The first chunk may carry only the role, and some chunks, such as a final usage chunk, can have an empty choices array, so check before you read.
  • The last content chunk sets finish_reason; data: [DONE] then closes the stream, and the Python SDK ends the loop at that point.
  • If the connection drops before [DONE], treat the reply as incomplete.

Handle errors

For a client timeout, check whether the request appears in call logs before sending it again. The Python examples disable automatic retries; add retry handling deliberately in your application. When contacting support, include the request time, endpoint, model, token group, and error message with credentials removed. Never send your API key.