serdes-ai 0.2.6

Type-safe, production-ready AI agent framework for Rust - a full port of pydantic-ai
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
//! # SerdesAI - Type-Safe AI Agent Framework for Rust
//!
//! SerdesAI is a comprehensive Rust library for building AI agents that interact with
//! large language models (LLMs). It is a complete port of [pydantic-ai](https://github.com/pydantic/pydantic-ai)
//! to Rust, providing type-safe, ergonomic APIs for creating intelligent agents.
//!
//! ## Quick Start
//!
//! ```ignore
//! use serdes_ai::prelude::*;
//!
//! #[tokio::main]
//! async fn main() -> anyhow::Result<()> {
//!     let agent = Agent::builder()
//!         .model("openai:gpt-4o")
//!         .system_prompt("You are a helpful assistant.")
//!         .build()?;
//!
//!     let result = agent.run("What is the capital of France?", ()).await?;
//!     println!("{}", result.output());
//!     Ok(())
//! }
//! ```
//!
//! ## Key Features
//!
//! - **Type-safe agents** with generic dependencies and output types
//! - **Multiple LLM providers** (OpenAI, Anthropic, Google, Groq, Mistral, Ollama, Bedrock)
//! - **Tool/function calling** with automatic JSON schema generation
//! - **Streaming responses** with real-time text updates
//! - **Structured outputs** with JSON Schema validation
//! - **MCP protocol support** for Model Context Protocol servers
//! - **Embeddings** for semantic search and RAG applications
//! - **Graph-based workflows** for complex multi-step tasks
//! - **Evaluation framework** for testing and benchmarking agents
//! - **Retry strategies** with exponential backoff
//! - **OpenTelemetry integration** for observability
//!
//! ## Feature Flags
//!
//! | Feature | Description | Default |
//! |---------|-------------|--------|
//! | `openai` | OpenAI GPT models | ✅ |
//! | `anthropic` | Anthropic Claude models | ✅ |
//! | `gemini` | Google Gemini models | ❌ |
//! | `groq` | Groq fast inference | ❌ |
//! | `mistral` | Mistral AI models | ❌ |
//! | `ollama` | Local Ollama models | ❌ |
//! | `bedrock` | AWS Bedrock | ❌ |
//! | `mcp` | MCP protocol support | ❌ |
//! | `embeddings` | Embedding models | ❌ |
//! | `graph` | Graph execution engine | ❌ |
//! | `evals` | Evaluation framework | ❌ |
//! | `macros` | Proc macros | ✅ |
//! | `otel` | OpenTelemetry | ❌ |
//! | `full` | All features | ❌ |
//!
//! ## Architecture
//!
//! SerdesAI is organized as a workspace of focused crates:
//!
//! - [`serdes_ai_core`] - Core types, messages, and errors
//! - [`serdes_ai_agent`] - Agent implementation and builder
//! - [`serdes_ai_models`] - Model trait and implementations
//! - [`serdes_ai_tools`] - Tool system and schema generation
//! - [`serdes_ai_toolsets`] - Toolset abstractions
//! - [`serdes_ai_output`] - Output schema validation
//! - [`serdes_ai_streaming`] - Streaming support
//! - [`serdes_ai_retries`] - Retry strategies
//! - [`serdes_ai_mcp`] - MCP protocol (optional)
//! - [`serdes_ai_embeddings`] - Embeddings (optional)
//! - [`serdes_ai_graph`] - Graph execution (optional)
//! - [`serdes_ai_evals`] - Evaluation framework (optional)
//! - [`serdes_ai_macros`] - Procedural macros
//!
//! ## Examples
//!
//! ### Simple Chat
//!
//! ```ignore
//! use serdes_ai::prelude::*;
//!
//! let agent = Agent::builder()
//!     .model("openai:gpt-4o")
//!     .system_prompt("You are helpful.")
//!     .build()?;
//!
//! let result = agent.run("Hello!", ()).await?;
//! ```
//!
//! ### With Tools
//!
//! ```ignore
//! use serdes_ai::prelude::*;
//!
//! #[tool(description = "Get weather for a city")]
//! async fn get_weather(ctx: &RunContext<()>, city: String) -> ToolResult<String> {
//!     Ok(format!("Weather in {}: 22°C, sunny", city))
//! }
//!
//! let agent = Agent::builder()
//!     .model("openai:gpt-4o")
//!     .tool(get_weather)
//!     .build()?;
//! ```
//!
//! ### Structured Output
//!
//! ```ignore
//! use serdes_ai::prelude::*;
//! use serde::Deserialize;
//!
//! #[derive(Deserialize, OutputSchema)]
//! struct Person {
//!     name: String,
//!     age: u32,
//! }
//!
//! let agent = Agent::builder()
//!     .model("openai:gpt-4o")
//!     .output_type::<Person>()
//!     .build()?;
//!
//! let result: Person = agent.run("Extract: John is 30 years old", ()).await?.into_output();
//! ```
//!
//! ### Streaming
//!
//! ```ignore
//! use serdes_ai::prelude::*;
//! use futures::StreamExt;
//!
//! let mut stream = agent.run_stream("Tell me a story", ()).await?;
//!
//! while let Some(event) = stream.next().await {
//!     if let AgentStreamEvent::Text { delta } = event? {
//!         print!("{}", delta);
//!     }
//! }
//! ```

