Skip to main content
Third-party models

GLM

Call GLM models through the OpenAI-compatible API or DashScope SDK on QwenCloud.

Quick start

glm-5.2 and glm-5.2-fast-preview are the latest GLM models with 1M context length. They support thinking and non-thinking modes via the enable_thinking parameter. Run the following code to call glm-5.2 in thinking mode. Prerequisites: obtain an API key and configure it as an environment variable. If calling via SDK, install the OpenAI or DashScope SDK.
  • OpenAI compatible
  • DashScope
  • Anthropic compatible
The enable_thinking parameter is not a standard OpenAI parameter. In the OpenAI Python SDK, pass it via extra_body; in the Node.js SDK, pass it as a top-level parameter.
  • Python
  • Node.js
  • curl
Example code
from openai import OpenAI
import os

client = OpenAI(
  api_key=os.getenv("DASHSCOPE_API_KEY"),
  base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
)

messages = [{"role": "user", "content": "Who are you?"}]
completion = client.chat.completions.create(
  model="glm-5.2",
  messages=messages,
  extra_body={"enable_thinking": True},
  stream=True,
  stream_options={"include_usage": True},
)

reasoning_content = ""
answer_content = ""
is_answering = False
print("\n" + "=" * 20 + " Thinking " + "=" * 20 + "\n")

for chunk in completion:
  if not chunk.choices:
    print("\n" + "=" * 20 + " Token Usage " + "=" * 20 + "\n")
    print(chunk.usage)
    continue

  delta = chunk.choices[0].delta

  if hasattr(delta, "reasoning_content") and delta.reasoning_content is not None:
    if not is_answering:
      print(delta.reasoning_content, end="", flush=True)
    reasoning_content += delta.reasoning_content

  if hasattr(delta, "content") and delta.content:
    if not is_answering:
      print("\n" + "=" * 20 + " Response " + "=" * 20 + "\n")
      is_answering = True
    print(delta.content, end="", flush=True)
    answer_content += delta.content

Streaming tool calling

glm-5.2, glm-5.2-fast-preview, glm-5.1, glm-5, glm-4.7, and glm-4.6 support the tool_stream parameter (boolean, default false), effective only when stream is true. When enabled, function calling arguments are returned incrementally across multiple chunks rather than all at once.
streamtool_streamtool_call behavior
truetruearguments returned incrementally across chunks
truefalse (default)arguments returned in a single chunk
falsetrue/falsetool_stream has no effect
  • OpenAI compatible
  • DashScope
  • Python
  • Node.js
  • curl
from openai import OpenAI
import os

client = OpenAI(
  api_key=os.getenv("DASHSCOPE_API_KEY"),
  base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
)

tools = [
  {
    "type": "function",
    "function": {
      "name": "get_weather",
      "description": "Get weather information for a city",
      "parameters": {
        "type": "object",
        "properties": {
          "city": {"type": "string", "description": "City name"}
        },
        "required": ["city"]
      }
    }
  }
]

messages = [{"role": "user", "content": "What's the weather like in Singapore?"}]

completion = client.chat.completions.create(
  model="glm-5.2",
  tools=tools,
  messages=messages,
  extra_body={"tool_stream": True},
  stream=True,
  stream_options={"include_usage": True},
)

for chunk in completion:
  if chunk.choices:
    delta = chunk.choices[0].delta
    if hasattr(delta, 'content') and delta.content:
      print(f"[content] {delta.content}")
    if hasattr(delta, 'tool_calls') and delta.tool_calls:
      for tc in delta.tool_calls:
        print(f"[tool_call] id={tc.id}, name={tc.function.name}, args={tc.function.arguments}")
    if chunk.choices[0].finish_reason:
      print(f"[finish_reason] {chunk.choices[0].finish_reason}")
  if not chunk.choices and chunk.usage:
    print(f"[usage] {chunk.usage}")

Reasoning effort (reasoning_effort)

glm-5.2, glm-5.2-fast-preview, and glm-5.1 have thinking mode enabled by default. The model first outputs the thinking process (reasoning_content) and then provides the final answer. You can use the reasoning_effort parameter to adjust the reasoning depth. A higher value indicates more thorough thinking. Supported values vary by model. Passing an unsupported value returns an invalid_parameter_error error.
ModelAvailable values
glm-5.2none (no reasoning), minimal, low, medium, high, xhigh, max (highest)
glm-5.2-fast-previewnone (no reasoning), minimal, low, medium, high, xhigh, max (highest)
glm-5.1none, minimal, low, medium, high, xhigh (highest, max is not supported)
To disable thinking mode, set enable_thinking to false in OpenAI compatible or DashScope mode. This parameter has higher priority than reasoning_effort.
Anthropic compatible mode does not support the reasoning_effort parameter. To obtain thinking content, use the native Anthropic thinking parameter: {"thinking":{"type":"enabled","budget_tokens":1024}}.
  • OpenAI compatible
  • DashScope
  • Python
  • Node.js
  • curl
