pub enum AnyClient {
OpenAI(OpenAIClient),
Anthropic(AnthropicClient),
Grok(GrokClient),
Gemini(GeminiClient),
}Expand description
A provider-agnostic client chosen at runtime.
Because LLMClient has a generic materialize method it is not
object-safe, so Box<dyn LLMClient> is impossible. AnyClient is an enum
over the concrete clients that itself implements LLMClient, giving you a
single, Clone, Send + Sync type that can hold whichever provider you
selected at runtime (from a CLI flag, config file, env, etc.).
Construct it with from_env_for, with
LLMClient::from_env (which auto-detects from the environment), or with
From<ConcreteClient> when you need custom configuration:
use rstructor::{AnyClient, Provider, LLMClient, Instructor};
use serde::{Serialize, Deserialize};
#[derive(Instructor, Serialize, Deserialize, Debug)]
struct Movie {
title: String,
}
// Provider decided at runtime.
let provider = Provider::Anthropic;
let client = AnyClient::from_env_for(provider)?;
let movie: Movie = client.materialize("Describe Inception").await?;
println!("{}", movie.title);Wrapping a pre-configured client:
use rstructor::{AnyClient, OpenAIClient, OpenAIModel};
let configured = OpenAIClient::from_env()?.model(OpenAIModel::Gpt55);
let client: AnyClient = configured.into();Variants§
OpenAI(OpenAIClient)
An OpenAI client.
Anthropic(AnthropicClient)
An Anthropic client.
Grok(GrokClient)
A Grok client.
Gemini(GeminiClient)
A Gemini client.
Implementations§
Trait Implementations§
Source§impl From<AnthropicClient> for AnyClient
Available on crate feature anthropic only.
impl From<AnthropicClient> for AnyClient
anthropic only.Source§fn from(client: AnthropicClient) -> Self
fn from(client: AnthropicClient) -> Self
Source§impl From<GeminiClient> for AnyClient
Available on crate feature gemini only.
impl From<GeminiClient> for AnyClient
gemini only.Source§fn from(client: GeminiClient) -> Self
fn from(client: GeminiClient) -> Self
Source§impl From<GrokClient> for AnyClient
Available on crate feature grok only.
impl From<GrokClient> for AnyClient
grok only.Source§fn from(client: GrokClient) -> Self
fn from(client: GrokClient) -> Self
Source§impl From<OpenAIClient> for AnyClient
Available on crate feature openai only.
impl From<OpenAIClient> for AnyClient
openai only.Source§fn from(client: OpenAIClient) -> Self
fn from(client: OpenAIClient) -> Self
Source§impl LLMClient for AnyClient
impl LLMClient for AnyClient
Source§fn from_env() -> Result<Self>
fn from_env() -> Result<Self>
Auto-detect a provider from the environment.
Enabled providers are tried in order (OpenAI, Anthropic, Grok, Gemini)
and the first one whose API-key variable is set is used. For deterministic
selection, prefer AnyClient::from_env_for.
§Errors
Returns an ApiErrorKind::AuthenticationFailed error if none of the
enabled providers’ API-key variables are set.