refactor(openai/evals): warn once via functools.cache instead of a global flag (PLW0603)

This commit is contained in:
Volodymyr 2026-10-18 05:07:43 +03:00
parent 9c5f1536b4
commit 3fdf7d24d6
2 changed files with 13 additions and 11 deletions

View file

@ -3,6 +3,7 @@ OpenAI Evals API configuration and transformations
"""
from collections.abc import Mapping
from functools import cache
from typing import Final
from urllib.parse import urlparse
@ -39,7 +40,12 @@ OPENAI_EVALS_SHUTDOWN_MESSAGE: Final = (
"Requests to api.openai.com/v1/evals will fail after that date, and OpenAI names no API replacement. "
"See https://developers.openai.com/api/docs/deprecations"
)
_openai_evals_shutdown_warned = False
@cache
def _log_openai_evals_shutdown() -> None:
"""Log the shutdown notice; the cache makes it run once per process."""
verbose_logger.warning(OPENAI_EVALS_SHUTDOWN_MESSAGE)
def _warn_openai_evals_shutdown_once(api_base: str | None) -> None:
@ -47,14 +53,9 @@ def _warn_openai_evals_shutdown_once(api_base: str | None) -> None:
Azure and other OpenAI-compatible bases keep their own lifecycles, so they don't get the warning.
"""
global _openai_evals_shutdown_warned
if _openai_evals_shutdown_warned:
return
host: Final = urlparse(api_base).hostname if api_base else "api.openai.com"
if host != "api.openai.com":
return
_openai_evals_shutdown_warned = True
verbose_logger.warning(OPENAI_EVALS_SHUTDOWN_MESSAGE)
if host == "api.openai.com":
_log_openai_evals_shutdown()
def _parsed_response_json(raw_response: httpx.Response) -> Mapping[str, object]:

View file

@ -48,11 +48,12 @@ def test_validate_environment_sets_headers(config: OpenAIEvalsConfig):
@pytest.fixture()
def reset_shutdown_warning(monkeypatch):
def reset_shutdown_warning():
import litellm.llms.openai.evals.transformation as evals_transformation
monkeypatch.setattr(evals_transformation, "_openai_evals_shutdown_warned", False)
return evals_transformation
evals_transformation._log_openai_evals_shutdown.cache_clear()
yield evals_transformation
evals_transformation._log_openai_evals_shutdown.cache_clear()
def test_validate_environment_warns_once_about_openai_evals_shutdown(