#![warn(missing_docs)]
#![warn(rustdoc::missing_crate_level_docs)]
#![deny(unsafe_code)]
#![cfg_attr(docsrs, feature(doc_cfg))]

// ============================================================================
// Direct Model Access
// ============================================================================

/// Direct model request functions for imperative API access.
///
/// Use this module when you want to make simple, direct requests to models
/// without the full agent infrastructure.
///
/// # Example
///
/// ```rust,ignore
/// use serdes_ai::direct::model_request;
/// use serdes_ai_core::ModelRequest;
///
/// let response = model_request(
///     "openai:gpt-4o",
///     &[ModelRequest::user("Hello!")],
///     None,
///     None,
/// ).await?;
/// ```
pub mod direct;

// ============================================================================
// Core Crate Re-exports
// ============================================================================

/// Core types, messages, and error handling.
pub use serdes_ai_core as core;

/// Agent implementation and builder.
pub use serdes_ai_agent as agent;

/// Model traits and implementations.
pub use serdes_ai_models as models;

/// Provider abstractions.
pub use serdes_ai_providers as providers;

/// Tool system.
pub use serdes_ai_tools as tools;

/// Toolset abstractions.
pub use serdes_ai_toolsets as toolsets;

/// Output schema validation.
pub use serdes_ai_output as output;

/// Streaming support.
pub use serdes_ai_streaming as streaming;

/// Retry strategies.
pub use serdes_ai_retries as retries;

// ============================================================================
// Optional Crate Re-exports
// ============================================================================

/// Model Context Protocol support.
#[cfg(feature = "mcp")]
#[cfg_attr(docsrs, doc(cfg(feature = "mcp")))]
pub use serdes_ai_mcp as mcp;

/// Embedding models.
#[cfg(feature = "embeddings")]
#[cfg_attr(docsrs, doc(cfg(feature = "embeddings")))]
pub use serdes_ai_embeddings as embeddings;

/// Graph-based execution engine.
#[cfg(feature = "graph")]
#[cfg_attr(docsrs, doc(cfg(feature = "graph")))]
pub use serdes_ai_graph as graph;

/// Evaluation framework.
#[cfg(feature = "evals")]
#[cfg_attr(docsrs, doc(cfg(feature = "evals")))]
pub use serdes_ai_evals as evals;

// ============================================================================
// Macro Re-exports
// ============================================================================

/// Derive macro for tools.
#[cfg(feature = "macros")]
#[cfg_attr(docsrs, doc(cfg(feature = "macros")))]
pub use serdes_ai_macros::Tool;

/// Derive macro for output schemas.
#[cfg(feature = "macros")]
#[cfg_attr(docsrs, doc(cfg(feature = "macros")))]
pub use serdes_ai_macros::OutputSchema;

/// Attribute macro for tool functions.
#[cfg(feature = "macros")]
#[cfg_attr(docsrs, doc(cfg(feature = "macros")))]
pub use serdes_ai_macros::tool;

/// Attribute macro for agent definitions.
#[cfg(feature = "macros")]
#[cfg_attr(docsrs, doc(cfg(feature = "macros")))]
pub use serdes_ai_macros::agent as agent_macro;

// ============================================================================
// Core Type Re-exports (Flat)
// ============================================================================

// Errors
pub use serdes_ai_core::SerdesAiError;

// Identifiers
pub use serdes_ai_core::{ConversationId, RunId, ToolCallId};

