Class Definition
Description
ModelConfig is a frozen dataclass that encapsulates configuration for instantiating a language model provider. It provides a clean interface for specifying which model to use, optionally disambiguating the provider, and passing provider-specific arguments.Attributes
The model identifier used to select a specific language model. Examples:
"gemini-2.5-flash"- Google’s Gemini model"gemini-1.5-pro"- Gemini Pro model"gpt-4o"- OpenAI’s GPT-4 Optimized"gpt-4-turbo"- OpenAI’s GPT-4 Turbo"claude-3-opus"- Anthropic’s Claude model"ollama/llama2"- Local Ollama model
model_id or provider must be specified.Optional explicit provider name or class name. Use this to disambiguate when multiple providers support the same model_id. Examples:
"GeminiProvider""OpenAIProvider""AnthropicProvider""OllamaProvider"
Optional provider-specific keyword arguments that are passed directly to the provider constructor. Common arguments include:Authentication:
api_key(str): API key for the providervertexai(bool): Use Vertex AI authentication for Google models
temperature(float): Sampling temperature (0.0 to 1.0)max_tokens(int): Maximum tokens to generatetop_p(float): Nucleus sampling parametertop_k(int): Top-k sampling parameter
base_url(str): Custom API endpoint (for Ollama, Azure, etc.)organization(str): OpenAI organization IDtimeout(int): Request timeout in seconds
Usage Examples
Basic Configuration
With Provider-Specific Arguments
Explicit Provider Selection
Vertex AI Configuration
Local Ollama Model
Reusable Configuration
Configuration with Schema Constraints
Notes
- ModelConfig is frozen (immutable), ensuring configuration cannot be accidentally modified after creation.
- ModelConfig uses slots for memory efficiency.
- The
provider_kwargsdictionary is not deeply frozen; avoid mutating it after creation. - API keys in
provider_kwargstake precedence over environment variables. - When
model_idandproviderare both specified,providertakes precedence for provider resolution. - Empty
provider_kwargsdict is created viadefault_factoryto avoid shared mutable default issues.
See Also
- create_model() - Use ModelConfig to instantiate a model
- Environment Variables - Automatic API key resolution