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
//! Provider-agnostic chat and embedding primitives and adapters.
//!
//! The crate root exposes the common day-to-day API surface directly:
//! [`ChatProvider`], [`ChatRequest`], [`ChatResponse`], [`Tool`],
//! [`EmbeddingProvider`], [`EmbeddingRequest`], [`EmbeddingResponse`], and
//! related streaming and wrapper types.
//!
//! Use [`prelude`] when you want a compact import for typical application code.
//! Reach for explicit root imports when writing libraries or examples that need
//! to make advanced types like [`ChatRequestRecord`], [`ChatResponseRecord`],
//! [`CollectedResponse`], or [`StreamCompleteness`] obvious at the callsite.
//!
//! ## Capabilities
//!
//! `anyllm` models each LLM capability as a sibling trait on top of a shared
//! [`ProviderIdentity`] super-trait:
//!
//! - [`ChatProvider`]: one-shot and streaming chat completion.
//! - [`EmbeddingProvider`]: batch-oriented text embedding.
//!
//! Providers implement whichever capabilities they support. A provider that
//! only exposes chat only implements `ChatProvider`; one that exposes both
//! implements both. Capability support queries share the [`CapabilitySupport`]
//! enum across both traits.
//!
//! ## Portability Model
//!
//! `anyllm` aims to keep the common request/response path provider-agnostic,
//! while still leaving explicit escape hatches for provider-specific features.
//!
//! The portable chat core is centered on [`ChatRequest`], [`ChatResponse`],
//! [`Message`], [`ContentBlock`], [`Tool`], and the streaming event model.
//! The portable embedding core is centered on [`EmbeddingRequest`],
//! [`EmbeddingResponse`], and [`EmbeddingCapability`], intentionally narrower
//! because the provider overlap on embedding features is narrower.
//!
//! These types intentionally expose their portable fields directly and pair
//! them with fluent constructors/helpers so application and test code can build
//! and adjust requests and responses without going through opaque builders.
//!
//! Provider-specific data lives in a few deliberate places:
//! [`RequestOptions`] and [`ResponseMetadata`] for typed extensions,
//! [`ExtraMap`] and `extensions` fields for portable JSON-shaped extras, and
//! `Other` enum variants for provider payloads that do not fit the normalized
//! model yet.
//!
//! This means portability is best-effort rather than absolute. Converting to
//! [`ChatRequestRecord`] or [`ChatResponseRecord`] preserves the portable core,
//! but typed provider-specific data may be dropped or flattened to JSON.
//!
//! ## Wrappers
//!
//! Four wrappers implement [`ChatProvider`] on top of another
//! [`ChatProvider`] and compose arbitrarily.
//!
//! | Want | Reach for |
//! |------|-----------|
//! | Retry the same provider on transient errors | [`RetryingChatProvider`] |
//! | Swap to a different provider on failure | [`FallbackChatProvider`] |
//! | OpenTelemetry GenAI spans around each call | `TracingChatProvider` (`tracing` feature) |
//! | Parse responses into typed Rust structs | `ExtractExt::extract` (`extract` feature) |
//!
//! The recommended stacking order, outermost first:
//!
//! ```rust,ignore
//! TracingChatProvider::new(
//! FallbackChatProvider::new(
//! RetryingChatProvider::new(primary),
//! RetryingChatProvider::new(fallback),
//! ),
//! )
//! ```
//!
//! Rationale:
//!
//! - **Tracing outermost.** Spans cover every downstream decision, so a
//! single trace shows the retry attempts and the fallback event.
//! - **Fallback around retry.** Each backend gets its own retry budget;
//! exhausted retries on the primary trigger the fallback, and the
//! fallback has its own retry window.
//! - **Retry innermost.** Transient errors are handled at the provider
//! that saw them, before bubbling out to the fallback decision.
//!
//! Structured extraction is orthogonal to the stack. `ExtractExt` is
//! implemented for every wrapper and for [`DynChatProvider`], so
//! `stacked.extract(&request)` works on any composition without an
//! explicit `ExtractingProvider`. Reach for `ExtractingProvider` only
//! when you want `chat()` itself to return the extraction-aware response
//! shape rather than calling `extract` at the call site.
pub use CapabilitySupport;
pub use ;
pub use ;
pub use ;
pub use ;
pub use MockEmbeddingProvider;
pub use ;
pub use ;
pub use ProviderIdentity;
pub use ;
pub use Usage;
/// Portable JSON object used wherever anyllm needs an untyped, wire-shaped
/// key/value bag.
///
/// `ExtraMap` is the library's JSON-native escape hatch. It appears as the
/// `data` field of [`ContentBlock::Other`], the `extensions` field on
/// messages and tools, the `metadata` and `data` fields on stream events,
/// and the portable half of [`ResponseMetadata`]. Aliasing to
/// `serde_json::Map<String, serde_json::Value>` (rather than `Value`) makes
/// the "this is a key/value bag" constraint compile-time: a scalar or array
/// will not fit.
///
/// For Rust-native typed configuration, use [`RequestOptions`] on the request
/// side or [`ResponseMetadataType`] entries on the response side. Reach for
/// `ExtraMap` only when the data is inherently JSON-shaped or when preserving
/// a wire-level blob verbatim.
pub type ExtraMap = Map;
/// Prelude module. Import `use anyllm::prelude::*` for common application-facing types.
///
/// The prelude intentionally omits specialized types so they remain explicit at
/// the callsite: record snapshots ([`ChatRequestRecord`], [`ChatResponseRecord`]),
/// stream diagnostics ([`CollectedResponse`], [`StreamCompleteness`]),
/// provider-authoring hooks ([`ProviderIdentity`], [`ChatCapabilityResolver`]),
/// the redacted logging view ([`ErrorLog`]), the JSON escape hatch ([`ExtraMap`]),
/// and variant reference types (e.g. [`ToolCallRef`], [`AssistantMessageRef`]).