PlatformAPI documentation

Developer docs

5. Chat API

Version 1.0 · 2026-10-20

← Back to the API reference

POST /v1/chat/completions uses the same request and response format as OpenAI Chat Completions, and supports streaming, tool calling, JSON output and image input.

Request parameters

ParameterTypeRequiredDescription
modelstringYesModel id; see section 4
messagesarrayYesConversation messages; role is system, user, assistant or tool
max_tokensintegerNoMaximum tokens to generate. If it exceeds the model limit or the remaining context, the platform lowers it automatically instead of returning an error
max_completion_tokensintegerNoSame as max_tokens
temperaturenumberNoSampling temperature; lower is more deterministic
top_pnumberNoNucleus sampling ratio
streambooleanNoWhen true, the response is streamed as SSE
stopstring or arrayNoStop sequences
toolsarrayNoFunction definitions the model may call
tool_choicestring or objectNoauto, none, or a specific function
response_formatobjectNo{"type":"json_object"} or {"type":"json_schema", ...}
seedintegerNoFixed sampling seed for more reproducible results

top_p, stop, seed and other OpenAI-compatible parameters are passed straight to the model, so their effect depends on the model. Parameters a model does not support may be ignored or return 400 — test before you go live.

Response fields

FieldDescription
choices[].message.contentThe model's answer
choices[].message.tool_callsTools the model wants to call (when tools are used)
choices[].finish_reasonstop, length (hit max_tokens) or tool_calls
usageprompt_tokens, completion_tokens, total_tokens — the basis for billing

Streaming

With stream: true, the response is text/event-stream. Each event is one data: {...} line, and the stream ends with data: [DONE].

  • The platform turns on usage reporting, so any chunk may carry usage, and there may be a final chunk whose choices is an empty array. Check that the array is not empty before reading choices[0].
  • If the backend fails after the stream has started, the connection simply closes without an error event. Tokens already generated are still billed.
python
stream = client.chat.completions.create(
    model="glm-5.2-fp8",
    messages=[{"role": "user", "content": "List three sights in Tainan"}],
    stream=True,
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

Tool calling

When you provide tools, the model may return tool_calls. Your code runs the tool and sends the result back as a role: "tool" message; the model then produces its final answer. In streaming mode, tool-call arguments arrive split across several chunks — concatenate them yourself.

python
tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Look up the weather for a city",
        "parameters": {
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"],
        },
    },
}]

resp = client.chat.completions.create(
    model="glm-5.2-fp8",
    messages=[{"role": "user", "content": "What's the weather in Taichung right now?"}],
    tools=tools,
)
call = resp.choices[0].message.tool_calls[0]
print(call.function.name, call.function.arguments)

JSON output

response_format: {"type": "json_object"} asks the model to output valid JSON only. Describe the fields in your prompt as well. When you need strict fields, use json_schema instead (supported only by glm-5.2-fp8 and qwen3-vl-30b).

Reasoning models

glm-5.2-fp8 and glm-5.3-flash-uncensored support reasoning. The thinking process may use up part of max_tokens and may count toward output token usage. If a response has an empty content and finish_reason is length, raise max_tokens.

Image input

Use qwen3-vl-30b and put image_url entries in the content array, up to 4 images per request. Sending images as data: base64 is recommended.

python
import base64

with open("invoice.jpg", "rb") as f:
    b64 = base64.b64encode(f.read()).decode()

resp = client.chat.completions.create(
    model="qwen3-vl-30b",
    messages=[{
        "role": "user",
        "content": [
            {"type": "text", "text": "List the items and amounts on this invoice"},
            {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64}"}},
        ],
    }],
)

Personas

Power Champion can set up a fixed persona for you. Call GET /v1/personas/{persona_id} to get its messages_prefix and put it at the start of messages on every request. A fixed prefix hits the cache and lowers time to first token. Add ?rev=N to get a specific version; we provide the persona id.

json
{
  "persona_id": "...",
  "name": "Support assistant",
  "rev": 3,
  "updated": "...",
  "messages_prefix": [{ "role": "system", "content": "..." }]
}