adk_model/lib.rs
1//! # adk-model
2//!
3//! LLM provider clients for ADK-Rust. Every client implements [`adk_core::Llm`], so agents and
4//! runners accept any of them.
5//!
6//! ## Providers
7//!
8//! | Client | Provider | Feature |
9//! |--------|----------|---------|
10//! | [`GeminiModel`] | Google Gemini (AI Studio, Vertex AI with `gemini-vertex`) | `gemini` (default) |
11//! | `OpenAIClient` | OpenAI Chat Completions | `openai` |
12//! | `openai::OpenAIResponsesClient` | OpenAI Responses API | `openai` |
13//! | `AzureOpenAIClient` | Azure OpenAI | `openai` |
14//! | `OpenAICompatible` | Any OpenAI-compatible endpoint, with presets for xAI, Fireworks AI, Together AI, Mistral AI, Perplexity, Cerebras, SambaNova, OrcaRouter, Vultr Serverless Inference, API Route, MiniMax, Zhipu AI, Baidu, Cohere, ByteDance, and Gemini | `openai` |
15//! | `AnthropicClient` | Anthropic Claude | `anthropic` |
16//! | `DeepSeekClient` | DeepSeek | `deepseek` |
17//! | `GroqClient` | Groq | `groq` |
18//! | `MuseClient` | Meta Muse Spark | `muse` |
19//! | `OllamaModel` | Ollama (local) | `ollama` |
20//! | `OpenRouterClient` | OpenRouter, with its native chat, responses, routing, discovery, and credits APIs | `openrouter` |
21//! | `OpenCodeClient` | OpenCode Go and Zen | `opencode` |
22//! | `BedrockClient` | Amazon Bedrock | `bedrock` |
23//! | `AzureAIClient` | Azure AI Inference | `azure-ai` |
24//! | [`FallbackLlm`] | An ordered chain of any of the above, each tried when the one before it fails | always |
25//! | [`ScriptedLlm`] | Test double that answers each call with the next scripted reply and records every request | always |
26//! | [`MockLlm`] | Test double that replays the same responses on every call | always |
27//!
28//! Transport and option features add to a provider:
29//!
30//! | Feature | Enables |
31//! |---------|---------|
32//! | `gemini-interactions` | `GeminiModel::use_interactions_api`, the Gemini Interactions API transport (Beta) |
33//! | `openai-ws` | The Responses API WebSocket transport (`WsTransport`) |
34//! | `openai-conversations` | The Conversations API client (`ConversationsClient`) |
35//! | `openai-responses-full` | `openai-ws` and `openai-conversations` |
36//! | `muse-responses` | `MuseApi::Responses` through the Open Responses engine; implies `muse` and `openai` |
37//! | `muse-realtime` | `MuseRealtimeAsrClient`, live transcription over Meta's realtime WebSocket; implies `muse` |
38//! | `all-providers` | `gemini`, `openai`, `anthropic`, `deepseek`, `ollama`, `groq`, `muse`, `openrouter`, `opencode`, `bedrock` and `azure-ai` |
39//!
40//! [`catalog`] holds the recommended model per provider and the lifecycle of known model IDs;
41//! [`pricing::PricingCatalog`] prices usage for every provider.
42//!
43//! ## Quick start
44//!
45//! ```rust,no_run
46//! use adk_core::{Content, Llm, LlmRequest};
47//! use adk_model::GeminiModel;
48//! use futures::StreamExt;
49//!
50//! # #[tokio::main]
51//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
52//! let model = GeminiModel::new(std::env::var("GOOGLE_API_KEY")?, "gemini-3.8-flash")?;
53//! let request = LlmRequest::new(
54//! "gemini-3.8-flash",
55//! vec![Content::new("user").with_text("Name one planet in a short sentence.")],
56//! );
57//!
58//! let mut responses = model.generate_content(request, false).await?;
59//! while let Some(response) = responses.next().await {
60//! if let Some(content) = response?.content {
61//! for part in &content.parts {
62//! if let Some(text) = part.text() {
63//! print!("{text}");
64//! }
65//! }
66//! }
67//! }
68//! # Ok(())
69//! # }
70//! ```
71//!
72//! ## Other providers
73//!
74//! Each client is built from its config and used the same way:
75//!
76//! ```rust,no_run
77//! # #[cfg(all(feature = "openai", feature = "anthropic", feature = "deepseek"))]
78//! # fn main() -> Result<(), adk_core::AdkError> {
79//! use adk_model::anthropic::{AnthropicClient, AnthropicConfig};
80//! use adk_model::deepseek::DeepSeekClient;
81//! use adk_model::openai::{OpenAIClient, OpenAIConfig, OpenAIResponsesClient, OpenAIResponsesConfig};
82//! use adk_model::{OpenAICompatible, OpenAICompatibleConfig};
83//!
84//! let chat = OpenAIClient::new(OpenAIConfig::new("sk-...", "gpt-6.1-sol"))?;
85//! let responses = OpenAIResponsesClient::new(OpenAIResponsesConfig::new("sk-...", "gpt-6.1-sol"))?;
86//! let claude = AnthropicClient::new(AnthropicConfig::new("sk-ant-...", "claude-sonnet-5-5"))?;
87//! let deepseek = DeepSeekClient::v4_flash("sk-...")?;
88//! let grok = OpenAICompatible::new(OpenAICompatibleConfig::xai("xai-...", "grok-4.7"))?;
89//! # let _ = (chat, responses, claude, deepseek, grok);
90//! # Ok(())
91//! # }
92//! # #[cfg(not(all(feature = "openai", feature = "anthropic", feature = "deepseek")))]
93//! # fn main() {}
94//! ```
95//!
96//! ## Test doubles
97//!
98//! ```rust
99//! use adk_core::{Content, Llm, LlmRequest};
100//! use adk_model::{ScriptedLlm, ScriptedResponse};
101//! use futures::StreamExt;
102//!
103//! # #[tokio::main]
104//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
105//! let model = ScriptedLlm::new("scripted", vec![ScriptedResponse::text("Mars.")]);
106//! let request = LlmRequest::new("scripted", vec![Content::new("user").with_text("Name a planet.")]);
107//!
108//! let response = model.generate_content(request, false).await?.next().await.ok_or("no response")??;
109//! assert_eq!(response.content.unwrap().parts[0].text(), Some("Mars."));
110//! assert_eq!(model.requests().len(), 1);
111//! # Ok(())
112//! # }
113//! ```
114//!
115//! ## OpenRouter scope boundary
116//!
117//! `OpenRouterClient`'s native methods cover model discovery and credits, exact chat and
118//! responses payloads, routing and fallback, plugins, and built-in tools. Its [`adk_core::Llm`]
119//! adapter covers streaming, reasoning parts, tool calling, and multimodal requests, takes
120//! OpenRouter tuning through `openrouter::OpenRouterRequestOptions`, and maps responses into
121//! the generic [`adk_core::LlmResponse`] shape.
122
123#![allow(clippy::result_large_err)]
124#![deny(missing_docs)]
125
126#[cfg(feature = "anthropic")]
127pub mod anthropic;
128pub(crate) mod attachment;
129#[cfg(feature = "azure-ai")]
130pub mod azure_ai;
131#[cfg(feature = "bedrock")]
132pub mod bedrock;
133/// Curated provider model catalog, lifecycle metadata, and recommended defaults.
134pub mod catalog;
135#[cfg(feature = "deepseek")]
136pub mod deepseek;
137#[cfg(any(
138 feature = "anthropic",
139 feature = "azure-ai",
140 feature = "deepseek",
141 feature = "gemini",
142 feature = "groq",
143 feature = "muse",
144 feature = "ollama",
145 feature = "openai",
146 feature = "openrouter"
147))]
148mod error_metadata;
149pub mod fallback;
150/// Gemini model provider (Google AI Studio and Vertex AI).
151#[cfg(feature = "gemini")]
152pub mod gemini;
153#[cfg(any(
154 feature = "openai",
155 feature = "anthropic",
156 feature = "bedrock",
157 feature = "groq",
158 feature = "muse",
159 feature = "deepseek",
160 feature = "azure-ai",
161 feature = "gemini-interactions",
162 feature = "openrouter",
163 feature = "ollama"
164))]
165mod generation_config;
166#[cfg(feature = "groq")]
167pub mod groq;
168/// Connect and idle timeouts for the HTTP transport of model clients.
169pub mod http_timeouts;
170/// Mock LLM for testing without real API calls.
171pub mod mock;
172#[cfg(feature = "muse")]
173pub mod muse;
174#[cfg(feature = "ollama")]
175pub mod ollama;
176#[cfg(feature = "openai")]
177pub mod openai;
178#[cfg(feature = "openai")]
179pub mod openai_compatible;
180#[cfg(feature = "opencode")]
181pub mod opencode;
182#[cfg(feature = "openrouter")]
183pub mod openrouter;
184/// Conversion outcomes for content parts sent to a provider.
185pub mod part_conversion;
186pub mod pricing;
187
188/// Canonical provider identifiers and metadata.
189pub mod provider;
190/// Retry logic with exponential backoff for transient provider errors.
191pub mod retry;
192/// Scripted LLM test double with per-call replies and request capture.
193pub mod scripted;
194#[cfg(any(
195 feature = "openai",
196 feature = "deepseek",
197 feature = "azure-ai",
198 feature = "groq",
199 feature = "muse",
200 feature = "ollama"
201))]
202mod sse;
203#[cfg(any(
204 feature = "openai",
205 feature = "deepseek",
206 feature = "azure-ai",
207 feature = "groq",
208 feature = "muse",
209 feature = "ollama",
210 feature = "openrouter"
211))]
212mod stream_error;
213pub mod telemetry;
214#[cfg(any(
215 feature = "openai",
216 feature = "anthropic",
217 feature = "deepseek",
218 feature = "groq",
219 feature = "muse",
220 feature = "bedrock",
221 feature = "azure-ai",
222 feature = "openrouter"
223))]
224pub(crate) mod tool_args;
225pub mod tool_call_parser;
226#[cfg(any(
227 feature = "openai",
228 feature = "ollama",
229 feature = "deepseek",
230 feature = "groq",
231 feature = "muse",
232 feature = "bedrock",
233 feature = "azure-ai",
234 feature = "openrouter"
235))]
236pub(crate) mod tool_result;
237#[cfg(any(
238 feature = "openai",
239 feature = "ollama",
240 feature = "deepseek",
241 feature = "groq",
242 feature = "muse"
243))]
244pub(crate) mod usage_json;
245pub mod usage_tracking;
246
247#[cfg(feature = "anthropic")]
248pub use anthropic::AnthropicClient;
249#[cfg(feature = "azure-ai")]
250pub use azure_ai::{AzureAIClient, AzureAIConfig};
251#[cfg(feature = "bedrock")]
252pub use bedrock::{BedrockClient, BedrockConfig};
253#[cfg(feature = "deepseek")]
254pub use deepseek::{DeepSeekClient, DeepSeekConfig};
255pub use fallback::{FallbackLlm, FallbackLlmBuilder, FallbackTrigger};
256#[cfg(feature = "gemini")]
257pub use gemini::GeminiModel;
258#[cfg(feature = "groq")]
259pub use groq::{GroqClient, GroqConfig};
260pub use http_timeouts::HttpTimeouts;
261pub use mock::MockLlm;
262#[cfg(feature = "muse")]
263pub use muse::{
264 FileObject, ImageData, ImageOptions, ImagesResponse, MuseApi, MuseAsrClient, MuseClient,
265 MuseConfig, MuseFilesClient, MuseImagesClient, MuseResponsesClient, ResponseObject,
266 ResponseRequest, TranscribeOptions, Transcript, TranscriptSegment,
267};
268#[cfg(feature = "ollama")]
269pub use ollama::{OllamaConfig, OllamaModel};
270#[cfg(feature = "openai")]
271pub use openai::{
272 AzureConfig, AzureOpenAIClient, OpenAIClient, OpenAIConfig, OpenAIResponsesClient,
273 OpenAIResponsesConfig, ReasoningEffort,
274};
275#[cfg(feature = "openai")]
276pub use openai_compatible::{OpenAICompatible, OpenAICompatibleConfig, ReasoningReplayField};
277#[cfg(feature = "opencode")]
278pub use opencode::{OpenCodeApi, OpenCodeClient, OpenCodeConfig, OpenCodeService};
279#[cfg(feature = "openrouter")]
280pub use openrouter::{OpenRouterApiMode, OpenRouterClient, OpenRouterConfig};
281pub use pricing::PricingCatalog;
282pub use provider::ModelProvider;
283pub use retry::RetryConfig;
284pub use retry::ServerRetryHint;
285pub use scripted::{SCRIPT_EXHAUSTED_CODE, ScriptedLlm, ScriptedResponse};