Official Python SDK for xAI's Grok models — gRPC-based client for chat, image generation, and agentic tool use. Also supports OpenAI-compatible REST API.
pip install xai-sdkVerified import paths — ran on the pinned version, not inferred.
Chat completion via OpenAI-compatible interface (recommended for portability)
Migrate to native xai-sdk, OpenAI-compatible interface, or Vercel AI SDK
Use Agent Tools API with xai.responses(modelId) — web_search, x_search, code_execution now server-side tools
Use grok-3, grok-3-mini-beta, grok-4, or grok-4-fast explicitly
Catch grpc.RpcError specifically. Default timeout is 900s. Set client = Client(timeout=300) to override.
Only pass reasoning_effort to mini/reasoning variants
pip install xai-sdk (not grok, not xai-grok, not xai-grok-sdk — all are third-party or unrelated)
Use pip install xai-sdk from pypi.org/project/xai-sdk
Ensure your Python environment is 3.10 or newer.
Set the XAI_API_KEY environment variable before running the application, e.g., export XAI_API_KEY='your_api_key_here' or pass it directly in your deployment environment configuration.
Run `pip install xai-sdk` to install the library.
Ensure your `XAI_API_KEY` environment variable is correctly set with a valid API key, or pass it directly to the `XaiClient` constructor: `xai_client = XaiClient(api_key="your_api_key_here")`.
Access chat completions via `xai_client.chat.completions.create(...)` instead of `xai_client.chat.create(...)`.
Ensure the `messages` parameter is a list of dictionaries, for example: `messages=[{"role": "user", "content": "Hello!"}]`.