mirror of
https://github.com/BerriAI/litellm.git
synced 2026-08-28 05:25:59 +00:00
* refactor(e2e/claude_code): align proxy env names with the rest of tests/e2e
Every claude_code compat cell used to read its own `LITELLM_PROXY_BASE_URL` and `LITELLM_PROXY_API_KEY` and duplicate the same 12-line "missing env, hard fail" block. The rest of `tests/e2e/` reads `LITELLM_PROXY_URL` and `LITELLM_MASTER_KEY` from `e2e_config.py`, so anyone standing up a live proxy for one suite had to export a second spelling for claude_code, and every cell repeated the same boilerplate.
Centralize the resolution in `claude_code/_env.py`. `resolve_proxy()` prefers the suite-wide `LITELLM_PROXY_URL` / `LITELLM_MASTER_KEY` names and falls back to the legacy pair so existing CI wiring on stage keeps working during the roll-out. `require_proxy(compat_result)` is the one-liner cells call to bind `(base_url, api_key)` or hard-fail with a message that names both spellings.
55 cell files, `_basic_messaging.py`, and the driver's own unit-test fixture now go through the helper. `run_compat.sh` accepts either spelling and normalizes to the primary names before invoking pytest. `cron_vm/run_daily.sh` exports the primary names when launching pytest.
`_pr_gate_unit_tests/test_env_resolution.py` pins the resolution rules so a future edit cannot silently reintroduce the drift: primary names win on tie, legacy names still resolve when primary is unset, mixed URL-primary key-legacy still resolves, empty-string exports are treated as unset, `require_proxy` names both spellings in its error message.
Net diff: 71 files, +370/-1240.
* fix(e2e): anchor claude_code Bash pin at parents[1] so container run collects
`test_bash_tool_restrictions.py` derived `REPO_ROOT = Path(__file__).resolve().parents[4]` and then joined `tests/e2e/claude_code/<feature>`. That works locally, but the stage container mounts tests/e2e/ at /app/e2e/, so parents[4] resolves to filesystem root and the `_bash_cells()` assertion looks for `/tests/e2e/claude_code/tool_use` — a path that doesn't exist. Collection interrupts before any test runs, so the entire e2e suite appears broken.
Fix: `CLAUDE_CODE_DIR = Path(__file__).resolve().parents[1]` resolves to the sibling `claude_code/` dir in either layout, and the `relative_to(REPO_ROOT)` calls become `relative_to(CLAUDE_CODE_DIR)` so test IDs and error messages read the same.
Adds `test_claude_code_dir_anchor_is_layout_independent` as a regression pin: it checks the anchor lands on a directory named `claude_code` that contains this test file, which would fail under the old parents[4] anchor when run from /app/e2e/.
* feat(e2e/claude_code): register compat deployments via /model/new from a session fixture
Every compat cell hardcodes a virtual model name like `claude-sonnet-4-6` or `claude-sonnet-4-6-bedrock-invoke` and hits the proxy expecting it to be routable. On stage those live in the deployed model_list; locally the `docker-config.yaml` under tests/e2e/ only declares one of them, so anything past haiku 400s with `Invalid model name`.
`claude_code/test_config.yaml` is the ground-truth compat matrix config the deployment already uses. `_compat_models.py` loads it, normalizes the yaml keys pydantic would silently drop (vertex_ai_* → vertex_*), and selects the subset whose provider credentials are present in the environment. An autouse session fixture in `conftest.py` POSTs each selected deployment to `/model/new`, blocks until it is servable on the data plane, and tears them all down on session exit. Skips silently when the proxy env is unset so pure-unit runs stay hermetic.
`test_compat_models.py` pins the invariants that keep this safe. Every cell-referenced name must have a yaml entry (drift check catches a cell probing a name the fixture never registered); the yaml has no unused declarations; the fixture registers exactly 15 deployments (3 tiers × 5 provider surfaces); vertex_ai_* yaml keys populate the pydantic body's vertex_* fields (they got silently dropped historically); Azure needs both AZURE_FOUNDRY_* env vars; Bedrock lifts creds from the ambient AWS chain; Vertex needs both the yaml refs AND ambient GCP credentials.
* refactor(e2e/claude_code): inject env + runner instead of monkeypatching
`require_proxy` and `_basic_messaging.run_basic_messaging_cell` now take the env mapping (and the CLI runner) as constructor-style arguments with `os.environ` and `run_claude_models_parallel` as defaults. Tests exercise the branching by passing dicts and callables directly, so `monkeypatch.setenv` and `monkeypatch.setattr(_basic_messaging, "run_claude_models_parallel", ...)` are gone from every unit test in this refactor's blast radius.
`test_env_resolution.py` drops the `monkeypatch.setenv`/`delenv` fixtures and passes `env={...}` dicts to `require_proxy`. Added a new pinned check that a successful resolution leaves `compat_result` untouched, and split the "unset env" test into three explicit shapes (empty, primary-only, legacy-only) so a regression that swaps the precedence rule can no longer hide behind a single monkeypatched fixture.
`test_basic_messaging.py` (driver) replaces the `_install_fake_runner(monkeypatch, ...)` helper with `_make_fake_runner(...)` that returns a `(callable, captured_dict)` pair the test passes in via the helper's new `runner=` kwarg. Also drops the autouse `_proxy_env` fixture in favor of a module-level `_PROXY_ENV` dict each test wires through the helper's new `env=` kwarg. Added a regression pin that a missing-env call hard-fails without ever invoking the runner (so the guard order stays correct).
`test_run_daily_pytest_scrubs_env.py` updates its pin to assert the new suite-wide env spellings (`LITELLM_PROXY_URL` / `LITELLM_MASTER_KEY`) instead of the legacy `LITELLM_PROXY_BASE_URL` / `LITELLM_PROXY_API_KEY` that `run_daily.sh` used to export.
* handwrote rules
176 lines
7.2 KiB
Python
176 lines
7.2 KiB
Python
"""Shared body for the `passthrough` × <provider> compat cells.
|
||
|
||
Every other matrix row drives the proxy's `/v1/messages` translation
|
||
layer: Claude Code speaks the first-party Anthropic wire and LiteLLM
|
||
transforms the request per provider. This row instead exercises
|
||
LiteLLM's *native passthrough* routes -- the "LLM gateway"
|
||
configuration documented at https://code.claude.com/docs/en/gateway --
|
||
where Claude Code speaks each cloud's own wire format and the proxy
|
||
forwards it, attaching provider credentials on the way out:
|
||
|
||
anthropic ANTHROPIC_BASE_URL={proxy}/anthropic. The CLI's
|
||
first-party wire, forwarded verbatim to
|
||
api.anthropic.com, so the model ids are real
|
||
Anthropic ids rather than proxy aliases.
|
||
bedrock_invoke CLAUDE_CODE_USE_BEDROCK=1 +
|
||
ANTHROPIC_BEDROCK_BASE_URL={proxy}/bedrock. The
|
||
CLI POSTs /model/{model}/invoke-with-response-stream;
|
||
the proxy recognizes a router alias in the model
|
||
segment, rewrites it to the deployment's upstream
|
||
model id, and SigV4-signs with its own AWS creds.
|
||
vertex_ai CLAUDE_CODE_USE_VERTEX=1 +
|
||
ANTHROPIC_VERTEX_BASE_URL={proxy}/vertex_ai/v1.
|
||
The CLI POSTs
|
||
.../models/{model}:streamRawPredict; the proxy
|
||
resolves a router alias in the model segment and
|
||
takes project, location, and credentials from the
|
||
deployment (which is why the deployment must set
|
||
`use_in_pass_through: true` -- see
|
||
test_config.yaml).
|
||
azure CLAUDE_CODE_USE_FOUNDRY=1 +
|
||
ANTHROPIC_FOUNDRY_BASE_URL={proxy}/azure. Foundry
|
||
mode sends the model in the JSON body, not the
|
||
URL, so the proxy's /azure route cannot resolve a
|
||
router alias and falls back to the env-configured
|
||
AZURE_API_BASE / AZURE_API_KEY target.
|
||
bedrock_converse not applicable -- Claude Code's bedrock mode is
|
||
InvokeModel-only; no Converse-wire client exists.
|
||
|
||
Auth is the same in every mode: the CLI's provider-native signing is
|
||
disabled via CLAUDE_CODE_SKIP_<PROVIDER>_AUTH, and the LiteLLM virtual
|
||
key travels as `Authorization: Bearer` (ANTHROPIC_AUTH_TOKEN), exactly
|
||
like the translation rows. The proxy holds the real provider
|
||
credentials.
|
||
|
||
The per-mode env vars and URL shapes above were captured from a real
|
||
`claude` CLI (2.1.210) run against a request-logging sink, not from
|
||
docs; if a CLI release changes them, the cells fail with the CLI's own
|
||
diagnostic rather than silently testing the wrong wire.
|
||
|
||
`run_models` and `env` are injection seams for
|
||
`_driver_unit_tests/test_passthrough.py`; production callers leave
|
||
them unset.
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from typing import Any, Callable, Dict, Mapping, Optional, Sequence
|
||
|
||
import pytest
|
||
|
||
from claude_code._env import require_proxy
|
||
from claude_code.cli_driver import (
|
||
ClaudeCLIError,
|
||
failure_diagnostic,
|
||
run_claude_models_parallel,
|
||
)
|
||
|
||
ANTHROPIC_PASSTHROUGH_BASE_PATH = "/anthropic"
|
||
|
||
CLIENT_SIDE_AWS_REGION = "us-east-1"
|
||
"""Satisfies the CLI's embedded AWS SDK, which refuses to construct a
|
||
client without a region. The value never influences routing: the proxy
|
||
signs the upstream request with its own credentials and region."""
|
||
|
||
VERTEX_PLACEHOLDER_PROJECT = "proxy-resolved-project"
|
||
VERTEX_PLACEHOLDER_REGION = "us-east5"
|
||
"""The CLI refuses to build a Vertex URL without a project id and
|
||
region, but the proxy replaces both path segments with the resolved
|
||
deployment's `vertex_project` / `vertex_location` before forwarding,
|
||
so deliberately-fake values prove the resolution actually happened."""
|
||
|
||
|
||
def bedrock_extra_env(proxy_base_url: str) -> Dict[str, str]:
|
||
return {
|
||
"CLAUDE_CODE_USE_BEDROCK": "1",
|
||
"CLAUDE_CODE_SKIP_BEDROCK_AUTH": "1",
|
||
"ANTHROPIC_BEDROCK_BASE_URL": f"{proxy_base_url}/bedrock",
|
||
"AWS_REGION": CLIENT_SIDE_AWS_REGION,
|
||
}
|
||
|
||
|
||
def vertex_extra_env(proxy_base_url: str) -> Dict[str, str]:
|
||
"""Vertex-mode CLI env pointed at the proxy's /vertex_ai route.
|
||
|
||
The `/v1` suffix on ANTHROPIC_VERTEX_BASE_URL is load-bearing: the
|
||
CLI's Vertex SDK ships its API version inside its *default* base
|
||
URL (`https://{region}-aiplatform.googleapis.com/v1`), so
|
||
overriding the base drops the version from the request path unless
|
||
the override carries it. LiteLLM's /vertex_ai route reuses the
|
||
incoming path verbatim when it contains `/projects/.../locations/...`,
|
||
so a version-less path would reach Google as
|
||
`aiplatform.googleapis.com/projects/...` and 404.
|
||
"""
|
||
return {
|
||
"CLAUDE_CODE_USE_VERTEX": "1",
|
||
"CLAUDE_CODE_SKIP_VERTEX_AUTH": "1",
|
||
"ANTHROPIC_VERTEX_BASE_URL": f"{proxy_base_url}/vertex_ai/v1",
|
||
"ANTHROPIC_VERTEX_PROJECT_ID": VERTEX_PLACEHOLDER_PROJECT,
|
||
"CLOUD_ML_REGION": VERTEX_PLACEHOLDER_REGION,
|
||
}
|
||
|
||
|
||
def foundry_extra_env(proxy_base_url: str) -> Dict[str, str]:
|
||
return {
|
||
"CLAUDE_CODE_USE_FOUNDRY": "1",
|
||
"CLAUDE_CODE_SKIP_FOUNDRY_AUTH": "1",
|
||
"ANTHROPIC_FOUNDRY_BASE_URL": f"{proxy_base_url}/azure",
|
||
}
|
||
|
||
|
||
def run_passthrough_cell(
|
||
*,
|
||
compat_result,
|
||
models: Sequence[str],
|
||
prompt: str,
|
||
passthrough_base_path: str = "",
|
||
build_extra_env: Optional[Callable[[str], Mapping[str, str]]] = None,
|
||
run_models: Callable[..., Mapping[str, Any]] = run_claude_models_parallel,
|
||
env: Optional[Mapping[str, str]] = None,
|
||
) -> None:
|
||
"""Run the shared `passthrough` × <provider> cell body.
|
||
|
||
`passthrough_base_path` is appended to the proxy base URL and
|
||
becomes the CLI's ANTHROPIC_BASE_URL (only the anthropic column
|
||
uses it; the cloud columns ignore ANTHROPIC_BASE_URL entirely once
|
||
their CLAUDE_CODE_USE_* flag is set). `build_extra_env` receives
|
||
the trailing-slash-normalized proxy base URL and returns the
|
||
provider-mode env for the CLI subprocess.
|
||
"""
|
||
proxy = require_proxy(compat_result, env=env)
|
||
proxy_base = proxy.base_url.rstrip("/")
|
||
extra_env = dict(build_extra_env(proxy_base)) if build_extra_env else None
|
||
|
||
outcomes = run_models(
|
||
models=models,
|
||
prompt=prompt,
|
||
base_url=proxy_base + passthrough_base_path,
|
||
api_key=proxy.api_key,
|
||
extra_env=extra_env,
|
||
)
|
||
|
||
failures = []
|
||
for model in models:
|
||
outcome = outcomes[model]
|
||
if isinstance(outcome, ClaudeCLIError):
|
||
error = f"[{model}] {outcome}"
|
||
compat_result.add({"status": "fail", "error": error})
|
||
failures.append(error)
|
||
continue
|
||
|
||
if outcome.exit_code != 0:
|
||
error = f"[{model}] claude CLI failed: {failure_diagnostic(outcome)}"
|
||
compat_result.add({"status": "fail", "error": error})
|
||
failures.append(error)
|
||
continue
|
||
|
||
if not outcome.text.strip():
|
||
error = f"[{model}] claude returned empty assistant text"
|
||
compat_result.add({"status": "fail", "error": error})
|
||
failures.append(error)
|
||
continue
|
||
|
||
compat_result.add({"status": "pass"})
|
||
|
||
if failures:
|
||
pytest.fail("; ".join(failures), pytrace=False)
|