Skip to content

models

Model definitions for AI providers and their models.

This module provides a standardized way to represent and work with AI models from different providers. It includes utilities for parsing model names, validating them, and accessing available models.

Classes:

Name Description
InvalidFullModelNameError

Raised when a full model name doesn't follow the 'provider:name' format.

MissingAPIKeyError

Raised when a model has no API key environment variable.

Model

Represents an AI model with its provider and name.

Functions:

Name Description
iter_pydantic_ai_model_full_names

Iterate over all available models from pydantic-ai's known models.

InvalidFullModelNameError πŸ”—

Bases: ValueError

Raised when a full model name doesn't follow the 'provider:name' format.

Source code in src/brag/models.py
class InvalidFullModelNameError(ValueError):
    """Raised when a full model name doesn't follow the 'provider:name' format."""

    def __init__(self, full_name: str) -> None:
        super().__init__(f"Invalid model name: {full_name}")

MissingAPIKeyError πŸ”—

Bases: ValueError

Raised when a model has no API key environment variable.

Source code in src/brag/models.py
class MissingAPIKeyError(ValueError):
    """Raised when a model has no API key environment variable."""

    def __init__(self, provider: ProviderName) -> None:
        super().__init__(f"Model {provider} has no API key environment variable.")

Model πŸ”—

Bases: BaseModel

Represents an AI model with its provider and name.

Models are immutable and identified by a combination of provider and name.

Methods:

Name Description
from_full_name

Parse a full model name into a Model instance.

get_default_context_window_size

Get the default context window size for the model.

Source code in src/brag/models.py
class Model(BaseModel):
    """Represents an AI model with its provider and name.

    Models are immutable and identified by a combination of provider and name.
    """

    model_config = ConfigDict(frozen=True)

    full_name: AvailableModelFullName

    @classmethod
    def from_full_name(cls, full_name: AvailableModelFullName) -> Self:
        """Parse a full model name into a Model instance."""
        return cls(full_name=full_name)

    def get_default_context_window_size(self) -> TokenCount | None:
        """Get the default context window size for the model.

        Returns:
            The default context window size for the model, or None if not found.
        """
        return KNOWN_CONTEXT_WINDOW_SIZES.get(self.full_name)

from_full_name classmethod πŸ”—

from_full_name(full_name: AvailableModelFullName) -> Self

Parse a full model name into a Model instance.

Source code in src/brag/models.py
@classmethod
def from_full_name(cls, full_name: AvailableModelFullName) -> Self:
    """Parse a full model name into a Model instance."""
    return cls(full_name=full_name)

get_default_context_window_size πŸ”—

get_default_context_window_size() -> TokenCount | None

Get the default context window size for the model.

Returns:

Type Description
TokenCount | None

The default context window size for the model, or None if not found.

Source code in src/brag/models.py
def get_default_context_window_size(self) -> TokenCount | None:
    """Get the default context window size for the model.

    Returns:
        The default context window size for the model, or None if not found.
    """
    return KNOWN_CONTEXT_WINDOW_SIZES.get(self.full_name)

iter_pydantic_ai_model_full_names πŸ”—

iter_pydantic_ai_model_full_names() -> Iterator[AvailableModelFullName]

Iterate over all available models from pydantic-ai's known models.

Source code in src/brag/models.py
def iter_pydantic_ai_model_full_names() -> Iterator[AvailableModelFullName]:
    """Iterate over all available models from pydantic-ai's known models."""
    yield from get_literal_type_args(_KnownModelName.__value__)