| Crates.io | xai-openapi |
| lib.rs | xai-openapi |
| version | 0.1.1 |
| created_at | 2025-12-12 17:15:01.493734+00 |
| updated_at | 2025-12-12 17:20:32.710684+00 |
| description | Rust types for the xAI API (Grok models) |
| homepage | |
| repository | https://github.com/whb07/xai_openapi |
| max_upload_size | |
| id | 1981888 |
| size | 465,910 |
Rust types for the xAI API, including support for Grok models.
All types are generated from the OpenAPI specification included in this repository.
Add this to your Cargo.toml:
[dependencies]
xai-openapi = "0.1"
Or use cargo:
cargo add xai-openapi
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();
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()
};
use xai_openapi::embeddings::{EmbeddingRequest, EmbeddingInput};
let request = EmbeddingRequest {
model: Some("v1".to_string()),
input: Some(EmbeddingInput::String("Hello, world!".to_string())),
..Default::default()
};
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()
};
std (default)Enables standard library support. Uses std::collections::HashMap.
no_std SupportThis 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 }
| 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.) |
All types follow these conventions:
Clone, Debug, Default, PartialEq, Serialize, DeserializeOption<T> with #[serde(skip_serializing_if = "Option::is_none")]#[serde(tag = "type")], #[serde(untagged)], etc.)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;
This crate requires Rust 1.75 or later.
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
Licensed under either of:
at your option.
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.