Skip to main content
Z.AI provides interfaces compatible with OpenAI API, which means you can use existing OpenAI SDK code and seamlessly switch to Z.AI’s model services by simply modifying the API key and base URL. This compatibility allows you to:
  • Quickly migrate existing OpenAI applications
  • Use familiar development patterns and tools
  • Enjoy the powerful capabilities of Z.AI models
  • Maintain code consistency and maintainability
In some scenarios, there are still differences between Z.AI and OpenAI interfaces, but this does not affect overall compatibility.

Core Advantages

Zero Learning Cost

If you are already familiar with OpenAI SDK, you can start using it immediately

Quick Migration

Existing OpenAI applications can be quickly migrated to Z.AI platform

Ecosystem Compatibility

Compatible with various tools and frameworks in the OpenAI ecosystem

Continuous Updates

Follow OpenAI SDK updates to maintain latest feature support

Environment Requirements

Python Version

Python 3.7.1 or higher

OpenAI SDK

OpenAI SDK version 1.0.0 or higher
Please ensure using OpenAI SDK 1.0.0 or higher, older versions may have compatibility issues.

Install OpenAI Python SDK

Install using pip

Install using poetry

Quick Start

Get API Key

  1. Access Z.AI Open Platform, Register or Login.
  2. Create an API Key in the API Keys management page.
  3. Copy your API Key for use.
It is recommended to set the API Key as an environment variable: export ZAI_API_KEY=your-api-key

Create Client

Quick Start Examples

Basic Chat

Streaming Response

Multi-turn Conversation

Advanced Features

Thinking Mode

In thinking mode, GLM-4.6, GLM-4.5 and GLM-4.5-Air can solve complex reasoning problems, including mathematics, science, and logic problems. The param thinking.type can be either enabled or disabled.

Function Calling

Parameter Configuration

Common Parameters

Note: The temperature parameter range is (0,1), do_sample = False (temperature = 0) is not applicable in OpenAI calls.

Best Practices

Performance Optimization

  • Use connection pooling and session reuse
  • Set reasonable timeout values
  • Implement async calls for high concurrency
  • Cache frequently used responses

Cost Control

  • Set reasonable max_tokens limits
  • Use appropriate models (don’t overuse powerful models)
  • Implement request deduplication
  • Monitor API usage

Security

  • Use environment variables to store API keys
  • Implement input validation and filtering
  • Log and monitor API calls
  • Rotate API keys regularly

Reliability

  • Implement retry mechanisms and error handling
  • Set reasonable timeout values
  • Monitor API status and response times
  • Prepare fallback solutions

Migration Guide

Migrating from OpenAI

If you’re already using OpenAI API, migrating to Z.AI is very simple:

Getting Help

API Documentation

View complete API interface documentation

OpenAI Official Documentation

Refer to OpenAI official documentation for more usage
Z.AI is committed to maintaining compatibility with OpenAI API. If you encounter any issues during migration, please contact our technical support team.