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.
This commit is contained in:
Mason Daugherty
2026-06-10 19:39:50 -04:00
committed by GitHub
parent 2a11a824ef
commit f0a78bf0d7
2 changed files with 21 additions and 15 deletions

View File

@@ -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:

View File

@@ -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"},
)