From f0a78bf0d78182dafe834c7afed1b3260598398f Mon Sep 17 00:00:00 2001 From: Mason Daugherty Date: Wed, 10 Jun 2026 19:39:50 -0400 Subject: [PATCH] test(anthropic): make tests robust to gateway base URL (#38043) Anthropic unit tests now pin the expected API base URL where serialization and initialization assertions depend on it. That keeps local gateway settings like `ANTHROPIC_BASE_URL` from changing snapshot output or default URL assertions during development. --- .../tests/unit_tests/test_chat_models.py | 29 ++++++++++--------- .../tests/unit_tests/test_standard.py | 7 +++-- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/libs/partners/anthropic/tests/unit_tests/test_chat_models.py b/libs/partners/anthropic/tests/unit_tests/test_chat_models.py index 0dbdceb483e..9d9518f0ba1 100644 --- a/libs/partners/anthropic/tests/unit_tests/test_chat_models.py +++ b/libs/partners/anthropic/tests/unit_tests/test_chat_models.py @@ -42,19 +42,22 @@ MODEL_NAME = "claude-sonnet-4-5-20250929" def test_initialization() -> None: """Test chat model initialization.""" - for model in [ - ChatAnthropic(model_name=MODEL_NAME, api_key="xyz", timeout=2), # type: ignore[arg-type, call-arg] - ChatAnthropic( # type: ignore[call-arg, call-arg, call-arg] - model=MODEL_NAME, - anthropic_api_key="xyz", - default_request_timeout=2, - base_url="https://api.anthropic.com", - ), - ]: - assert model.model == MODEL_NAME - assert cast("SecretStr", model.anthropic_api_key).get_secret_value() == "xyz" - assert model.default_request_timeout == 2.0 - assert model.anthropic_api_url == "https://api.anthropic.com" + with patch.dict(os.environ, {"ANTHROPIC_API_URL": "https://api.anthropic.com"}): + for model in [ + ChatAnthropic(model_name=MODEL_NAME, api_key="xyz", timeout=2), # type: ignore[arg-type, call-arg] + ChatAnthropic( # type: ignore[call-arg, call-arg, call-arg] + model=MODEL_NAME, + anthropic_api_key="xyz", + default_request_timeout=2, + base_url="https://api.anthropic.com", + ), + ]: + assert model.model == MODEL_NAME + assert ( + cast("SecretStr", model.anthropic_api_key).get_secret_value() == "xyz" + ) + assert model.default_request_timeout == 2.0 + assert model.anthropic_api_url == "https://api.anthropic.com" def test_user_agent_header_in_client_params() -> None: diff --git a/libs/partners/anthropic/tests/unit_tests/test_standard.py b/libs/partners/anthropic/tests/unit_tests/test_standard.py index 75b393dfbdd..77bc50e76c8 100644 --- a/libs/partners/anthropic/tests/unit_tests/test_standard.py +++ b/libs/partners/anthropic/tests/unit_tests/test_standard.py @@ -19,12 +19,15 @@ class TestAnthropicStandard(ChatModelUnitTests): @property def chat_model_params(self) -> dict: - return {"model": _MODEL} + return {"model": _MODEL, "base_url": "https://api.anthropic.com"} @property def init_from_env_params(self) -> tuple[dict, dict, dict]: return ( - {"ANTHROPIC_API_KEY": "test"}, + { + "ANTHROPIC_API_KEY": "test", + "ANTHROPIC_API_URL": "https://api.anthropic.com", + }, {"model": _MODEL}, {"anthropic_api_key": "test"}, )