// Messages
pub use serdes_ai_core::{
    BinaryContent,
    // Builtin tools
    BuiltinToolCallPart,
    BuiltinToolReturnContent,
    BuiltinToolReturnPart,
    CodeExecutionResult,
    // File and binary content
    FilePart,
    FileSearchResult,
    FileSearchResults,
    FinishReason,
    ModelRequest,
    ModelRequestPart,
    ModelResponse,
    ModelResponsePart,
    ModelResponsePartDelta,
    // Streaming events
    ModelResponseStreamEvent,
    PartDeltaEvent,
    PartEndEvent,
    PartStartEvent,
    SystemPromptPart,
    TextPart,
    ThinkingPart,
    ToolCallPart,
    ToolReturnPart,
    UserContent,
    WebSearchResult,
    WebSearchResults,
};

// Settings
pub use serdes_ai_core::ModelSettings;

// Usage
pub use serdes_ai_core::{RequestUsage, RunUsage, UsageLimits};

// Format
pub use serdes_ai_core::{
    format_as_xml, format_as_xml_with_options, XmlFormatError, XmlFormatOptions,
};

// Agent
pub use serdes_ai_agent::{
    Agent, AgentBuilder, AgentRun, AgentRunResult, AgentStream, AgentStreamEvent, EndStrategy,
    ModelConfig, RunContext, RunOptions, StepResult,
};

// Models
pub use serdes_ai_models::Model;
pub use serdes_ai_models::{build_model_extended, build_model_with_config, ExtendedModelConfig};

#[cfg(feature = "openai")]
#[cfg_attr(docsrs, doc(cfg(feature = "openai")))]
pub use serdes_ai_models::openai::OpenAIChatModel;

#[cfg(feature = "anthropic")]
#[cfg_attr(docsrs, doc(cfg(feature = "anthropic")))]
pub use serdes_ai_models::anthropic::AnthropicModel;

#[cfg(feature = "gemini")]
#[cfg_attr(docsrs, doc(cfg(feature = "gemini")))]
pub use serdes_ai_models::GeminiModel;

#[cfg(feature = "groq")]
#[cfg_attr(docsrs, doc(cfg(feature = "groq")))]
pub use serdes_ai_models::groq::GroqModel;

#[cfg(feature = "mistral")]
#[cfg_attr(docsrs, doc(cfg(feature = "mistral")))]
pub use serdes_ai_models::mistral::MistralModel;

#[cfg(feature = "ollama")]
#[cfg_attr(docsrs, doc(cfg(feature = "ollama")))]
pub use serdes_ai_models::ollama::OllamaModel;

#[cfg(feature = "bedrock")]
#[cfg_attr(docsrs, doc(cfg(feature = "bedrock")))]
pub use serdes_ai_models::bedrock::BedrockModel;

// Tools
pub use serdes_ai_tools::{
    ObjectJsonSchema, SchemaBuilder, Tool, ToolDefinition, ToolRegistry, ToolResult,
};

// Toolsets
pub use serdes_ai_toolsets::{
    AbstractToolset, ApprovalRequiredToolset, BoxedToolset, CombinedToolset, DynamicToolset,
    ExternalToolset, FilteredToolset, FunctionToolset, PrefixedToolset, PreparedToolset,
    RenamedToolset, ToolsetInfo, ToolsetTool, WrapperToolset,
};

// Output
pub use serdes_ai_output::{
    OutputSchema, StructuredOutputSchema, TextOutputSchema, ValidationResult,
};

// Streaming
pub use serdes_ai_streaming::{ResponseDelta, ResponseStream};

// Retries
pub use serdes_ai_retries::{
    ExponentialBackoff, FixedDelay, LinearBackoff, RetryConfig, RetryStrategy,
};

// Direct model access
pub use direct::{
    model_request, model_request_stream, model_request_stream_sync, model_request_sync,
    DirectError, ModelSpec, StreamedResponseSync,
};

// ============================================================================
// Optional Type Re-exports
// ============================================================================

// MCP
#[cfg(feature = "mcp")]
#[cfg_attr(docsrs, doc(cfg(feature = "mcp")))]
pub use serdes_ai_mcp::{McpClient, McpToolset};

// Embeddings
#[cfg(feature = "embeddings")]
#[cfg_attr(docsrs, doc(cfg(feature = "embeddings")))]
pub use serdes_ai_embeddings::{EmbeddingModel, EmbeddingResult};

