freshcrate
Skin:/
Home > RAG & Memory > xai-sdk

xai-sdk

The official Python SDK for the xAI API

Why this rank:Strong adoptionRelease freshnessHealthy release cadence

Description

<div align="center"> <img src="https://avatars.githubusercontent.com/u/130314967?s=200&v=4" alt="xAI Logo" width="100" /> <h1>xAI Python SDK</h1> <p>The official Python SDK for xAI's APIs</p> <a href="https://pypi.org/project/xai-sdk"> <img src="https://img.shields.io/pypi/v/xai-sdk" alt="PyPI Version" /> </a> <a href=""> <img src="https://img.shields.io/pypi/l/xai-sdk" alt="License" /> </a> <a href=""> <img src="https://img.shields.io/pypi/pyversions/xai-sdk" alt="Python Version" /> </a> </div> <br> The xAI Python SDK is a gRPC-based Python library for interacting with xAI's APIs. Built for Python 3.10 and above, it offers both **synchronous** and **asynchronous** clients. Whether you're generating text, images, or structured outputs, the xAI SDK is designed to be intuitive, robust, and developer-friendly. ## Documentation Comprehensive documentation is available at [docs.x.ai](https://docs.x.ai). Explore detailed guides, API references, and tutorials to get the most out of the xAI SDK. ## Installation Install from PyPI with pip. ```bash pip install xai-sdk ``` Alternatively you can also use [uv](https://docs.astral.sh/uv/) ```bash uv add xai-sdk ``` ### Requirements Python 3.10 or higher is required to use the xAI SDK. ## Usage The xAI SDK supports both synchronous (`xai_sdk.Client`) and asynchronous (`xai_sdk.AsyncClient`) clients. For a complete set of examples demonstrating the SDK's capabilities, including authentication, chat, image generation, function calling, and more, refer to the [examples folder](https://github.com/xai-org/xai-sdk-python/tree/main/examples). ### Client Instantiation To use the xAI SDK, you need to instantiate either a synchronous or asynchronous client. By default, the SDK looks for an environment variable named `XAI_API_KEY` for authentication. If this variable is set, you can instantiate the clients without explicitly passing the API key: ```python from xai_sdk import Client, AsyncClient # Synchronous client sync_client = Client() # Asynchronous client async_client = AsyncClient() ``` If you prefer to explicitly pass the API key, you can do so using `os.getenv` or by loading it from a `.env` file using the `python-dotenv` package: ```python import os from dotenv import load_dotenv from xai_sdk import Client, AsyncClient load_dotenv() api_key = os.getenv("XAI_API_KEY") sync_client = Client(api_key=api_key) async_client = AsyncClient(api_key=api_key) ``` Make sure to set the `XAI_API_KEY` environment variable or load it from a `.env` file before using the SDK. This ensures secure handling of your API key without hardcoding it into your codebase. ### Multi-Turn Chat (Synchronous) The xAI SDK supports multi-turn conversations with a simple `append` method to manage conversation history, making it ideal for interactive applications. First, create a `chat` instance, start `append`ing messages to it, and finally call `sample` to yield a response from the model. While the underlying APIs are still stateless, this approach makes it easy to manage the message history. ```python from xai_sdk import Client from xai_sdk.chat import system, user client = Client() chat = client.chat.create( model="grok-3", messages=[system("You are a pirate assistant.")] ) while True: prompt = input("You: ") if prompt.lower() == "exit": break chat.append(user(prompt)) response = chat.sample() print(f"Grok: {response.content}") chat.append(response) ``` ### Multi-Turn Chat (Asynchronous) For async usage, simply import `AsyncClient` instead of `Client`. ```python import asyncio from xai_sdk import AsyncClient from xai_sdk.chat import system, user async def main(): client = AsyncClient() chat = client.chat.create( model="grok-3", messages=[system("You are a pirate assistant.")] ) while True: prompt = input("You: ") if prompt.lower() == "exit": break chat.append(user(prompt)) response = await chat.sample() print(f"Grok: {response.content}") chat.append(response) if __name__ == "__main__": asyncio.run(main()) ``` ### Streaming The xAI SDK supports streaming responses, allowing you to process model outputs in real-time, which is ideal for interactive applications like chatbots. The `stream` method returns a tuple containing `response` and `chunk`. The chunks contain the text deltas from the stream, while the `response` variable automatically accumulates the response as the stream progresses. ```python from xai_sdk import Client from xai_sdk.chat import user client = Client() chat = client.chat.create(model="grok-3") while True: prompt = input("You: ") if prompt.lower() == "exit": break chat.append(user(prompt)) print("Grok: ", end="", flush=True) for response, chunk in chat.stream(): print(chunk.content, end="", flush=True) print() chat.append(response) ``` ### Image

Release History

VersionChangesUrgencyDate
v1.17.0## What's Changed * feat: add service_tier support for priority processing by @double-di in https://github.com/xai-org/xai-sdk-python/pull/163 * chore: bump version to 1.17.0 by @double-di in https://github.com/xai-org/xai-sdk-python/pull/165 **Full Changelog**: https://github.com/xai-org/xai-sdk-python/compare/v1.16.0...v1.17.0High6/12/2026
v1.16.0## What's Changed * Add Imagine file storage & file-ID inputs to image/video generation by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/160 * Add public file URLs and list filtering to the files client by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/161 * Add new grok imagine models to types/model.py for editor autocomplete by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/162 * Prepare to release v1.16.0 of the xAI Python SDK by @Omar-V2 in https://githubHigh6/10/2026
v1.15.0## What's Changed * Add grok-build-0.1 to ChatModel by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/155 * Add context compaction support by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/156 * Bump version from 1.14.0 to 1.15.0 by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/157 **Full Changelog**: https://github.com/xai-org/xai-sdk-python/compare/v1.14.0...v1.15.0High5/30/2026
v1.14.0## What's Changed * Add enable_image_search to web_search and SERVER_SIDE_TOOL_IMAGE_SEARCH enum by @ushiromiya-lion in https://github.com/xai-org/xai-sdk-python/pull/152 * Bump version to 1.14.0 by @ushiromiya-lion in https://github.com/xai-org/xai-sdk-python/pull/153 ## New Contributors * @ushiromiya-lion made their first contribution in https://github.com/xai-org/xai-sdk-python/pull/152 **Full Changelog**: https://github.com/xai-org/xai-sdk-python/compare/v1.13.0...v1.14.0High5/27/2026
v1.13.0## What's Changed * feat: add prepare_extension() for batch video extension by @double-di in https://github.com/xai-org/xai-sdk-python/pull/141 **Full Changelog**: https://github.com/xai-org/xai-sdk-python/compare/v1.12.2...v1.13.0High5/16/2026
v1.12.2## What's Changed * Add none and medium reasoning effort levels by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/139 * Bump version from 1.12.1 to 1.12.2 by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/140 **Full Changelog**: https://github.com/xai-org/xai-sdk-python/compare/v1.12.1...v1.12.2High5/7/2026
v1.12.1## What's Changed * Update the CHANGELOG with missing entries by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/135 * Add grok-4.3 to ChatModel by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/137 * Bump version from 1.12.0 to 1.12.1 by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/138 **Full Changelog**: https://github.com/xai-org/xai-sdk-python/compare/v1.12.0...v1.12.1High4/30/2026
v1.12.0## What's Changed * docs: add video generation and extension documentation to README by @double-di in https://github.com/xai-org/xai-sdk-python/pull/127 * Add collection descriptions, field definition updates, and bytes chunking by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/130 * Add `expires_after` to file uploads, drop `team_id` from `File` by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/131 * Add `cost_usd` tracking, model migration, example fixes by @Omar-V2 in High4/29/2026
1.11.0Imported from PyPI (1.11.0)Low4/21/2026
v1.11.0## What's Changed * Support inline file attachments by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/122 * Add url support for FileContent by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/123 * Bump version from 1.10.0 to 1.11.0 by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/124 **Full Changelog**: https://github.com/xai-org/xai-sdk-python/compare/v1.10.0...v1.11.0Medium3/27/2026
v1.11.0## What's Changed * Support inline file attachments by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/122 * Add url support for FileContent by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/123 * Bump version from 1.10.0 to 1.11.0 by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/124 **Full Changelog**: https://github.com/xai-org/xai-sdk-python/compare/v1.10.0...v1.11.0Medium3/27/2026
v1.11.0## What's Changed * Support inline file attachments by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/122 * Add url support for FileContent by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/123 * Bump version from 1.10.0 to 1.11.0 by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/124 **Full Changelog**: https://github.com/xai-org/xai-sdk-python/compare/v1.10.0...v1.11.0Medium3/27/2026
v1.11.0## What's Changed * Support inline file attachments by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/122 * Add url support for FileContent by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/123 * Bump version from 1.10.0 to 1.11.0 by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/124 **Full Changelog**: https://github.com/xai-org/xai-sdk-python/compare/v1.10.0...v1.11.0Low3/27/2026
v1.11.0## What's Changed * Support inline file attachments by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/122 * Add url support for FileContent by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/123 * Bump version from 1.10.0 to 1.11.0 by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/124 **Full Changelog**: https://github.com/xai-org/xai-sdk-python/compare/v1.10.0...v1.11.0Low3/27/2026
v1.11.0## What's Changed * Support inline file attachments by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/122 * Add url support for FileContent by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/123 * Bump version from 1.10.0 to 1.11.0 by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/124 **Full Changelog**: https://github.com/xai-org/xai-sdk-python/compare/v1.10.0...v1.11.0Low3/27/2026
v1.10.0## What's Changed * Add video extension API and reference_image_urls support by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/120 * Prepare to release v1.10.0 of the xAI Python SDK by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/121 **Full Changelog**: https://github.com/xai-org/xai-sdk-python/compare/v1.9.1...v1.10.0Medium3/24/2026
v1.9.1## What's Changed * Update grok-4.20 and multi-agent model variants in ChatModel by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/117 * Bump version from 1.9.0 to 1.9.1 by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/118 **Full Changelog**: https://github.com/xai-org/xai-sdk-python/compare/v1.9.0...v1.9.1Low3/19/2026
v1.9.0## What's Changed * feat: add image/video batch support and input_file_id by @double-di in https://github.com/xai-org/xai-sdk-python/pull/112 * Fix unknown status handling in polling loops by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/113 * Use model type aliases in image and video method signatures by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/114 * Prepare to release v1.9.0 of the xAI Python SDK by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/1Low3/19/2026
v1.8.2## What's Changed * Handle video generation failure case by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/110 * Prepare to release v1.8.2 of the xAI Python SDK by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/111 **Full Changelog**: https://github.com/xai-org/xai-sdk-python/compare/v1.8.1...v1.8.2Low3/16/2026
v1.8.1## What's Changed * Update grok-4.20 beta and multi-agent model variants in ChatModel by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/107 * Bump version from 1.8.0 to 1.8.1 by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/109 **Full Changelog**: https://github.com/xai-org/xai-sdk-python/compare/v1.8.0...v1.8.1Low3/11/2026
v1.8.0## What's Changed * Add grok-imagine-image-pro to ImageGenerationModel by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/103 * Add agent_count parameter to chat API for multi-agent models by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/104 * Add grok-4.20 experimental beta and multi-agent model variants to ChatModel by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/105 * Bump version from 1.7.0 to 1.8.0 by @shawnthapa in https://github.com/xai-org/xLow3/5/2026
v1.7.0## What's Changed * Deprecate `BaseImageResponse.prompt` after `up_sampled_prompt` proto removal by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/96 * Add 2K image resolution support by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/97 * Reduce default PollTimer poll interval and introduce default video polling settings by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/98 * Add support for multi-reference image editing with new `image_urls` parameter byLow2/18/2026
v1.6.1## What's Changed * Update changelog by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/90 * Add Imagine image/video API support by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/91 * Bump version from 1.6.0 to 1.6.1 by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/92 **Full Changelog**: https://github.com/xai-org/xai-sdk-python/compare/v1.6.0...v1.6.1Low1/29/2026
v1.6.0## What's Changed * Add user-location support for web-search tool by @mark-xai in https://github.com/xai-org/xai-sdk-python/pull/80 * Add `tool_call_id` field by @mark-xai in https://github.com/xai-org/xai-sdk-python/pull/81 * Add `developer` role by @mark-xai in https://github.com/xai-org/xai-sdk-python/pull/86 * Add batch API support (#46) by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/82 * Instrument Collections Methods by @aaditjuneja in https://github.com/xai-org/xai-sdk-pythLow1/27/2026
v1.5.0## What's Changed * Add `end_index`, rename document search tools, and add `verbose_streaming` include option by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/71 * Add inline citations support to Chunk and Response classes by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/73 * Add new features to Collections API client by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/74 * Add util functions to retrieve server-side tool outputs by @mark-xai in https://gLow12/5/2025
v1.4.1## What's Changed * Update changelog following the v.1.4.0 release by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/63 * Update the metadata sent on requests to also include the language by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/62 * Update proto definitions to include new tool call status field on tool calls by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/65 * Add max_turns parameter in chat creation for agentic conversations by @shawnthapa in httpLow11/26/2025
v1.4.0## What's Changed * Add xai-sdk-version to the request metadata by @mark-xai in https://github.com/xai-org/xai-sdk-python/pull/41 * Remove double await in async interceptor by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/42 * Add context manager support and proper channel cleanup to clients by @shawnthapa in https://github.com/xai-org/xai-sdk-python/pull/43 * Add ability to exclude sensitive attributes for traces by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/44 * ReLow11/7/2025
v1.3.1## What's Changed * Support multiple outputs in chunk and response of chat service by @mark-xai in https://github.com/xai-org/xai-sdk-python/pull/39 * Bump version from v1.3.0 -> v.1.3.1 by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/40 ## New Contributors * @mark-xai made their first contribution in https://github.com/xai-org/xai-sdk-python/pull/39 **Full Changelog**: https://github.com/xai-org/xai-sdk-python/compare/v1.3.0...v1.3.1Low10/17/2025
v1.3.0## What's Changed * Update changelog by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/31 * Add proto support for server side tools by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/32 * Bump version from v1.2.0 -> v1.3.0a0 by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/33 * Add convenience functions for using new server-side tools by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/35 * Updated response handling to use 'outputs' instead of 'choicLow10/15/2025
v1.3.0a0## What's Changed * Update changelog by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/31 * Add proto support for server side tools by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/32 * Bump version from v1.2.0 -> v1.3.0a0 by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/33 **Full Changelog**: https://github.com/xai-org/xai-sdk-python/compare/v1.2.0...v1.3.0a0Low9/29/2025
v1.2.0## What's Changed * Update changelog by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/26 * Add SDK support for the Collections API by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/28 * Add support for the stateful chat API by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/29 * Bump version from v1.1.0 -> v1.2.0 by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/30 **Full Changelog**: https://github.com/xai-org/xai-sdk-python/compare/v1.1.0...v1.Low9/18/2025
v1.1.0## What's Changed * Update changelog by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/19 * Add OpenTelemetry integration for span creation by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/22 * Add support for documents semantic search by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/23 * Add telemetry examples and update README with telemetry docs by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/24 * Bump version from v1.0.1 -> v1.1.0 by @Omar-Low8/21/2025
v1.0.1## What's Changed * Update changelog by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/15 * Apply date filters correctly when using SearchParameters by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/17 * Bump version from v1.0.0 -> v1.0.1 by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/18 **Full Changelog**: https://github.com/xai-org/xai-sdk-python/compare/v1.0.0...v1.0.1Low7/24/2025
v1.0.0## What's Changed * Add support for new X source params for live search by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/10 ## New Contributors * @DaneJacobson made their first contribution in https://github.com/xai-org/xai-sdk-python/pull/11 **Full Changelog**: https://github.com/xai-org/xai-sdk-python/compare/v1.0.0rc2...v1.0.0Low7/3/2025
v1.0.0rc2## What's Changed * Update protos with new reasoning_content field added to Message proto by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/6 * Fix: Enable keep-alive on HTTP/2 connections by @jimmyfraiture in https://github.com/xai-org/xai-sdk-python/pull/7 * Bump version from v1.0.0rc1 -> v1.0.0rc2 by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/8 ## New Contributors * @jimmyfraiture made their first contribution in https://github.com/xai-org/xai-sdk-python/pull/7 *Low6/26/2025
v1.0.0rc1## What's Changed * Update SECURITY.md by @avixai in https://github.com/xai-org/xai-sdk-python/pull/1 * Support protobuf major versions v5 and v6 by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/2 * Add missing packaging dep, use hatch-fancy-pypi-readme by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/3 * Prepare for public PyPI release by @Omar-V2 in https://github.com/xai-org/xai-sdk-python/pull/5 ## New Contributors * @avixai made their first contribution in Low6/13/2025

Dependencies & License Audit

Loading dependencies...

Similar Packages

ghapiA python client for the GitHub API2.0.3
diff-coverRun coverage and linting reports on diffsv10.4.0
biopythonFreely available tools for computational molecular biology.master@2026-07-20
azure-search-documentsMicrosoft Azure Cognitive Search Client Library for Pythonazure-mgmt-consumption_11.0.0
xgrammarEfficient, Flexible and Portable Structured Generationv0.2.4

More from pypi

markitdownUtility tool for converting various files to Markdown
fastapiFastAPI framework, high performance, easy to learn, fast to code, ready for production
djangoA high-level Python web framework that encourages rapid development and clean, pragmatic design.
flaskA simple framework for building complex web applications.

More in RAG & Memory

edgequakeEdegQuake 🌋 High-performance GraphRAG inspired from LightRag written in Rust; Transform documents into intelligent knowledge graphs for superior retrieval and generation
vllmA high-throughput and memory-efficient inference and serving engine for LLMs
awesome-opensource-aiCurated list of the best truly open-source AI projects, models, tools, and infrastructure.
rust-sdkThe official Rust SDK for the Model Context Protocol