llmsdk_provider/language_model/mod.rs
1//! Language model trait and supporting types.
2//!
3//! Maps to `@ai-sdk/provider/src/language-model/v4/*`. The trait describes
4//! a *raw* model interface — user-facing prompt formats (chat, instruction,
5//! ...) are translated into [`Prompt`] before reaching the trait.
6// Rust guideline compliant 2026-02-21
7
8mod call_options;
9mod content;
10mod finish_reason;
11mod prompt;
12mod result;
13mod stream_part;
14mod tool;
15mod usage;
16
17pub use call_options::{CallOptions, ReasoningEffort, ResponseFormat};
18pub use content::{
19 Content, ReasoningPart, Source, ToolApprovalRequest, ToolOutputPart, ToolResult,
20 ToolResultOutput,
21};
22pub use finish_reason::{FinishReason, FinishReasonKind};
23pub use prompt::{
24 AssistantPart, FilePart, Message, Prompt, TextPart, ToolApprovalResponsePart, ToolCallPart,
25 ToolMessagePart, ToolResultPart, UserPart,
26};
27pub use result::{
28 GenerateResponse, GenerateResult, ResponseMetadata, StreamResponse, StreamResult,
29 SupportedUrls, UrlPattern,
30};
31pub use stream_part::StreamPart;
32pub use tool::{FunctionTool, ProviderTool, Tool, ToolChoice, ToolInputExample};
33pub use usage::{InputTokenUsage, OutputTokenUsage, Usage};
34
35use std::pin::Pin;
36
37use async_trait::async_trait;
38use futures::Stream;
39
40use crate::error::Result;
41
42/// Boxed `Send` stream alias used for streaming results.
43pub type BoxStream<T> = Pin<Box<dyn Stream<Item = T> + Send>>;
44
45/// Contract every chat / completion model implements.
46///
47/// Mirrors `LanguageModelV4`. Method names keep the `do_` prefix from ai-sdk
48/// to discourage direct end-user usage; downstream `llmsdk` crates wrap
49/// these into ergonomic helpers.
50#[async_trait]
51pub trait LanguageModel: Send + Sync + std::fmt::Debug {
52 /// Provider id, e.g. `"openai"`.
53 fn provider(&self) -> &str;
54
55 /// Provider-specific model id, e.g. `"gpt-4o-mini"`.
56 fn model_id(&self) -> &str;
57
58 /// Specification version (currently `"v4"`).
59 ///
60 /// Mirrors `LanguageModelV4.specificationVersion` (ai-sdk
61 /// `language-model-v4.ts`). Acts as the trait-version tag for middleware
62 /// composing across versions; provider impls inherit the default.
63 fn specification_version(&self) -> &'static str {
64 "v4"
65 }
66
67 /// URL patterns the model can ingest natively, by media type.
68 ///
69 /// Returning a pattern tells the SDK *not* to download a matching URL
70 /// before calling the model. Defaults to an empty map (no native URL
71 /// support).
72 async fn supported_urls(&self) -> SupportedUrls {
73 SupportedUrls::default()
74 }
75
76 /// Run a non-streaming generation.
77 ///
78 /// # Errors
79 ///
80 /// Returns a [`crate::ProviderError`] when the upstream call fails,
81 /// the response is malformed, or the prompt is rejected.
82 async fn do_generate(&self, options: CallOptions) -> Result<GenerateResult>;
83
84 /// Run a streaming generation.
85 ///
86 /// The returned stream yields [`StreamPart`]s. Errors come in two flavors:
87 ///
88 /// - Outer `Result::Err` — the call itself failed before any data flowed.
89 /// - Inner [`StreamPart::Error`] — the stream is alive but the provider
90 /// reported a recoverable issue (content filter, partial failure).
91 ///
92 /// # Errors
93 ///
94 /// Same conditions as [`Self::do_generate`].
95 async fn do_stream(&self, options: CallOptions) -> Result<StreamResult>;
96}