Skip to main content

Crate zai_rs

Crate zai_rs 

Source
Expand description

§ZAI-RS: Zhipu AI Rust SDK

zai-rs is a type-safe Rust SDK providing full coverage of the Zhipu AI (BigModel) API. Strongly-typed clients and models span chat completions, image generation, speech recognition, text embeddings, knowledge-base management, and more.

§Capabilities

CapabilityDescriptionModule
Chat completionsSync / async / streaming text, vision, voicemodel
Image generationText-to-imagemodel::gen_image
Video generationAsync text-to-videomodel::gen_video_async
Text-to-speechAudio synthesismodel::text_to_audio
Speech-to-textAudio transcriptionmodel::audio_to_text
Voice cloningVoice clone, list, deletemodel::voice_clone
Text embeddingsEmbeddings, reranking, tokenizationmodel::text_embedded
Content moderationSafety analysismodel::moderation
OCRHandwriting recognitionmodel::ocr
File managementUpload, list, content, deletefile
Batch processingCreate, list, retrieve, cancelbatches
Knowledge baseCRUD, document upload, retrievalknowledge
Tool callingFunction calling, web search, file parsingtool
AgentAgent creation & managementagent
Tool execution frameworkDynamic registration, execution, cachingtoolkits
Real-timeWebSocket audio/video (framework ready)realTime

§Module Structure

  • client — HTTP client, connection pool, retry strategy, error types
  • model — Data models, request/response types, model definitions, SSE parsing
  • file — File management (upload, list, content, delete)
  • batches — Batch processing (create, list, retrieve, cancel)
  • knowledge — Knowledge-base management (CRUD, document upload, retrieval)
  • tool — Tool implementations (web search, file parsing)
  • agent — Agent API (creation, chat, history)
  • toolkits — Tool execution framework (registration, execution, caching, RMCP bridge)
  • realTime — Real-time audio/video communication (WebSocket)

§Quick Start

use zai_rs::{client::http::*, model::*};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let model = GLM4_5_flash {};
    let key = std::env::var("ZHIPU_API_KEY").unwrap();
    let client = ChatCompletion::new(model, TextMessage::user("Hello"), key);
    let _resp = client.post().await?;
    Ok(())
}

§Streaming Responses

use zai_rs::{client::http::*, model::*};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let model = GLM4_5_flash {};
    let key = std::env::var("ZHIPU_API_KEY").unwrap();
    let mut client =
        ChatCompletion::new(model, TextMessage::user("Hello"), key).enable_stream();
    client
        .stream_sse_for_each(|data| {
            print!("{}", String::from_utf8_lossy(data));
        })
        .await?;
    Ok(())
}

§Feature Flags

FeatureDefaultDescription
(default)enabledCore API functionality
rmcp-kitsdisabledEnable RMCP protocol bridge for MCP tool calling
web-exampledisabledEnable axum/tower dependencies for web examples

Enable in Cargo.toml:

[dependencies]
zai-rs = { version = "0.1", features = ["rmcp-kits"] }

§Error Handling

All API calls return ZaiResult<T>, unified under the ZaiError enum:

  • ApiError — Business-level API error (with code and message)
  • NetworkError — Network / timeout error
  • JsonError — JSON serialization / deserialization error
  • RateLimitError — Rate-limit or quota exceeded
  • AuthError — Authentication / authorization error

§Design Principles

  • Compile-time type safety — trait bounds and type-state patterns ensure model/message compatibility at compile time
  • Zero-cost abstractions — marker traits and type-state patterns impose no runtime overhead
  • Consistent API style — all API clients follow a uniform builder pattern and implement the HttpClient trait

Re-exports§

pub use client::error::*;

Modules§

agent
Agent API Module
batches
Batch Processing Module
client
HTTP Client Module
file
File Management Module
knowledge
Knowledge Base Module
model
Model Module
realTime
Real-time API Module
tool
Tool Module
toolkits
Toolkits Module

Macros§

define_model_type
Macro for defining AI model types with standard implementations.
impl_message_binding
Macro for binding message types to AI models.
impl_model_markers
Macro for implementing multiple capability traits on model types.