Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

728 B
Raw Blame
from __future__ import annotations

from collections.abc import Iterator
from typing import Final

import anthropic
import openai
from pydantic import BaseModel

_SDK_PACKAGES: Final = frozenset({openai.__name__, anthropic.__name__})


def _subclasses(model: type[BaseModel]) -> Iterator[type[BaseModel]]:
    for subclass in model.__subclasses__():
        yield subclass
        yield from _subclasses(subclass)


def build_sdk_models() -> None:
    """Build every pydantic model the OpenAI and Anthropic SDKs define now rather than on its first use."""
    for model in frozenset(_subclasses(BaseModel)):
        if model.__module__.partition(".")[0] in _SDK_PACKAGES:
            _ = model.model_rebuild(raise_errors=False)