mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-08-27 14:17:10 -04:00
## Description
The Vertex `publisher=google` routes forward to a **fixed** upstream
host, ignoring the
request's region. In `headroom/providers/proxy_routes.py`,
`vertex_generate_content`,
`vertex_stream_generate_content`, and `vertex_count_tokens` all do:
```python
del api_version, project, location # <-- location discarded
if publisher == "google":
return await proxy.handle_gemini_generate_content(
request, model,
_api_target(proxy, "vertex"), # <-- single fixed host (default us-central1)
"vertex:google",
)
```
The sibling Anthropic `rawPredict` route already does this correctly —
it keeps `location` and
passes `_vertex_target_for_location(proxy, location)`, which derives the
regional host from the
path.
So a request to
`.../locations/europe-west1/publishers/google/models/gemini-2.0-flash:generateContent`
(with the proxy left at the default Vertex URL) is forwarded to
`https://us-central1-aiplatform.googleapis.com/...europe-west1...` — a
`us-central1` host serving a
`europe-west1` path. Vertex requires the host region to match the path
location, so it rejects the
request. `_vertex_target_for_location` and the region-aware Anthropic
routing landed together in
`0e059150`; the three google routes were the missed spot.
Closes: no issue filed — found while auditing Vertex routing.
## Fix
In all three `publisher == "google"` branches, keep `location` and pass
`_vertex_target_for_location(proxy, location)` instead of
`_api_target(proxy, "vertex")`. That
helper honors an operator-pinned non-default upstream (private gateway)
and otherwise derives the
host from the request's `location` (`global` → the unprefixed host).
## Type of Change
- [x] Bug fix (non-breaking change that fixes an issue)
## Changes Made
- `headroom/providers/proxy_routes.py`: region-aware host for the google
generateContent / streamGenerateContent / countTokens routes.
- `tests/test_vertex_claude_compression.py`: add route-level tests that
the google generateContent and countTokens routes forward a
`europe-west1` request to
`https://europe-west1-aiplatform.googleapis.com` (default config),
mirroring the existing anthropic-route test.
## Testing
- [x] New regression tests added
(`tests/test_vertex_claude_compression.py`)
- [x] Linting/formatting clean — run with the CI-pinned `ruff==0.15.17`
- [ ] Full `pytest` deferred to CI (local-OOM reason below).
```text
$ uvx ruff@0.15.17 check headroom/providers/proxy_routes.py tests/test_vertex_claude_compression.py
All checks passed!
```
## Real Behavior Proof
- Environment: Windows 11, Python 3.10, headroom from this branch.
Importing `headroom` pulls in the torch/transformers stack and a full
`pytest` gets OOM-killed on this box, so I verified the host-derivation
with a dependency-free script and left the full pytest to CI.
- Exact command / steps: ran a `europe-west1` request through the old
fixed `_api_target` host and the new `_vertex_target_for_location`, plus
the `us-central1`/`global`/operator-pinned cases.
- Observed result: the old path sends europe-west1 to the us-central1
host (rejected); the new path derives the correct region and still
honors a pinned upstream:
```text
europe-west1: OLD host=https://us-central1-aiplatform.googleapis.com
europe-west1: NEW host=https://europe-west1-aiplatform.googleapis.com
VERTEX REGION ROUTING FIX VERIFIED (old = fixed us-central1; new = per-request region)
```
- Not tested: a live GCP/Vertex round-trip (handlers stubbed, as the
existing tests do). The existing tests that pin a non-default
`vertex_api_url="https://vertex.test"` still pass, since
`_vertex_target_for_location` honors the pinned upstream. Full local
`pytest` deferred to CI (OOM, per above).
## Review Readiness
- [x] I have performed a self-review
- [x] This PR is ready for human review
## Checklist
- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes — ran
lint + a standalone logic check; full pytest deferred to CI (local OOM,
disclosed above)
- [x] I have updated the CHANGELOG.md if applicable
## Additional Notes
- Reuses the in-file `_vertex_target_for_location` helper the anthropic
route already uses; no new dependencies. (The
non-`google`/non-`anthropic` publisher passthrough is still fixed-host —
a separate, lower-priority follow-up.)
- @JerrettDavis tagging you — non-`us-central1` Vertex Gemini requests
currently fail on a host/region mismatch; this brings the google routes
in line with the anthropic one you reviewed. Thanks!
Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
277 lines
10 KiB
Python
277 lines
10 KiB
Python
"""Turnkey Claude Code + Vertex compression wiring.
|
|
|
|
Covers the fixes that let `headroom wrap claude` + Vertex actually deliver
|
|
compression:
|
|
|
|
- `litellm-vertex` -> `vertex_ai` provider alias (registry),
|
|
- the Vertex upstream host derived per-request from the path's `location`
|
|
(so a europe-west1 request is not sent to a us-central1 host),
|
|
- the native `:rawPredict` route running the compression handler (not the
|
|
verbatim passthrough) for the `anthropic` publisher.
|
|
|
|
No real GCP/Vertex is contacted — handlers and the backend class are stubbed.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import types
|
|
from typing import Any
|
|
|
|
from fastapi.responses import JSONResponse
|
|
from fastapi.testclient import TestClient
|
|
|
|
from headroom.providers import proxy_routes, registry
|
|
from headroom.providers.registry import DEFAULT_VERTEX_API_URL
|
|
from headroom.proxy.server import HeadroomProxy, ProxyConfig, create_app
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
# Region-aware Vertex upstream host
|
|
# --------------------------------------------------------------------------
|
|
def _stub_proxy(vertex_url: str) -> Any:
|
|
# `_api_target` reads `proxy.VERTEX_API_URL`, but its getattr default eagerly
|
|
# evaluates `proxy.provider_runtime.api_target(...)`, so the stub needs both.
|
|
return types.SimpleNamespace(
|
|
VERTEX_API_URL=vertex_url,
|
|
provider_runtime=types.SimpleNamespace(api_target=lambda name: vertex_url),
|
|
)
|
|
|
|
|
|
def test_vertex_target_derives_region_from_location_on_default() -> None:
|
|
proxy = _stub_proxy(DEFAULT_VERTEX_API_URL)
|
|
assert (
|
|
proxy_routes._vertex_target_for_location(proxy, "europe-west1")
|
|
== "https://europe-west1-aiplatform.googleapis.com"
|
|
)
|
|
|
|
|
|
def test_vertex_target_global_uses_unprefixed_host() -> None:
|
|
proxy = _stub_proxy(DEFAULT_VERTEX_API_URL)
|
|
assert (
|
|
proxy_routes._vertex_target_for_location(proxy, "global")
|
|
== "https://aiplatform.googleapis.com"
|
|
)
|
|
|
|
|
|
def test_vertex_target_empty_location_uses_unprefixed_host() -> None:
|
|
proxy = _stub_proxy(DEFAULT_VERTEX_API_URL)
|
|
assert (
|
|
proxy_routes._vertex_target_for_location(proxy, "") == "https://aiplatform.googleapis.com"
|
|
)
|
|
|
|
|
|
def test_vertex_target_honors_explicit_override() -> None:
|
|
# An operator who pinned a non-default upstream (private gateway) wins,
|
|
# regardless of the path's location.
|
|
proxy = _stub_proxy("https://vertex-gateway.internal")
|
|
assert (
|
|
proxy_routes._vertex_target_for_location(proxy, "europe-west1")
|
|
== "https://vertex-gateway.internal"
|
|
)
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
# litellm-vertex -> vertex_ai provider alias
|
|
# --------------------------------------------------------------------------
|
|
def _capture_provider(backend: str) -> dict[str, Any]:
|
|
captured: dict[str, Any] = {}
|
|
|
|
class FakeLiteLLM:
|
|
def __init__(
|
|
self, provider: str, region: str | None = None, profile_name: str | None = None
|
|
) -> None:
|
|
captured["provider"] = provider
|
|
captured["region"] = region
|
|
|
|
registry.create_proxy_backend(
|
|
backend=backend,
|
|
anyllm_provider="openai",
|
|
bedrock_region="us-east5",
|
|
logger=logging.getLogger("test-vertex"),
|
|
litellm_backend_cls=FakeLiteLLM,
|
|
)
|
|
return captured
|
|
|
|
|
|
def test_litellm_vertex_aliases_to_vertex_ai() -> None:
|
|
# The documented `litellm-vertex` must reach the `vertex_ai` provider, not
|
|
# a generic pass-through (which would drop the region and mangle the model).
|
|
assert _capture_provider("litellm-vertex")["provider"] == "vertex_ai"
|
|
|
|
|
|
def test_litellm_vertex_ai_unchanged() -> None:
|
|
assert _capture_provider("litellm-vertex_ai")["provider"] == "vertex_ai"
|
|
|
|
|
|
def test_litellm_bedrock_not_aliased() -> None:
|
|
assert _capture_provider("litellm-bedrock")["provider"] == "bedrock"
|
|
|
|
|
|
# --------------------------------------------------------------------------
|
|
# Native :rawPredict route → compression handler with region-derived host
|
|
# --------------------------------------------------------------------------
|
|
def _default_vertex_app() -> Any:
|
|
# No vertex_api_url override -> default us-central1 host -> route derives
|
|
# the regional host from the request path.
|
|
return create_app(ProxyConfig(optimize=True, cache_enabled=False, rate_limit_enabled=False))
|
|
|
|
|
|
def test_vertex_rawpredict_anthropic_runs_compression_handler(monkeypatch) -> None:
|
|
captured: dict[str, str] = {}
|
|
|
|
# The route calls handle_anthropic_messages(request, base_url, provider, model).
|
|
async def fake(self, request, base_url, provider, model, *rest): # type: ignore[no-untyped-def]
|
|
captured.update(base_url=str(base_url), provider=str(provider), model=str(model))
|
|
return JSONResponse({"ok": True})
|
|
|
|
monkeypatch.setattr(HeadroomProxy, "handle_anthropic_messages", fake)
|
|
|
|
with TestClient(_default_vertex_app()) as client:
|
|
resp = client.post(
|
|
"/v1/projects/p/locations/europe-west1/publishers/anthropic/models/"
|
|
"claude-sonnet-4-6:rawPredict",
|
|
json={"anthropic_version": "vertex-2023-10-16", "messages": []},
|
|
)
|
|
assert resp.status_code == 200
|
|
# The anthropic publisher is routed to the compression handler (not the
|
|
# verbatim passthrough), with the region-derived upstream host (exact match,
|
|
# not a substring check).
|
|
assert captured["provider"] == "vertex:anthropic"
|
|
assert captured["base_url"] == "https://europe-west1-aiplatform.googleapis.com"
|
|
assert captured["model"] == "claude-sonnet-4-6"
|
|
|
|
|
|
def test_vertex_google_generate_content_uses_region_derived_host(monkeypatch) -> None:
|
|
"""The google-publisher generateContent route must derive the upstream host
|
|
from the request's location (like the anthropic route), not send a
|
|
europe-west1 request to the fixed us-central1 host."""
|
|
captured: dict[str, str] = {}
|
|
|
|
# The route calls handle_gemini_generate_content(request, model, base_url, provider).
|
|
async def fake(self, request, model, base_url, provider, *rest): # type: ignore[no-untyped-def]
|
|
captured.update(base_url=str(base_url), provider=str(provider), model=str(model))
|
|
return JSONResponse({"ok": True})
|
|
|
|
monkeypatch.setattr(HeadroomProxy, "handle_gemini_generate_content", fake)
|
|
|
|
with TestClient(_default_vertex_app()) as client:
|
|
resp = client.post(
|
|
"/v1/projects/p/locations/europe-west1/publishers/google/models/"
|
|
"gemini-2.0-flash:generateContent",
|
|
json={"contents": []},
|
|
)
|
|
assert resp.status_code == 200
|
|
assert captured["provider"] == "vertex:google"
|
|
assert captured["base_url"] == "https://europe-west1-aiplatform.googleapis.com"
|
|
assert captured["model"] == "gemini-2.0-flash"
|
|
|
|
|
|
def test_vertex_google_count_tokens_uses_region_derived_host(monkeypatch) -> None:
|
|
"""The google-publisher countTokens route is region-aware too."""
|
|
captured: dict[str, str] = {}
|
|
|
|
async def fake(self, request, model, base_url, provider, *rest): # type: ignore[no-untyped-def]
|
|
captured.update(base_url=str(base_url), provider=str(provider))
|
|
return JSONResponse({"ok": True})
|
|
|
|
monkeypatch.setattr(HeadroomProxy, "handle_gemini_count_tokens", fake)
|
|
|
|
with TestClient(_default_vertex_app()) as client:
|
|
resp = client.post(
|
|
"/v1/projects/p/locations/europe-west1/publishers/google/models/"
|
|
"gemini-2.0-flash:countTokens",
|
|
json={"contents": []},
|
|
)
|
|
assert resp.status_code == 200
|
|
assert captured["provider"] == "vertex:google"
|
|
assert captured["base_url"] == "https://europe-west1-aiplatform.googleapis.com"
|
|
|
|
|
|
def test_vertex_rawpredict_versionless_anthropic_rewrites_to_v1(monkeypatch) -> None:
|
|
captured: dict[str, Any] = {}
|
|
|
|
async def fake(
|
|
self,
|
|
request,
|
|
base_url,
|
|
provider,
|
|
model,
|
|
force_stream=False,
|
|
): # type: ignore[no-untyped-def]
|
|
captured.update(
|
|
path=request.url.path,
|
|
raw_path=request.scope.get("raw_path"),
|
|
base_url=str(base_url),
|
|
provider=str(provider),
|
|
model=str(model),
|
|
force_stream=force_stream,
|
|
)
|
|
return JSONResponse({"ok": True})
|
|
|
|
monkeypatch.setattr(HeadroomProxy, "handle_anthropic_messages", fake)
|
|
|
|
with TestClient(_default_vertex_app()) as client:
|
|
resp = client.post(
|
|
"/projects/p/locations/europe-west1/publishers/anthropic/models/"
|
|
"claude-sonnet-4-6:rawPredict",
|
|
json={"anthropic_version": "vertex-2023-10-16", "messages": []},
|
|
)
|
|
|
|
assert resp.status_code == 200
|
|
assert captured == {
|
|
"path": (
|
|
"/projects/p/locations/europe-west1/publishers/anthropic/models/"
|
|
"claude-sonnet-4-6:rawPredict"
|
|
),
|
|
"raw_path": (
|
|
b"/projects/p/locations/europe-west1/publishers/anthropic/models/"
|
|
b"claude-sonnet-4-6:rawPredict"
|
|
),
|
|
"base_url": "https://europe-west1-aiplatform.googleapis.com/v1",
|
|
"provider": "vertex:anthropic",
|
|
"model": "claude-sonnet-4-6",
|
|
"force_stream": False,
|
|
}
|
|
|
|
|
|
def test_vertex_stream_rawpredict_versionless_anthropic_forces_stream(monkeypatch) -> None:
|
|
captured: dict[str, Any] = {}
|
|
|
|
async def fake(
|
|
self,
|
|
request,
|
|
base_url,
|
|
provider,
|
|
model,
|
|
force_stream=False,
|
|
): # type: ignore[no-untyped-def]
|
|
captured.update(
|
|
path=request.url.path,
|
|
base_url=str(base_url),
|
|
provider=str(provider),
|
|
model=str(model),
|
|
force_stream=force_stream,
|
|
)
|
|
return JSONResponse({"ok": True})
|
|
|
|
monkeypatch.setattr(HeadroomProxy, "handle_anthropic_messages", fake)
|
|
|
|
with TestClient(_default_vertex_app()) as client:
|
|
resp = client.post(
|
|
"/projects/p/locations/europe-west1/publishers/anthropic/models/"
|
|
"claude-sonnet-4-6:streamRawPredict",
|
|
json={"anthropic_version": "vertex-2023-10-16", "messages": []},
|
|
)
|
|
|
|
assert resp.status_code == 200
|
|
assert captured == {
|
|
"path": (
|
|
"/projects/p/locations/europe-west1/publishers/anthropic/models/"
|
|
"claude-sonnet-4-6:streamRawPredict"
|
|
),
|
|
"base_url": "https://europe-west1-aiplatform.googleapis.com/v1",
|
|
"provider": "vertex:anthropic",
|
|
"model": "claude-sonnet-4-6",
|
|
"force_stream": True,
|
|
}
|