How to Use GLM 5.2 API with Python: Complete Integration Guide
Jul 20, 2026

How to Use GLM 5.2 API with Python: Complete Integration Guide

GLM 5.2 is OpenAI SDK-compatible. Change base_url and model name and your existing Python code works. Here is the step-by-step guide with streaming, function calling, and async examples.

If you already use OpenAI's Python SDK, integrating GLM 5.2 takes about two minutes: swap the base_url, change the model name, and your existing code runs against a 753B-parameter model that hits 158 tokens per second at $1.40/M input tokens. This guide covers everything from installation through streaming, function calling, async usage, and JSON mode — with working code you can copy directly.

Prerequisites

Before writing any code, make sure you have the following:

  • Python 3.8 or later installed on your machine
  • A Z.ai API key — sign up at z.ai and generate a key from your dashboard (GLM 5.2 is served through Z.ai's inference API)
  • pip available in your environment
  • Basic familiarity with Python and REST APIs (no prior GLM experience required)

If you want to use GLM 5.2 through OpenRouter instead of Z.ai directly, you will need an OpenRouter account and key instead. Both approaches are covered below.


Step 1: Install the OpenAI Python SDK

GLM 5.2's API is fully compatible with the OpenAI SDK — you do not need a separate client library. Install it with pip:

pip install openai

If you are using a virtual environment (recommended), activate it first:

python -m venv glm-env
source glm-env/bin/activate   # Windows: glm-env\Scripts\activate
pip install openai

Verify the installation:

python -c "import openai; print(openai.__version__)"

Any version of the openai package from 1.0.0 onward works. The SDK's base_url parameter is what routes requests to Z.ai instead of OpenAI's servers.


Step 2: Make Your First API Call

Create a file called glm_hello.py and paste this in:

from openai import OpenAI

client = OpenAI(
    api_key="YOUR_ZAI_KEY",          # replace with your Z.ai API key
    base_url="https://api.z.ai/v1",  # GLM 5.2 endpoint
)

response = client.chat.completions.create(
    model="glm-5.2",
    messages=[
        {"role": "user", "content": "Explain the Mixture of Experts architecture in two sentences."}
    ],
)

print(response.choices[0].message.content)

Run it:

python glm_hello.py

You should see a response in under two seconds. GLM 5.2's median time-to-first-token is 1.54 seconds, so the first words appear quickly even on long outputs.

What just happened: The OpenAI client sent an HTTPS POST to https://api.z.ai/v1/chat/completions with your key in the Authorization header. Z.ai routed the request to GLM 5.2 (753B parameters, Mixture of Experts, 40B active per forward pass) and returned an OpenAI-format JSON response. Your code never knew it wasn't talking to OpenAI.


Step 3: Add a System Prompt

System prompts go as the first message with "role": "system". GLM 5.2's 1M-token context window (1,048,576 tokens) means you can include large instruction sets, extensive examples, or full document context without hitting limits.

from openai import OpenAI

client = OpenAI(
    api_key="YOUR_ZAI_KEY",
    base_url="https://api.z.ai/v1",
)

response = client.chat.completions.create(
    model="glm-5.2",
    messages=[
        {
            "role": "system",
            "content": (
                "You are a senior Python engineer. "
                "Always respond with production-quality code and include error handling. "
                "Add type hints and docstrings to every function."
            ),
        },
        {
            "role": "user",
            "content": "Write a function that retries an HTTP request up to 3 times with exponential backoff.",
        },
    ],
)

print(response.choices[0].message.content)

The system message shapes the model's behavior for the entire conversation. With a 1M-token window, you can pass entire codebases as context — see GLM 5.2 context window explained for practical limits and cost implications.


Step 4: Enable Streaming

For long outputs or real-time applications, set stream=True. The response becomes an iterator; read delta.content from each chunk:

from openai import OpenAI

client = OpenAI(
    api_key="YOUR_ZAI_KEY",
    base_url="https://api.z.ai/v1",
)

stream = client.chat.completions.create(
    model="glm-5.2",
    messages=[
        {"role": "user", "content": "Write a 500-word essay on the history of transformer models."}
    ],
    stream=True,
)

for chunk in stream:
    delta = chunk.choices[0].delta
    if delta.content:
        print(delta.content, end="", flush=True)

print()  # newline after streaming finishes

At 158 tokens per second, GLM 5.2 ranks third among frontier models for throughput. Streaming makes that speed visible to the user — words appear as fast as the model generates them rather than waiting for the full response to buffer.

Collecting the streamed response into a string:

full_response = ""
for chunk in stream:
    delta = chunk.choices[0].delta
    if delta.content:
        full_response += delta.content
        print(delta.content, end="", flush=True)
print()
print(f"\nTotal characters: {len(full_response)}")

Step 5: Function Calling (Tool Use)

GLM 5.2 supports function calling with the same schema OpenAI uses. Define your tools, pass them in the tools parameter, and handle the model's tool call in the response:

import json
from openai import OpenAI

client = OpenAI(
    api_key="YOUR_ZAI_KEY",
    base_url="https://api.z.ai/v1",
)

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get the current weather for a given city.",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {
                        "type": "string",
                        "description": "The city name, e.g. Tokyo",
                    },
                    "unit": {
                        "type": "string",
                        "enum": ["celsius", "fahrenheit"],
                        "description": "Temperature unit",
                    },
                },
                "required": ["city"],
            },
        },
    }
]

