Skip to content

openai_adapter

OpenAI provider adapter implementation.

This module defines OpenAIAdapter, a ProviderAdapter implementation for OpenAI chat models. It validates that incoming model configuration objects are instances of OpenAIModelConfig before client creation.

OpenAIAdapter

Bases: ProviderAdapter

Provider adapter for OpenAI chat models.

This adapter ensures that client creation uses OpenAIModelConfig instances.

Attributes:

Name Type Description
provider_name

Name of the provider handled by this adapter.

chat_client

Chat client class used to create OpenAI chat clients.

Methods:

Name Description
validate_model_config

Validate that model_config is an OpenAIModelConfig instance.

Source code in src/mada/core/chat_clients/openai_adapter.py
class OpenAIAdapter(ProviderAdapter):
    """
    Provider adapter for OpenAI chat models.

    This adapter ensures that client creation uses `OpenAIModelConfig`
    instances.

    Attributes:
        provider_name:
            Name of the provider handled by this adapter.
        chat_client:
            Chat client class used to create OpenAI chat clients.

    Methods:
        validate_model_config:
            Validate that `model_config` is an `OpenAIModelConfig` instance.
    """

    provider_name = "openai"
    chat_client = OpenAIChatClient

    def validate_model_config(self, model_config: BaseModelConfig) -> None:
        """
        Validate that `model_config` is compatible with the OpenAI adapter.

        Args:
            model_config:
                Model configuration to validate.

        Raises:
            TypeError:
                Raised if `model_config` is not an `OpenAIModelConfig`
                instance.
        """
        if not isinstance(model_config, OpenAIModelConfig):
            raise TypeError(
                f"{self.provider_name} adapter requires OpenAIModelConfig, "
                f"got {type(model_config).__name__}"
            )

validate_model_config(model_config)

Validate that model_config is compatible with the OpenAI adapter.

Parameters:

Name Type Description Default
model_config BaseModelConfig

Model configuration to validate.

required

Raises:

Type Description
TypeError

Raised if model_config is not an OpenAIModelConfig instance.

Source code in src/mada/core/chat_clients/openai_adapter.py
def validate_model_config(self, model_config: BaseModelConfig) -> None:
    """
    Validate that `model_config` is compatible with the OpenAI adapter.

    Args:
        model_config:
            Model configuration to validate.

    Raises:
        TypeError:
            Raised if `model_config` is not an `OpenAIModelConfig`
            instance.
    """
    if not isinstance(model_config, OpenAIModelConfig):
        raise TypeError(
            f"{self.provider_name} adapter requires OpenAIModelConfig, "
            f"got {type(model_config).__name__}"
        )