xAI SDK
A Rust SDK for xAI's API, providing type-safe gRPC clients for all xAI services including Grok language models, embeddings, image generation, and more.
Features
- Complete API Coverage: Full gRPC client implementation for all xAI services
- Type Safety: Auto-generated Rust types from Protocol Buffers
- Multiple Models: Support for all xAI language models (Grok-3, Grok-4, etc.)
- Streaming Support: Real-time streaming for chat completions and text generation
- Response Assembly: Convert streaming chunks into complete responses
- Secure: TLS encryption with automatic certificate validation
Quick Start
Prerequisites
- Rust 1.88+ installed
- xAI API key
Installation
Add to your Cargo.toml:
[]
= "0.8"
= { = "1.0", = ["full"] }
= "1.0"
Running the Examples
-
Set your API key as an environment variable:
-
Run the authentication info example:
-
Run the raw text sampling example:
-
Run the chat completion example (supports multiple modes):
# Blocking completion # Streaming completion # Streaming with assembly -
Run the multi-client example (demonstrates using multiple services with shared channel):
-
Run the interceptor composition example:
-
Run the tool calls example:
API Services
The SDK provides clients for all xAI services:
Chat Service
get_completion- Get chat completionget_completion_chunk- Stream chat completion in chunksstart_deferred_completion- Start deferred chat completionget_deferred_completion- Retrieve deferred completionget_stored_completion- Get stored chat completiondelete_stored_completion- Delete stored chat completion
Sample Service
sample_text- Raw text generationsample_text_streaming- Streaming text generation
Models Service
list_language_models- List available language modelslist_embedding_models- List embedding modelslist_image_generation_models- List image generation models
Embed Service
embed- Generate embeddings from text or images
Image Service
generate_image- Create images from text prompts
Video Service
generate_video- Create videos with deferred processingget_deferred_video- Retrieve generated videos
Auth Service
get_api_key_info- Get API key information
Billing Service
set_billing_info- Set billing information for a teamget_billing_info- Get billing information for a teamlist_payment_methods- List payment methods on fileset_default_payment_method- Set default payment methodget_amount_to_pay- Preview current billing period amountanalyze_billing_items- Analyze historical billing usagelist_invoices- List invoices for a teamlist_prepaid_balance_changes- List prepaid credit balance changestop_up_or_get_existing_pending_change- Top up prepaid creditsget_spending_limits- Get spending limitsset_soft_spending_limit- Set soft spending limit
Client Modules
The SDK is organized into focused modules, each providing easy client creation:
Available Modules
auth- Authentication servicesbilling- Billing and payment managementchat- Chat completions and streamingdocuments- Document processingembed- Text and image embeddingsimage- Image generationmodels- Model listing and informationsample- Text sampling and generationtokenize- Text tokenizationvideo- Video generation with deferred processing
Complete Example
Here's a complete example showing multiple services using the modular architecture:
use ;
use env;
use Request;
use ;
use ;
async
Streaming Utilities
The SDK provides powerful utilities for working with streaming responses:
Stream Consumer
A flexible callback system for processing streaming data:
on_chunk(chunk)- Called for each complete chunk receivedon_reasoning_start(&OutputContext)- Called once per output when the reasoning phase starts (before the first reasoning token)on_reasoning_token(&OutputContext, token: &str)- Called for each piece of reasoning contenton_reasoning_complete(&OutputContext)- Called once when the reasoning phase completes for an outputon_content_start(&OutputContext)- Called once per output when the content phase starts (before the first content token)on_content_token(&OutputContext, token: &str)- Called for each piece of response contenton_content_complete(&OutputContext)- Called once when the content phase completes for an outputon_inline_citations(&OutputContext, &[InlineCitation])- Called when inline citations are present in a deltaon_client_tool_calls(&OutputContext, &[ToolCall])- Called when client-side tool calls are presenton_server_tool_calls(&OutputContext, &[ToolCall])- Called when server-side tool calls are presenton_usage(&SamplingUsage)- Called once on the last chunk with usage statisticson_citations(&[String])- Called once on the last chunk with citations
The OutputContext provides:
total_outputs- Total number of outputs in the streamoutput_index- Index of the output this context belongs toreasoning_status- Current status of the reasoning phase (Init,Start,Pending, orComplete)content_status- Current status of the content phase (Init,Start,Pending, orComplete)
Stream Processing Functions
chat::stream::process- Process streaming responses with custom callbackschat::stream::assemble- Convert collected chunks into complete responseschat::stream::Consumer::new_static()- Empty consumer with'staticlifetime for one-line chaining:Consumer::new_static().on_content_token(...).on_usage(...)chat::stream::Consumer::with_sink(snk)- Consumer that forwards all stream activity asEvents into anySink<Event>(e.g.futures::channel::mpsc::unbounded()). Create the channel, pass the sender, then drain the receiver for a single event stream. Bounded senders apply backpressure.chat::stream::Event- Enum of streaming events:Chunk,ReasoningStart/ReasoningToken/ReasoningComplete,ContentStart/ContentToken/ContentComplete,InlineCitations,ClientToolCalls,ServerToolCalls,Citations,Usage,Complete,Error.chat::stream::Consumer::with_stdout()- Pre-configured consumer for single-output real-time outputchat::stream::Consumer::with_buffered_stdout()- Pre-configured consumer for multi-output buffered output
Interceptors
The SDK provides a flexible interceptor system for customizing request handling:
Authentication Interceptor
The auth() function creates an interceptor that adds Bearer token authentication:
use auth;
let interceptor = auth;
let client = with_interceptor.await?;
Composing Interceptors
Combine multiple interceptors using compose():
use ;
let interceptors: = vec!;
let composed = compose;
let client = with_interceptor.await?;
Note: All interceptors must be Send + Sync to ensure thread safety when used in async contexts.
ClientInterceptor Type
All client functions return ClientInterceptor, a concrete type that can be:
- Stored in structs
- Used in trait implementations
- Passed between functions without type erasure issues
- Used across thread boundaries (
Send + Sync) - safe to use intokio::spawnand other async contexts
The ClientInterceptor can be created from any impl Interceptor + Send + Sync + 'static:
use ClientInterceptor;
let interceptor = new;
// Can be used in spawned tasks
spawn;
Configuration
The SDK supports comprehensive configuration options:
- Temperature: Controls randomness (0.0 to 2.0)
- Top-p: Nucleus sampling parameter (0.0 to 1.0)
- Max tokens: Maximum tokens to generate
- Log probabilities: Enable detailed token probability logging
- Multiple completions: Generate multiple responses per request
- Stop sequences: Custom stop conditions
- Frequency/Presence penalties: Control repetition and topic diversity
Security
- TLS Encryption: Automatic HTTPS with certificate validation
- Authentication: Bearer token support for API key authentication
- Secure by Default: No manual TLS configuration required
Error Handling
Comprehensive error handling for:
- Connection errors and timeouts
- Authentication failures
- API rate limiting
- Invalid parameters
- Network issues
Development
This SDK is built using:
- Protocol Buffers: Auto-generated Rust types from xAI's
.protodefinitions - Tonic: Modern gRPC framework for Rust with async/await support
- Prost: High-performance Protocol Buffer implementation
The code is generated from xAI's official Protocol Buffer definitions, ensuring compatibility and type safety.
Changelog
See CHANGELOG.md for a detailed list of changes and new features.
License
This project is licensed under the MIT License.