Developer docs
5. Chat API
Version 1.0 · 2026-10-20
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
| Parameter | Type | Required | Description |
|---|---|---|---|
model | string | Yes | Model id; see section 4 |
messages | array | Yes | Conversation messages; role is system, user, assistant or tool |
max_tokens | integer | No | Maximum 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_tokens | integer | No | Same as max_tokens |
temperature | number | No | Sampling temperature; lower is more deterministic |
top_p | number | No | Nucleus sampling ratio |
stream | boolean | No | When true, the response is streamed as SSE |
stop | string or array | No | Stop sequences |
tools | array | No | Function definitions the model may call |
tool_choice | string or object | No | auto, none, or a specific function |
response_format | object | No | {"type":"json_object"} or {"type":"json_schema", ...} |
seed | integer | No | Fixed 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
| Field | Description |
|---|---|
choices[].message.content | The model's answer |
choices[].message.tool_calls | Tools the model wants to call (when tools are used) |
choices[].finish_reason | stop, length (hit max_tokens) or tool_calls |
usage | prompt_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 whosechoicesis an empty array. Check that the array is not empty before readingchoices[0]. - If the backend fails after the stream has started, the connection simply closes without an error event. Tokens already generated are still billed.
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.
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.
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.
{
"persona_id": "...",
"name": "Support assistant",
"rev": 3,
"updated": "...",
"messages_prefix": [{ "role": "system", "content": "..." }]
}