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 pre_create(self, model_config: BaseModelConfig) -> None:
        """
        Create an OpenAI SDK client that uses MADA's shared TLS configuration.

        OpenAI's default client relies on ``certifi``, which can reject
        institution-managed certificate chains on HPC systems. MADA already uses
        the system trust store for MCP HTTP clients, so mirror that behavior for
        OpenAI-compatible providers as well.
        """
        self.validate_model_config(model_config)

        if model_config.extra.get("async_client") is not None:
            return

        client_kwargs = {
            "api_key": model_config.api_key,
            "base_url": model_config.base_url,
            "http_client": DefaultAsyncHttpxClient(
                verify=resolve_httpx_verify_value(verify=model_config.verify),
            ),
        }

        if org_id := model_config.extra.get("org_id"):
            client_kwargs["organization"] = org_id

        if default_headers := model_config.extra.get("default_headers"):
            client_kwargs["default_headers"] = default_headers

        model_config.extra["async_client"] = AsyncOpenAI(**client_kwargs)

    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__}"
            )

pre_create(model_config)

Create an OpenAI SDK client that uses MADA's shared TLS configuration.

OpenAI's default client relies on certifi, which can reject institution-managed certificate chains on HPC systems. MADA already uses the system trust store for MCP HTTP clients, so mirror that behavior for OpenAI-compatible providers as well.

Source code in src/mada/core/chat_clients/openai_adapter.py
def pre_create(self, model_config: BaseModelConfig) -> None:
    """
    Create an OpenAI SDK client that uses MADA's shared TLS configuration.

    OpenAI's default client relies on ``certifi``, which can reject
    institution-managed certificate chains on HPC systems. MADA already uses
    the system trust store for MCP HTTP clients, so mirror that behavior for
    OpenAI-compatible providers as well.
    """
    self.validate_model_config(model_config)

    if model_config.extra.get("async_client") is not None:
        return

    client_kwargs = {
        "api_key": model_config.api_key,
        "base_url": model_config.base_url,
        "http_client": DefaultAsyncHttpxClient(
            verify=resolve_httpx_verify_value(verify=model_config.verify),
        ),
    }

    if org_id := model_config.extra.get("org_id"):
        client_kwargs["organization"] = org_id

    if default_headers := model_config.extra.get("default_headers"):
        client_kwargs["default_headers"] = default_headers

    model_config.extra["async_client"] = AsyncOpenAI(**client_kwargs)

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__}"
        )