{"id":7880,"library":"zhipuai","title":"ZhipuAI Python SDK","description":"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.","status":"active","version":"2.1.5.20250825","language":"en","source_language":"en","source_url":"https://github.com/MetaGLM/zhipuai-sdk-python-v4","tags":["AI","LLM","ZhipuAI","API client","natural language processing","generative AI"],"install":[{"cmd":"pip install zhipuai","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Required Python version for the SDK.","package":"python","version":">=3.8","optional":false},{"reason":"HTTP client for making API requests.","package":"httpx","version":">=0.23.0","optional":false},{"reason":"Data validation and serialization for API models.","package":"pydantic","version":">=1.9.0,<3.0.0","optional":false},{"reason":"Enhanced type hints support.","package":"typing-extensions","version":">=4.0.0","optional":false}],"imports":[{"symbol":"ZhipuAI","correct":"from zhipuai import ZhipuAI"}],"quickstart":{"code":"import os\nfrom zhipuai import ZhipuAI\n\n# It's recommended to set your API key as an environment variable (ZHIPUAI_API_KEY)\n# For quick testing, you can pass it directly, but avoid in production.\napi_key = os.environ.get('ZHIPUAI_API_KEY', 'YOUR_API_KEY_HERE')\n\nif api_key == 'YOUR_API_KEY_HERE':\n    print(\"Warning: Please set the ZHIPUAI_API_KEY environment variable or replace 'YOUR_API_KEY_HERE'.\")\n    exit()\n\nclient = ZhipuAI(api_key=api_key)\n\ntry:\n    response = client.chat.completions.create(\n        model=\"glm-4\",\n        messages=[\n            {\"role\": \"user\", \"content\": \"Hello, ZhipuAI!\"}\n        ]\n    )\n    print(\"ZhipuAI Response:\")\n    print(response.choices[0].message.content)\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")","lang":"python","description":"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."},"warnings":[{"fix":"Review the official SDK documentation for v4.x to update API key initialization and method calls, especially for `chat.completions.create` and error handling logic.","message":"When upgrading from v3.x to v4.x (current major version), significant changes were introduced including simplified API key configuration, modified method signatures for improved type safety, and more specific exception types for error handling. Code written for v3.x will likely require adaptation.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Monitor ZhipuAI official announcements for migration guides and consider adopting the `z-ai-sdk-python` when its features and stability meet project requirements.","message":"ZhipuAI has released a new Python SDK, `z-ai-sdk-python`, and recommends using it for better, faster, and long-term support. While `zhipuai` is currently active, users should be aware of this future-oriented recommendation.","severity":"gotcha","affected_versions":"All"},{"fix":"Store your ZhipuAI API key in an environment variable (e.g., `ZHIPUAI_API_KEY`) and retrieve it using `os.environ.get('ZHIPUAI_API_KEY')`.","message":"API keys should never be hardcoded in your application. They are sensitive credentials and should be managed securely, ideally using environment variables.","severity":"gotcha","affected_versions":"All"},{"fix":"Explicitly specify a supported model (e.g., 'glm-4') in your `client.chat.completions.create` call. Refer to ZhipuAI's model documentation for the latest available models.","message":"The SDK might default to specific models in some integrations that could become deprecated or invalid over time, leading to 'Model does not exist' errors.","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure that messages within tool-use sequences adhere to the expected format, avoiding empty user content where the schema requires it, or restructure message chains to normalize them before sending.","message":"Using consecutive `tool_use`/`tool_result` chains with empty user content can result in 'API Error 1214 — Invalid message format'.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Verify the model name against the ZhipuAI official documentation. Ensure you are using a currently supported model (e.g., `glm-4`).","cause":"Attempting to use a model that is deprecated, misspelled, or not available for your account/region.","error":"APIStatusError: error code:1211 Model does not exist"},{"fix":"Review the structure of your `messages` array, especially when integrating tool calls. Ensure all required fields for each message type are present and correctly formatted, avoiding empty content where not allowed.","cause":"The `messages` array in a chat completion request contains an invalid structure, often related to `tool_use` or `tool_result` roles with missing or incorrectly formatted `content` fields.","error":"APIStatusError: error code:1214 Invalid message format"},{"fix":"Increase the `timeout` parameter when initializing the `ZhipuAI` client or when making the request. You can also configure `max_retries` for transient network issues. Example: `client = ZhipuAI(api_key=api_key, timeout=httpx.Timeout(timeout=120.0, connect=10.0), max_retries=5)`.","cause":"The API request took longer than the configured timeout duration, often due to network issues, complex model computations, or high load on the API.","error":"zhipuai.APITimeoutError: Request timed out"}]}