Skip to main content

ProviderFactory

Trait ProviderFactory 

Source
pub trait ProviderFactory: Sized {
    // Required methods
    fn from_env() -> impl Future<Output = LlmResult<Self>> + Send;
    fn with_model(self, model: &str) -> Self;

    // Provided method
    fn from_env_with_connection(
        connection: ProviderConnectionConfig,
    ) -> impl Future<Output = LlmResult<Self>> + Send { ... }
}
Expand description

Factory trait for constructing model providers from environment configuration.

This trait is separate from StreamingModelProvider because construction methods require Sized, which is incompatible with trait objects (Box<dyn StreamingModelProvider>). By splitting the factory methods into their own trait, the provider trait remains object-safe.

§Methods

  • async from_env() -> Result<Self> – Create a provider from environment variables (e.g. ANTHROPIC_API_KEY, OPENAI_API_KEY). Returns LlmError::MissingApiKey if the required variable is not set.

  • with_model(self, model: &str) -> Self – Set or override the model for this provider. Returns self for builder-style chaining.

§Example

use llm::{ProviderFactory, StreamingModelProvider};
use llm::providers::anthropic::AnthropicProvider;

let provider = AnthropicProvider::from_env().await
    .expect("ANTHROPIC_API_KEY must be set")
    .with_model("claude-sonnet-4-5-20250929");

println!("Using: {}", provider.display_name());

Required Methods§

Source

fn from_env() -> impl Future<Output = LlmResult<Self>> + Send

Create provider from environment variables and default configuration

Source

fn with_model(self, model: &str) -> Self

Set or update the model for this provider (builder pattern)

Provided Methods§

Source

fn from_env_with_connection( connection: ProviderConnectionConfig, ) -> impl Future<Output = LlmResult<Self>> + Send

Create provider from environment variables with provider connection overrides.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§