Expand description
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:
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.
Modules§
- prelude
- Prelude module. Import
use anyllm::prelude::*for common application-facing types.
Structs§
- Assistant
Message Ref - Borrowed view returned by
Message::as_assistant. - Chat
Request - A provider-agnostic chat completion request.
- Chat
Request Record - Portable, serde-friendly snapshot of a
ChatRequest. - Chat
Response - A provider-agnostic chat completion response.
- Chat
Response Builder - Lightweight builder for common
ChatResponsetest fixtures. - Chat
Response Record - Portable, serde-friendly snapshot of a
ChatResponse. - Collected
Response - The result of collecting a
ChatStreaminto aChatResponseviaStreamCollector, paired with structural completeness information and any terminal stream error that stopped collection early. - DynChat
Provider - A type-erased chat provider for dynamic dispatch.
- DynEmbedding
Provider - A type-erased embedding provider for dynamic dispatch.
- Embedding
Request - A provider-agnostic embedding request.
- Embedding
Response - A provider-agnostic embedding response.
- Error
Log - Redacted logging/telemetry view of an
Error. - Extracted
- The result of a structured extraction: a typed value, the underlying provider response, and metadata about how the extraction was performed.
- Extracting
Provider - A
ChatProviderwrapper that adds orchestrated structured extraction viaExtractExtusing a bundledExtractor. - Extraction
Metadata - Metadata about how a structured extraction was performed.
- Extraction
Request - Input for a dedicated structured extraction pass.
- Extractor
- Orchestrates structured extraction from LLM providers.
- Fallback
Chat Provider - A
ChatProviderwrapper that delegates to a fallback provider when the primary provider returns an error that qualifies for built-in failover. - Image
Block Ref - Borrowed view returned by
ContentBlock::as_image. - Image
Part Ref - Borrowed view returned by
ContentPart::as_image. - Mock
Embedding Provider - Deterministic embedding provider for tests.
- Mock
Provider - Deterministic chat provider for tests and downstream fixtures.
- Mock
Provider Builder - Builder used by
MockProvider::buildto assemble canned responses and capability metadata. - Mock
Streaming Provider - Deterministic streaming provider for stream reconstruction and error tests.
- Mock
Streaming Provider Builder - Builder used by
MockStreamingProvider::buildto assemble mocked stream transcripts and capability metadata. - Mock
Tool Round Trip - Two canned responses representing a single tool-call round trip.
- Owned
Tool Call - Owned representation of a tool call, independent of any
ContentBlock. - Reasoning
Config - Configuration for model reasoning/thinking behavior.
- Request
Options - Typed, per-request extension bag keyed by Rust type.
- Response
Metadata - Provider-populated metadata attached to a
ChatResponse. - Retry
Policy - Policy controlling retry behavior for transient errors.
- Retrying
Chat Provider - A
ChatProviderwrapper that retries non-streamingchat()calls on retryable errors with exponential backoff. - Serialization
Error - Wrapper for serialization/deserialization failures surfaced through
Error. - Single
Response Stream - A zero-allocation stream that emits the normalized event sequence for a
fully materialized
ChatResponse. - Stream
Collector - Accumulates
StreamEvents into aCollectedResponse. - Stream
Completeness - Tracks whether a stream was fully received without missing structural events.
- System
Options - Type-erased bag of per-prompt options keyed by Rust type.
- System
Prompt - A single request-level system instruction.
- Tool
- A tool (function) definition that the model may call.
- Tool
Call Ref - Borrowed view returned by
ContentBlock::as_tool_call. - Tool
Message Ref - Borrowed view returned by
Message::as_tool. - Tracing
Chat Provider - A wrapper around any
ChatProviderthat emitstracingspans with OTel Gen AI semantic convention field names for eachchat()andchat_stream()call. - Tracing
Content Config - Opt-in capture settings for tracing GenAI message content.
- Usage
- Token usage statistics for a chat completion.
- User
Message Ref - Borrowed view returned by
Message::as_user.
Enums§
- Capability
Support - Support state for a provider/model capability query.
- Chat
Capability - Portable chat features that a provider/model may support.
- Content
Block - A single block of content returned by an LLM provider.
- Content
Part - A single part within multimodal user content.
- Embedding
Capability - Portable embedding features that a provider/model may support.
- Error
- Unified error type for all LLM provider operations.
- Extract
Error - Error produced while extracting structured output
- Extraction
Mode - Controls how structured data is extracted from a provider response.
- Finish
Reason - Why the model stopped generating.
- Image
Source - Source for an image in a content part.
- Message
- A message in a conversation.
- Mock
Response - A canned response returned by
MockProvider. - Mock
Stream Event - One item in a mocked streaming transcript.
- Reasoning
Effort - How much effort the model should spend on reasoning.
- Response
Format - Requested response format from the model.
- Stream
Block Type - The type of content block being streamed.
- Stream
Event - A single event in a streaming chat completion response.
- Tool
Choice - Controls how the model selects tools for a request.
- Tool
Result Content - Content of a tool result message: either plain text or multimodal parts.
- Usage
Metadata Mode - Interpretation mode for streaming usage metadata
- User
Content - Content of a user message: either plain text or multimodal parts.
Traits§
- Chat
Capability Resolver - Optional resolver used to customize a provider’s chat capability answers.
- Chat
Provider - Core trait for LLM chat completion providers.
- Chat
Provider Ext - Convenience extension methods for
ChatProviderimplementors. - Chat
Stream Ext - Extension trait for consuming a
ChatStreaminto aChatResponse. - Embedding
Provider - Core trait for providers that expose a text embedding API.
- Embedding
Provider Ext - Convenience extension methods for
EmbeddingProviderimplementors. - Extract
Ext - Extension trait for providers that support structured data extraction.
- Provider
Identity - Shared provider identity surface for chat and embedding providers.
- Response
Metadata Type - Marker trait for typed entries stored in
ResponseMetadata.
Functions§
- otel_
genai_ provider_ name - Maps internal provider names onto OTEL GenAI provider semantic-convention values where the spec defines a standard name.
Type Aliases§
- Chat
Stream - Boxed stream of normalized chat response events
- Extra
Map - Portable JSON object used wherever anyllm needs an untyped, wire-shaped key/value bag.
- Result
- Convenience alias for
std::result::Result<T, Error>.