Skip to main content

Class Definition

@dataclasses.dataclass(slots=True, frozen=True)
class ModelConfig:
    model_id: str | None = None
    provider: str | None = None
    provider_kwargs: dict[str, typing.Any] = dataclasses.field(
        default_factory=dict
    )

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

model_id
str | None
default:"None"
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
Either model_id or provider must be specified.
provider
str | None
default:"None"
Optional explicit provider name or class name. Use this to disambiguate when multiple providers support the same model_id. Examples:
  • "GeminiProvider"
  • "OpenAIProvider"
  • "AnthropicProvider"
  • "OllamaProvider"
If not specified, the provider is automatically resolved from the model_id.
provider_kwargs
dict[str, Any]
default:"{}"
Optional provider-specific keyword arguments that are passed directly to the provider constructor. Common arguments include:Authentication:
  • api_key (str): API key for the provider
  • vertexai (bool): Use Vertex AI authentication for Google models
Generation Parameters:
  • temperature (float): Sampling temperature (0.0 to 1.0)
  • max_tokens (int): Maximum tokens to generate
  • top_p (float): Nucleus sampling parameter
  • top_k (int): Top-k sampling parameter
Provider-Specific:
  • base_url (str): Custom API endpoint (for Ollama, Azure, etc.)
  • organization (str): OpenAI organization ID
  • timeout (int): Request timeout in seconds

Usage Examples

Basic Configuration

from langextract.factory import ModelConfig, create_model

# Minimal configuration with just model_id
config = ModelConfig(model_id="gemini-2.5-flash")
model = create_model(config)

With Provider-Specific Arguments

from langextract.factory import ModelConfig, create_model

# Configuration with custom parameters
config = ModelConfig(
    model_id="gpt-4o",
    provider_kwargs={
        "api_key": "sk-your-api-key",
        "temperature": 0.7,
        "max_tokens": 2000,
        "organization": "org-your-org-id"
    }
)
model = create_model(config)

Explicit Provider Selection

from langextract.factory import ModelConfig, create_model

# Disambiguate provider explicitly
config = ModelConfig(
    model_id="gpt-4o",
    provider="OpenAIProvider",
    provider_kwargs={"temperature": 0.3}
)
model = create_model(config)

Vertex AI Configuration

from langextract.factory import ModelConfig, create_model

# Use Vertex AI instead of API key
config = ModelConfig(
    model_id="gemini-2.5-flash",
    provider_kwargs={
        "vertexai": True,
        "project_id": "your-gcp-project",
        "location": "us-central1"
    }
)
model = create_model(config)

Local Ollama Model

from langextract.factory import ModelConfig, create_model

# Configure local Ollama instance
config = ModelConfig(
    model_id="ollama/llama2",
    provider_kwargs={
        "base_url": "http://localhost:11434"
    }
)
model = create_model(config)

Reusable Configuration

from langextract.factory import ModelConfig, create_model

# Create configuration once, use multiple times
base_config = ModelConfig(
    model_id="gemini-2.5-flash",
    provider_kwargs={
        "temperature": 0.5,
        "max_tokens": 1500
    }
)

# Create multiple model instances
model1 = create_model(base_config)
model2 = create_model(base_config)

# ModelConfig is immutable (frozen)
# base_config.model_id = "other-model"  # This would raise an error

Configuration with Schema Constraints

from langextract.factory import ModelConfig, create_model
from langextract.core.data import ExampleData

examples = [
    ExampleData(input="text", output={"label": "value"})
]

config = ModelConfig(
    model_id="gemini-2.5-flash",
    provider_kwargs={"temperature": 0.0}  # Use deterministic output
)

model = create_model(
    config,
    examples=examples,
    use_schema_constraints=True
)

Notes

  • ModelConfig is frozen (immutable), ensuring configuration cannot be accidentally modified after creation.
  • ModelConfig uses slots for memory efficiency.
  • The provider_kwargs dictionary is not deeply frozen; avoid mutating it after creation.
  • API keys in provider_kwargs take precedence over environment variables.
  • When model_id and provider are both specified, provider takes precedence for provider resolution.
  • Empty provider_kwargs dict is created via default_factory to avoid shared mutable default issues.

See Also

Build docs developers (and LLMs) love