Z.ai SDK
raw JSON → 0.2.2 verified Mon Apr 27 auth: no python
A Python SDK for accessing large model APIs from Z.ai. Version 0.2.2 supports Python >=3.8. Provides clients for chatting, streaming, and embedding with multiple models. Early stage with frequent changes.
pip install zai-sdk Common errors
error ImportError: cannot import name 'ZAI' from 'zai_sdk' ↓
cause Trying to import from zai_sdk instead of zai.
fix
Correct import: from zai import ZAI
error AttributeError: 'NoneType' object has no attribute 'create' ↓
cause API key not set or client not initialized correctly.
fix
Set ZAI_API_KEY environment variable or pass api_key to ZAI(api_key='...')
Warnings
gotcha The package name on PyPI is 'zai-sdk' but the import is 'from zai'. Do not use 'import zai_sdk'. ↓
fix Use pip install zai-sdk, then from zai import ZAI
breaking Version 0.2.x changed the client initialization signature. Old code used ZAI() with no arguments, now requires api_key parameter. ↓
fix Pass api_key to ZAI(api_key='your_key')
deprecated The method 'client.complete()' is deprecated in favor of 'client.chat.completions.create()'. ↓
fix Use client.chat.completions.create(model=..., messages=...)
Imports
- ZAI wrong
import zaicorrectfrom zai import ZAI - ChatMessage
from zai import ChatMessage
Quickstart
import os
from zai import ZAI
client = ZAI(api_key=os.environ.get('ZAI_API_KEY', ''))
response = client.chat.completions.create(
model="zai-gpt-4o",
messages=[{"role": "user", "content": "Hello"}]
)
print(response.choices[0].message.content)