ZhipuAI Python SDK

2.1.5.20250825 · active · verified Thu Apr 16

ZhipuAI is the official Python SDK for accessing the large model APIs from ZhipuAI Open Platform. It provides a convenient, type-safe, and high-performance interface for interacting with ZhipuAI models like GLM series, supporting chat completions, embeddings, and other AI capabilities. The library maintains an active development status, with frequent updates indicated by its versioning scheme, reflecting ongoing enhancements and feature additions to the ZhipuAI platform.

Common errors

Warnings

Install

Imports

Quickstart

Initializes the ZhipuAI client using an API key (preferably from an environment variable) and performs a simple chat completion request with the 'glm-4' model.

import os
from zhipuai import ZhipuAI

# It's recommended to set your API key as an environment variable (ZHIPUAI_API_KEY)
# For quick testing, you can pass it directly, but avoid in production.
api_key = os.environ.get('ZHIPUAI_API_KEY', 'YOUR_API_KEY_HERE')

if api_key == 'YOUR_API_KEY_HERE':
    print("Warning: Please set the ZHIPUAI_API_KEY environment variable or replace 'YOUR_API_KEY_HERE'.")
    exit()

client = ZhipuAI(api_key=api_key)

try:
    response = client.chat.completions.create(
        model="glm-4",
        messages=[
            {"role": "user", "content": "Hello, ZhipuAI!"}
        ]
    )
    print("ZhipuAI Response:")
    print(response.choices[0].message.content)
except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →