litellm/tests/integration/_support/sdk_models.py
yucheng-berri 250e6bd24b
test(integration): build OpenAI and Anthropic SDK models before tests start threads (#46797)
K2 in test_logging_only_scope_chaos.py flaked on CircleCI with
AttributeError: type object 'BaseModel' has no attribute '__pydantic_core_schema__'
raised inside the OpenAI client while its 30-thread control burst parsed responses

The SDKs define their response models with deferred schemas, so the first parse of each
model rebuilds it, and pydantic's model_rebuild is not safe when several threads hit it
at once. Every integration worker now builds those schemas in pytest_configure, before
any test can start a thread pool

Co-authored-by: yucheng-berri <293926326+yucheng-berri@users.noreply.github.com>
2026-10-15 18:28:32 -07:00

23 lines
728 B
Python

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)