messages = [
    {"role": "user", "content": "What is the weather like in Tokyo right now?"}
]

response = client.chat.completions.create(
    model="glm-5.2",
    messages=messages,
    tools=tools,
    tool_choice="auto",
)

choice = response.choices[0]
if choice.finish_reason == "tool_calls":
    tool_call = choice.message.tool_calls[0]
    function_name = tool_call.function.name
    arguments = json.loads(tool_call.function.arguments)
    print(f"Model wants to call: {function_name}")
    print(f"Arguments: {arguments}")

    # In production, call your actual function here:
    # result = get_weather(**arguments)
    # Then add the result back to messages and call the API again.

GLM 5.2 scored 62.1% on SWE-bench Pro, meaning its code and tool-use reasoning is solid for agent workflows. For multi-step agentic tasks, keep passing tool results back into the message list and re-calling the API until finish_reason is "stop".


Step 6: JSON Mode

When you need structured output, use response_format={"type": "json_object"}. The model guarantees its response is valid JSON:

from openai import OpenAI
import json

client = OpenAI(
    api_key="YOUR_ZAI_KEY",
    base_url="https://api.z.ai/v1",
)

response = client.chat.completions.create(
    model="glm-5.2",
    messages=[
        {
            "role": "system",
            "content": "You always respond with valid JSON.",
        },
        {
            "role": "user",
            "content": (
                "Return a JSON object with fields: "
                "model_name (string), parameter_count (string), "
                "speed_tps (number), context_tokens (number)."
            ),
        },
    ],
    response_format={"type": "json_object"},
)

data = json.loads(response.choices[0].message.content)
print(json.dumps(data, indent=2))

JSON mode is useful for classification pipelines, structured data extraction, and any downstream code that needs to json.loads() the result without defensive parsing.


Step 7: Async Usage

For high-concurrency applications — batch processing, FastAPI endpoints, async pipelines — use AsyncOpenAI:

import asyncio
from openai import AsyncOpenAI

client = AsyncOpenAI(
    api_key="YOUR_ZAI_KEY",
    base_url="https://api.z.ai/v1",
)

async def summarize(text: str) -> str:
    response = await client.chat.completions.create(
        model="glm-5.2",
        messages=[
            {"role": "system", "content": "Summarize the following text in one paragraph."},
            {"role": "user", "content": text},
        ],
    )
    return response.choices[0].message.content

async def main():
    texts = [
        "Mixture of Experts (MoE) is an architecture where only a subset of parameters...",
        "The attention mechanism in transformers computes queries, keys, and values...",
        "Reinforcement learning from human feedback (RLHF) fine-tunes language models...",
    ]
    # Run all three requests concurrently
    results = await asyncio.gather(*[summarize(t) for t in texts])
    for i, result in enumerate(results):
        print(f"Summary {i + 1}: {result}\n")

asyncio.run(main())

Parallel async requests are the right approach for batch jobs. GLM 5.2 at 158 t/s means a 500-token summary finishes in about 3 seconds; running 10 concurrently takes roughly the same wall-clock time as running one.


Step 8: OpenRouter Alternative

If you prefer OpenRouter's unified billing or want to A/B test GLM 5.2 against other models through a single key, point base_url at OpenRouter:

from openai import OpenAI

client = OpenAI(
    api_key="YOUR_OPENROUTER_KEY",
    base_url="https://openrouter.ai/api/v1",
)

response = client.chat.completions.create(
    model="z-ai/glm-5.2",   # note the OpenRouter model identifier
    messages=[
        {"role": "user", "content": "What is 158 tokens per second in practical terms?"}
    ],
)

print(response.choices[0].message.content)

The only differences are the base_url, your OpenRouter key, and the model string ("z-ai/glm-5.2" instead of "glm-5.2"). Everything else — streaming, tools, JSON mode, async — works identically.


GLM 5.2 API Quick Reference

ParameterValue
Base URL (Z.ai)https://api.z.ai/v1
Base URL (OpenRouter)https://openrouter.ai/api/v1
Model name (Z.ai)glm-5.2
Model name (OpenRouter)z-ai/glm-5.2
Context window1,048,576 tokens (1M)
Input price$1.40/M tokens
Output price$4.40/M tokens
Cache hit price$0.26/M tokens
Speed158 t/s
Median TTFT1.54s
ModalitiesText only

