Expand description
Provider trait abstractions for llmsdk.
This crate is the Rust port of @ai-sdk/provider
at specification version v4. It defines the contract every model provider
(OpenAI, Anthropic, …) implements, plus the shared message / content /
streaming types and the unified error type.
The crate is intentionally minimal: no HTTP, no retry, no middleware.
Those live in llmsdk-provider-utils and downstream provider crates.
§Layout
language_model: chat / completion models with streaming.embedding_model: vector embedding models.image_model: image generation models.video_model: video generation models.reranking_model: document reranking models.files_model: file-upload models (e.g.Anthropic’sPOST /files).skills_model: skill-upload models (Anthropicskills bundles).middleware: decorators for stacking cross-cutting concerns (retry / logging / caching) on top of anyLanguageModel.provider: top-level factory returning model instances by id.error: unifiedProviderError.shared: provider options / metadata / warnings reused across models.
§Example
use llmsdk_provider::SPECIFICATION_VERSION;
assert_eq!(SPECIFICATION_VERSION, "v4");§Stability
Until 1.0, expect breaking changes; the spec version pins compatibility
with the matching @ai-sdk/provider major.
Modules§
- embedding_
model - Embedding model trait and supporting types.
- error
- Unified error type for all provider operations.
- files_
model - File upload model trait and supporting types.
- image_
model - Image generation model trait and supporting types.
- json
- JSON value re-exports used across the provider surface.
- language_
model - Language model trait and supporting types.
- middleware
- Middleware layer for the language model surface.
- provider
- Top-level
Providertrait — the factory for model instances. - reranking_
model - Reranking model trait and supporting types.
- shared
- Shared types reused by language / embedding / image models.
- skills_
model - Skill upload model trait and supporting types.
- speech_
model - Text-to-speech model trait and supporting types.
- transcription_
model - Speech-to-text model trait and supporting types.
- video_
model - Video generation model trait and supporting types.
Structs§
- ApiCall
Error Builder - Builder for API call errors with optional fields.
- Cache
Middleware - Middleware that memoizes generate / stream responses keyed by
CallOptions. - Call
Options - Options for one model invocation.
- DynEmbedding
Model - Type-erased embedding model handle. See
DynLanguageModelfor rationale. - DynImage
Model - Type-erased image model handle. See
DynLanguageModelfor rationale. - DynLanguage
Model - Type-erased language model handle.
- Embed
Options - Options for one
EmbeddingModel::do_embedcall. - Embed
Result - Result of
EmbeddingModel::do_embed. - Embedding
Usage - Token usage for an embedding call.
- File
Part - File attachment part.
- Finish
Reason - Why the model stopped generating.
- Function
Tool - Client-defined function tool with a JSON schema input.
- Generate
Response - Response-side info attached to
GenerateResult. - Generate
Result - Result of
super::LanguageModel::do_generate. - Generated
Image - One image returned by the provider.
- Image
Options - Options for one
ImageModel::do_generatecall. - Image
Result - Result of
ImageModel::do_generate. - Image
Usage - Token usage reported by an image-generation model.
- Image
Usage Input Details - Input-token breakdown by modality.
- Input
Token Usage - Input-side token counters.
- Logging
Middleware - Middleware that emits
Loggerevents around every call. - Memory
Cache Store - In-memory
CacheStorewith optional TTL and LRU eviction. - Memory
Cache Store Builder - Builder for
MemoryCacheStorewith TTL / LRU options. - Middleware
Context - Structured fields carried across the middleware chain.
- Output
Token Usage - Output-side token counters.
- Provider
Error - Unified error returned by every provider operation.
- Provider
Middleware Set - Two middleware chains: one for language models, one for image models.
- Provider
Tool - Provider-defined tool referenced by id.
- Ranking
Entry - One entry in
RerankingResult::ranking. - Reasoning
Part - Reasoning trace produced by the model.
- Request
Info - Request metadata for telemetry / debugging.
- Reranking
Options - Options for one
RerankingModel::do_rerankcall. - Reranking
Result - Result of
RerankingModel::do_rerank. - Response
Info - Response metadata for telemetry / debugging.
- Response
Metadata - Response metadata reported by a provider mid-stream or post-call.
- Retry
Middleware - Middleware that retries failed calls with exponential backoff.
- Retry
Middleware Builder - Builder for
RetryMiddleware; create viaRetryMiddleware::builder. - Skill
File - One file within a skill bundle.
- Speech
Options - Options for one
SpeechModel::do_generatecall. - Speech
Response Info - Response metadata attached to
SpeechResult. - Speech
Result - Result of
SpeechModel::do_generate. - Stderr
Logger - Minimal
Loggerthat writes one line per event to stderr. - Stream
Response - Headers / metadata available when the stream opens.
- Stream
Result - Result of
super::LanguageModel::do_stream. - Text
Part - Plain text part.
- Tool
Approval Request - Approval request for a provider-executed tool call.
- Tool
Approval Response Part - User decision on a provider-executed tool call.
- Tool
Call Part - Tool call requested by the assistant.
- Tool
Input Example - One input example for
FunctionTool::input_examples. - Tool
Result - Tool result emitted by a provider-executed tool.
- Tool
Result Part - Result of a tool invocation.
- Transcription
Options - Options for one
TranscriptionModel::do_generatecall. - Transcription
Response Info - Response metadata for transcription results.
- Transcription
Result - Result of
TranscriptionModel::do_generate. - Transcription
Segment - One transcription segment.
- Upload
File Options - Options for one
FilesModel::upload_filecall. - Upload
File Result - Result of
FilesModel::upload_file. - Upload
Skill Options - Options for one
SkillsModel::upload_skillcall. - Upload
Skill Result - Result of
SkillsModel::upload_skill. - UrlPattern
- One supported-URL regex pattern entry.
- Usage
- Aggregated token usage for a call.
- Video
Options - Options for one
VideoModel::do_generatecall. - Video
Response Info - Response metadata for
VideoModel::do_generate. - Video
Result - Result of
VideoModel::do_generate.
Enums§
- Assistant
Part - Content part allowed in a
Message::Assistant. - Cached
Entry - A cached call result.
- Call
Kind - Discriminates the active call kind passed to
LanguageModelMiddleware::transform_params. - Content
- One generated content unit.
- File
Bytes - Either raw bytes or a base64-encoded string.
- File
Data - File data carried in prompts or tool results.
- Finish
Reason Kind - Normalized reason variants.
- Json
Value - Represents any valid JSON value.
- Message
- One turn in a conversation.
- Reasoning
Effort - Reasoning effort level.
- Reranking
Documents - Documents to rerank. Two-state tagged union over plain text or JSON objects.
- Response
Format - Response format directive.
- Source
- Citation / grounding source returned by the model.
- Stream
Part - One unit emitted on the stream returned by
super::LanguageModel::do_stream. - Tool
- Either a client-defined function tool or a provider-defined tool.
- Tool
Choice - How the model should choose among the configured tools.
- Tool
Message Part - Content part allowed in a
Message::Tool. - Tool
Output Part - One part inside
ToolResultOutput::Content. - Tool
Result Output - Payload variants for a tool result.
- Upload
File Data - Payload variants accepted by
FilesModel::upload_file. - User
Part - Content part allowed in a
Message::User. - Video
Data - One video returned by the provider.
- Video
File - Input image or video for image-to-video / video editing endpoints.
- Warning
- Provider-emitted warning about a model call.
Constants§
- SPECIFICATION_
VERSION - Specification version this crate implements.
Traits§
- Cache
Store - Backing store for
CacheMiddleware. - Embedding
Model - Contract every text-embedding model implements.
- Embedding
Model Middleware - Contract for middleware that decorates an
EmbeddingModel. - Files
Model - Contract every file-upload model implements.
- Image
Model - Contract every image-generation model implements.
- Image
Model Middleware - Contract for middleware that decorates an
ImageModel. - Language
Model - Contract every chat / completion model implements.
- Language
Model Middleware - Contract for middleware that decorates a
LanguageModel. - Logger
- Sink for
LoggingMiddlewareevents. - Provider
- Factory returning model instances by id.
- Reranking
Model - Contract every reranking model implements.
- Reranking
Model Middleware - Contract for middleware that decorates a
RerankingModel. - Skills
Model - Contract every skill-upload model implements.
- Speech
Model - Contract every text-to-speech model implements.
- Transcription
Model - Contract every speech-to-text model implements.
- Video
Model - Contract every video-generation model implements.
- Video
Model Middleware - Contract for middleware that decorates a
VideoModel.
Functions§
- wrap_
embedding_ model - Compose an embedding model with one or more middlewares.
- wrap_
image_ model - Compose an image model with one or more middlewares.
- wrap_
language_ model - Compose a model with one or more middlewares.
- wrap_
provider - Wrap a provider so every returned model is decorated with the matching middleware chain.
- wrap_
reranking_ model - Compose a reranking model with one or more middlewares.
- wrap_
video_ model - Compose a video model with one or more middlewares.
Type Aliases§
- BoxStream
- Boxed
Sendstream alias used for streaming results. - Embedding
- One embedding vector.
- Headers
- HTTP headers attached to a request or response.
- Json
Object - JSON object map (
{ string: JsonValue }), matchingserde_json::Map<String, Value>. - Json
Schema - JSON schema describing a tool’s input or a JSON response format.
- Prompt
- An ordered list of
Messages sent to the model. - Provider
Metadata - Provider-specific metadata returned by a provider, keyed by provider id.
- Provider
Options - Provider-specific options, keyed by provider id.
- Provider
Reference - Mapping of provider names to provider-specific file / skill identifiers.
- Result
- Convenience alias used across the crate.
- Supported
Urls - Native-URL support map returned by
super::LanguageModel::supported_urls.