// Graph
#[cfg(feature = "graph")]
#[cfg_attr(docsrs, doc(cfg(feature = "graph")))]
pub use serdes_ai_graph::{
    BaseNode, Edge, End, Graph, GraphError, GraphExecutor, GraphResult, GraphRunContext,
    GraphRunResult, NodeResult,
};

// Evals
#[cfg(feature = "evals")]
#[cfg_attr(docsrs, doc(cfg(feature = "evals")))]
pub use serdes_ai_evals::{
    Case, ContainsScorer, Dataset, EvalCase, EvalRunner, EvalSuite, EvaluationReport,
    EvaluationResult, Evaluator, ExactMatchScorer,
};

// ============================================================================
// Prelude Module
// ============================================================================

/// Convenient prelude for common imports.
///
/// Import everything you need with a single use statement:
///
/// ```ignore
/// use serdes_ai::prelude::*;
/// ```
pub mod prelude {
    // Core types
    pub use crate::core::{ConversationId, Result, RunId, SerdesAiError, ToolCallId};

    // Messages
    pub use crate::core::{
        FinishReason, ModelRequest, ModelResponse, ModelSettings, RequestUsage, RunUsage,
        UsageLimits, UserContent,
    };

    // Agent
    pub use crate::agent::{
        Agent, AgentBuilder, AgentRun, AgentRunResult, AgentStream, AgentStreamEvent, EndStrategy,
        ModelConfig, RunContext, RunOptions,
    };

    // Models
    pub use crate::models::Model;

    #[cfg(feature = "openai")]
    pub use crate::models::openai::OpenAIChatModel;

    #[cfg(feature = "anthropic")]
    pub use crate::models::anthropic::AnthropicModel;

    // Tools
    pub use crate::tools::{Tool, ToolDefinition, ToolRegistry, ToolResult};

    // Toolsets
    pub use crate::toolsets::{
        AbstractToolset, BoxedToolset, CombinedToolset, DynamicToolset, FunctionToolset,
    };

    // Output
    pub use crate::output::{
        OutputSchema, StructuredOutputSchema, TextOutputSchema, ValidationResult,
    };

    // Streaming
    pub use crate::streaming::{ResponseDelta, ResponseStream};

    // Retries
    pub use crate::retries::{ExponentialBackoff, RetryConfig, RetryStrategy};

    // Direct model access
    pub use crate::direct::{model_request, model_request_stream, DirectError, ModelSpec};

    // Format
    pub use crate::core::{format_as_xml, XmlFormatOptions};

    // Macros
    #[cfg(feature = "macros")]
    pub use crate::{tool, OutputSchema as DeriveOutputSchema, Tool as DeriveTool};

    // MCP
    #[cfg(feature = "mcp")]
    pub use crate::mcp::{McpClient, McpToolset};

    // Graph
    #[cfg(feature = "graph")]
    pub use crate::graph::{
        BaseNode, End, Graph, GraphError, GraphExecutor, GraphResult, GraphRunContext,
        GraphRunResult, NodeResult,
    };

    // Evals
    #[cfg(feature = "evals")]
    pub use crate::evals::{EvalCase, EvalRunner, EvalSuite, Evaluator};
}

// ============================================================================
// Version Information
// ============================================================================

/// Returns the current version of serdes-ai.
pub fn version() -> &'static str {
    env!("CARGO_PKG_VERSION")
}

/// Returns version information as a tuple (major, minor, patch).
pub fn version_tuple() -> (u32, u32, u32) {
    let version = version();
    let parts: Vec<&str> = version.split('.').collect();
    (
        parts.first().and_then(|s| s.parse().ok()).unwrap_or(0),
        parts.get(1).and_then(|s| s.parse().ok()).unwrap_or(0),
        parts.get(2).and_then(|s| s.parse().ok()).unwrap_or(0),
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_version() {
        assert_eq!(version(), env!("CARGO_PKG_VERSION"));
    }

    #[test]
    fn test_version_tuple() {
        let (major, minor, patch) = version_tuple();
        let expected: Vec<u32> = env!("CARGO_PKG_VERSION")
            .split('.')
            .map(|s| s.parse::<u32>().unwrap_or(0))
            .collect();

        assert_eq!(major, *expected.first().unwrap_or(&0));
        assert_eq!(minor, *expected.get(1).unwrap_or(&0));
        assert_eq!(patch, *expected.get(2).unwrap_or(&0));
    }

    #[test]
    fn test_prelude_imports() {
        // Just verify these types exist and are accessible
        let _: fn() -> &'static str = crate::version;
    }
}