Pricing at Scale

Understanding cost before you scale matters. GLM 5.2's Mixture of Experts architecture keeps prices low because only 40B of 753B parameters activate per request.

WorkloadTokensInput CostOutput CostTotal
Single chat message~500 in / ~300 out$0.00070$0.00132$0.00202
Code review (large file)~4,000 in / ~800 out$0.0056$0.00352$0.00912
Document summarization~10,000 in / ~500 out$0.014$0.0022$0.0162
1M-token context call1,000,000 in / ~2,000 out$1.40$0.0088$1.4088
10,000 chat completions~500 avg in / ~300 avg out$7.00$13.20$20.20

Cache hits at $0.26/M make repeated large-context calls significantly cheaper. If you pass a static system prompt or fixed document with every request, cache hits reduce that portion of input costs by ~81%. For full pricing context, see GLM 5.2 pricing breakdown.


Common Issues

IssueCauseFix
AuthenticationErrorWrong or missing API keyCheck api_key value; ensure Z.ai key, not OpenAI key
404 Not Found on modelWrong model nameUse "glm-5.2" for Z.ai, "z-ai/glm-5.2" for OpenRouter
Response is NoneStreaming without reading deltaCheck delta.content is not None before printing
JSONDecodeError after JSON modePrompt didn't instruct JSON outputAdd system prompt "You always respond with valid JSON."
Slow first tokenCold start on some requestsTTFT is ~1.54s median; first call in a session may be slightly higher
Tool call not triggeredModel chose not to callUse tool_choice="required" to force a tool call
Context limit exceededInput exceeds 1,048,576 tokensTruncate or chunk the input; GLM 5.2's 1M window is generous but not infinite

Frequently Asked Questions

Do I need a special SDK to use GLM 5.2?

No. The standard openai Python package works without modification. GLM 5.2's API follows the OpenAI Chat Completions format exactly — same request schema, same response shape, same streaming protocol. The only changes are base_url and model.

Is GLM 5.2 safe to use in production?

Yes. Z.ai operates production infrastructure with standard SLAs. GLM 5.2 itself is an MIT-licensed open-weights model (available on Hugging Face at THUDM/GLM-5.2), so the model weights are public and auditable. The API service through Z.ai is the hosted version.

Can I run GLM 5.2 locally?

Yes, because the MIT license allows it. Download the weights from Hugging Face. However, 753B parameters in FP16 requires substantial GPU memory — multiple H100s. For most teams, the hosted API at $1.40/M input tokens is more practical than self-hosting.

Does GLM 5.2 support images or audio?

No. GLM 5.2 is text-only. It does not accept image, audio, or video input. If your application needs vision, you will need a multimodal model. GLM 5.2's strengths are coding (90%+ HumanEval, 62.1% SWE-bench Pro) and long-context reasoning.

What is the token limit per request?

The context window is 1,048,576 tokens (1M). This covers input tokens (messages, system prompt, tool definitions) plus output tokens combined. For most applications, this limit is not a practical constraint — a 1M-token window fits roughly 750,000 words of text.

How does GLM 5.2 compare to GPT-4o on coding tasks?

GLM 5.2 scores 62.1% on SWE-bench Pro and 90%+ on HumanEval. For a direct benchmark comparison with other frontier models, see GLM 5.2 vs GPT-4o. At $1.40/M input versus $2.50/M for GPT-4o, GLM 5.2 offers strong coding capability at lower cost.

Can I use GLM 5.2 with LangChain or LlamaIndex?

Yes. Both frameworks support custom OpenAI-compatible endpoints. In LangChain, use ChatOpenAI(api_key="YOUR_ZAI_KEY", base_url="https://api.z.ai/v1", model="glm-5.2"). In LlamaIndex, pass the same parameters to OpenAI(...). No special adapter is needed.


Next Steps

With the basics working, here is where to go next:

  • Explore the 1M context window — pass entire codebases or long documents in a single call; see GLM 5.2 context window for strategies on chunking versus single-shot approaches
  • Compare costs with other models — GLM 5.2's $1.40/M input and 158 t/s throughput position it uniquely; GLM 5.2 pricing has a full cost comparison table
  • Build an agent loop — combine function calling with a retry-until-stop loop for multi-step reasoning tasks; GLM 5.2's 62.1% SWE-bench Pro score makes it capable for real code agents
  • Benchmark latency for your use case — TTFT of 1.54s is a median; test with your typical prompt lengths to understand p95 latency before committing to SLAs

Try GLM 5.2 — no API key needed: glm5.app/chat.


Sources

今すぐGLM 5を始めよう

GLM 5を無料でお試しください — 推論、コーディング、エージェント、画像生成を一つのプラットフォームで。