xai-openapi

Crates.ioxai-openapi
lib.rsxai-openapi
version0.1.1
created_at2025-12-12 17:15:01.493734+00
updated_at2025-12-12 17:20:32.710684+00
descriptionRust types for the xAI API (Grok models)
homepage
repositoryhttps://github.com/whb07/xai_openapi
max_upload_size
id1981888
size465,910
william bright (whb07)

documentation

README

xai-openapi

Crates.io Documentation CI License

Rust types for the xAI API, including support for Grok models.

All types are generated from the OpenAPI specification included in this repository.

Installation

Add this to your Cargo.toml:

[dependencies]
xai-openapi = "0.1"

Or use cargo:

cargo add xai-openapi

Usage

This crate provides type definitions only. You'll need to bring your own HTTP client (e.g., reqwest, ureq).

use xai_openapi::{ChatRequest, Message};

// Create a chat completion request
let request = ChatRequest {
    model: Some("grok-3".to_string()),
    messages: vec![
        Message::System {
            content: "You are a helpful assistant.".into(),
            name: None,
        },
        Message::User {
            content: "Hello!".into(),
            name: None,
        },
    ],
    ..Default::default()
};

// Serialize to JSON for your HTTP client
let json = serde_json::to_string(&request).unwrap();

Tool Calling

use xai_openapi::{ChatRequest, Message, Tool, FunctionDefinition};
use serde_json::json;

let request = ChatRequest {
    model: Some("grok-3".to_string()),
    messages: vec![
        Message::User {
            content: "What's the weather in San Francisco?".into(),
            name: None,
        },
    ],
    tools: Some(vec![
        Tool::Function {
            function: FunctionDefinition {
                name: "get_weather".to_string(),
                description: Some("Get the current weather".to_string()),
                parameters: json!({
                    "type": "object",
                    "properties": {
                        "location": { "type": "string" }
                    },
                    "required": ["location"]
                }),
                strict: None,
            },
        },
    ]),
    ..Default::default()
};

Embeddings

use xai_openapi::embeddings::{EmbeddingRequest, EmbeddingInput};

let request = EmbeddingRequest {
    model: Some("v1".to_string()),
    input: Some(EmbeddingInput::String("Hello, world!".to_string())),
    ..Default::default()
};

Image Generation

use xai_openapi::images::GenerateImageRequest;

let request = GenerateImageRequest {
    model: Some("grok-2-image".to_string()),
    prompt: Some("A sunset over mountains".to_string()),
    n: Some(1),
    ..Default::default()
};

Features

std (default)

Enables standard library support. Uses std::collections::HashMap.

no_std Support

This crate supports no_std environments. Disable default features and the crate will use hashbrown::HashMap instead:

[dependencies]
xai-openapi = { version = "0.1", default-features = false }

API Coverage

Module Endpoint Description
chat /v1/chat/completions Chat completions with streaming support
responses /v1/responses Responses API (OpenAI-compatible)
embeddings /v1/embeddings Text embeddings
images /v1/images/generations Image generation and editing
models /v1/models Model information and listing
messages /v1/messages Anthropic-compatible messages API
search /v1/documents/search Document search and retrieval
tokenize /v1/tokenize-text Text tokenization
tools - Tool/function calling types
usage - Token usage tracking
common - Shared types (Content, ImageUrl, etc.)

Type Conventions

All types follow these conventions:

  • Derive Clone, Debug, Default, PartialEq, Serialize, Deserialize
  • Optional fields use Option<T> with #[serde(skip_serializing_if = "Option::is_none")]
  • Enums use appropriate serde attributes (#[serde(tag = "type")], #[serde(untagged)], etc.)

Re-exports

Common types are re-exported at the crate root for convenience:

pub use chat::{ChatRequest, ChatResponse, Message};
pub use common::{Content, ImageUrl, StreamOptions};
pub use embeddings::{EmbeddingRequest, EmbeddingResponse};
pub use images::{GenerateImageRequest, GeneratedImageResponse};
pub use messages::{MessageRequest, MessageResponse};
pub use models::{LanguageModel, ListModelsResponse, Model};
pub use responses::{ModelRequest, ModelResponse};
pub use tools::{FunctionDefinition, Tool, ToolCall, ToolChoice};
pub use usage::Usage;

Minimum Supported Rust Version (MSRV)

This crate requires Rust 1.75 or later.

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

License

Licensed under either of:

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Commit count: 0

cargo fmt