Skip to main content

provider_from_env

Function provider_from_env 

Source
pub fn provider_from_env() -> Result<Arc<dyn Llm>>
Expand description

Detect LLM provider from environment variables.

Checks environment variables in precedence order and returns the first matching provider that was compiled through Cargo features. The default minimal tier detects Gemini via GOOGLE_API_KEY; add openai or anthropic to widen detection.

  1. GOOGLE_GENAI_USE_ENTERPRISE / GOOGLE_GENAI_USE_VERTEXAI truthy (1 or case-insensitive true) → Gemini on Vertex AI via Application Default Credentials, using GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_LOCATION (requires the gemini-vertex feature; GOOGLE_GENAI_USE_ENTERPRISE takes precedence when both are set)
  2. ANTHROPIC_API_KEY → Anthropic (Claude)
  3. OPENAI_API_KEY → OpenAI
  4. GOOGLE_API_KEY → Gemini

When a Vertex flag is truthy but the gemini-vertex feature is not compiled, a tracing warning is emitted and detection falls through to the API-key steps, which may select the Gemini Studio endpoint (generativelanguage.googleapis.com).

§Errors

Returns AdkError when no supported environment variable is set, or when a Vertex flag is truthy but GOOGLE_CLOUD_PROJECT / GOOGLE_CLOUD_LOCATION is missing.

§Example

use adk_rust::provider_from_env;
use std::sync::Arc;

let model: Arc<dyn adk_rust::Llm> = provider_from_env()?;