from openai import OpenAI
import os

client = OpenAI(
  api_key=os.getenv("DASHSCOPE_API_KEY"),
  base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
)

completion = client.chat.completions.create(
  model="glm-5.2",
  messages=[{"role": "user", "content": "Which is larger, 9.9 or 9.11?"}],
  reasoning_effort="high",
)
print(completion.choices[0].message.content)

Clear thinking history (clear_thinking)

The clear_thinking parameter controls whether the reasoning_content (thinking process) from previous turns is included as context input for the model in multi-turn conversations. Only GLM series models support this parameter.
  • true: Ignore reasoning_content from previous turns. Only visible text, tool calls, and results are used as context, reducing context length and cost.
  • false (default): Retain reasoning_content from previous turns and pass it to the model as part of the context. To enable Preserved Thinking, you must pass the complete, unmodified reasoning_content from previous turns in the original order. Missing, trimmed, rewritten, or reordered content may degrade effectiveness or prevent it from working.
This parameter only affects the thinking content from previous turns. It does not change whether the model generates or outputs thinking in the current turn.
The following example uses the same multi-turn messages (with reasoning_content in the assistant messages). When clear_thinking is set to true, the historical thinking content is excluded from context, resulting in fewer prompt_tokens compared to false (default). The actual difference depends on the length of the historical reasoning_content.
  • OpenAI compatible
  • Python
  • curl
from openai import OpenAI
import os
client = OpenAI(
  api_key=os.getenv("DASHSCOPE_API_KEY"),
  base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
)
messages = [
  {"role": "user", "content": "What is 15 * 23?"},
  {"role": "assistant", "content": "15 times 23 equals 345.", "reasoning_content": "15 * 23 = 345"},
  {"role": "user", "content": "Now add 55 to that."},
  {"role": "assistant", "content": "345 plus 55 equals 400.", "reasoning_content": "345 + 55 = 400"},
  {"role": "user", "content": "What were the intermediate results?"},
]
completion = client.chat.completions.create(
  model="glm-5.2",
  messages=messages,
  extra_body={
    "thinking": {
      "type": "enabled",
      "clear_thinking": False  # False = retain thinking content
    }
  }
)
print(completion.usage.prompt_tokens)  # fewer when true vs false

Responses API

glm-5.2 supports calls through the OpenAI-compatible Responses API. When calling the Responses API, you can add the web_search (Web search), web_extractor (Web extractor), and code_interpreter (Code interpreter) tools to the tools parameter.
  • Python
  • Node.js
  • curl
Example code
from openai import OpenAI
import os

client = OpenAI(
  api_key=os.getenv("DASHSCOPE_API_KEY"),
  base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
)

response = client.responses.create(
  model="glm-5.2",
  input="Hello! Please introduce yourself in one sentence.",
  # Optional: enable the web search, web extractor, and code interpreter tools
  tools=[
    {"type": "web_search"},
    {"type": "web_extractor"},
    {"type": "code_interpreter"},
  ],
)

# Get the model response
print(response.output_text)

Other features

ModelMulti-turnFunction callingWeb searchContext cache
glm-5.2✓ (non-thinking mode only)✓ (implicit only)
glm-5.2-fast-preview✓ (non-thinking mode only)✓ (implicit only)
glm-5.1✓ (non-thinking mode only)✓ (explicit and implicit)

Parameter defaults

Modelenable_thinkingtemperaturetop_ptop_krepetition_penalty
glm-5.2true1.00.95201.0
glm-5.1true1.00.95201.0

Precautions

Cloud-deployed third-party open-source models (such as glm-5.2) handle hyperparameters differently from the model's official version: the official version performs threshold validation on hyperparameters and falls back to default values when thresholds are exceeded; the cloud-deployed version directly passes through user-provided parameter values without threshold validation. Therefore, improper hyperparameter settings (such as setting repetition_penalty to 0.1) may cause unexpected output (such as repeated printing). We recommend using the default hyperparameter values (see the default parameter values table above) for third-party open-source models and avoiding custom parameters.
GLM - QwenCloud