Skip to content

fix(vertex_ai): build full request path when custom api_base has no path - #32367

Merged
mateo-berri merged 1 commit into
litellm_internal_stagingfrom
litellm_fix_vertex_bare_api_base_graft
Jul 7, 2026
Merged

mateo-berri merged 1 commit into
litellm_internal_stagingfrom
litellm_fix_vertex_bare_api_base_graft

Conversation

@mateo-berri

@mateo-berri mateo-berri commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Relevant issues

Linear ticket

Resolves LIT-3167

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • My PR passes all CI/CD checks (e.g., lint, format, unit tests)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

Reproduced live against the real Vertex API with a proxy config containing one deployment (gemini-embedding-2 -> vertex_ai/gemini-embedding-2, vertex_location: global, bare-host api_base: https://aiplatform.googleapis.com); the identical curl was run first on the base branch, then on this PR's branch

Before (base branch, 8c0e3c0)

curl -sS -X POST 'http://localhost:65196/embeddings' \
  -H "Authorization: Bearer $LITELLM_MASTER_KEY" \
  -H 'Content-Type: application/json' \
  -d '{"model": "gemini-embedding-2", "input": ["Have we discovered everything in our solar system?"]}'
HTTP 500
{
    "error": {
        "message": "litellm.APIConnectionError: Invalid port: 'embedContent'
            Traceback (most recent call last):
              File ".../litellm/llms/vertex_ai/gemini_embeddings/batch_embed_content_handler.py", line 355, in async_batch_embeddings
                response = await async_handler.post(
              File ".../litellm/llms/custom_httpx/http_handler.py", line 622, in post
                req = self.client.build_request(
              File ".../httpx/_urlparse.py", line 411, in normalize_port
                raise InvalidURL(f"Invalid port: {port!r}")
            httpx.InvalidURL: Invalid port: 'embedContent'
            [traceback trimmed]",
        "type": null,
        "param": null,
        "code": "500"
    }
}

After (this PR, bb589ac)

curl -sS -X POST 'http://localhost:65286/embeddings' \
  -H "Authorization: Bearer $LITELLM_MASTER_KEY" \
  -H 'Content-Type: application/json' \
  -d '{"model": "gemini-embedding-2", "input": ["Have we discovered everything in our solar system?"]}'
HTTP 200
{
  "model": "gemini-embedding-2",
  "data": [
    {
      "embedding": [
        0.009381193,
        -0.0035839744,
        0.005121866,
        -0.0014076182,
        0.0033341018,
        ... 3072 floats total ...
      ],
      "index": 0,
      "object": "embedding"
    }
  ],
  "object": "list",
  "usage": {
    "completion_tokens": 0,
    "prompt_tokens": 9,
    "total_tokens": 9,
    "completion_tokens_details": null,
    "prompt_tokens_details": {
      "audio_tokens": 0,
      "cached_tokens": null,
      "text_tokens": 9,
      "image_tokens": null,
      "video_tokens": null,
      "image_count": 0,
      "video_length_seconds": 0.0,
      "audio_length_seconds": 0.0
    }
  }
}

The --detailed_debug log confirms the grafted outbound URL: https://aiplatform.googleapis.com/v1/projects/<project>/locations/global/publishers/google/models/gemini-embedding-2:embedContent

Type

🐛 Bug Fix

Changes

Vertex AI requests with a custom api_base that has no URL path (a bare host such as https://aiplatform.googleapis.com or an internal gateway host) produced invalid URLs. VertexBase._check_custom_proxy appended the endpoint verb with a colon, yielding e.g. https://aiplatform.googleapis.com:embedContent, which httpx rejects with httpx.InvalidURL: Invalid port: 'embedContent' before any network call. A customer hit this as 500s on /embeddings for a vertex_ai Gemini embedding model routed through GoogleBatchEmbeddings -> _get_token_and_url -> _check_custom_proxy, and the same flaw affected every vertex mode routed through that append (generateContent, predict, countTokens)

The colon append exists for gateway api_bases (e.g. the Cloudflare AI Gateway convention) whose api_base already contains the full /v1/projects/.../models/{model} path; for those, appending :{endpoint} is correct and stays unchanged. The fix parses the api_base and, when its path is empty, instead grafts the default-built vertex URL's path onto the bare host, so the request goes to {api_base}/v1/projects/.../models/{model}:{endpoint}. The PSC endpoint format branch and the gemini (Google AI Studio) branch are untouched, and chat streaming on a bare host still ends up with exactly one ?alt=sse. A bare-host api_base could never produce a valid URL before, so no working setup changes behavior

Regression tests cover the bare host graft for embedContent, a bare host with a trailing slash, a path-ful gateway api_base keeping the colon append, chat streaming on a bare host keeping a single ?alt=sse, and the PSC endpoint format staying intact

@greptile-apps

greptile-apps Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Fixes a URL construction bug in VertexBase._check_custom_proxy where a bare-host api_base (e.g. https://aiplatform.googleapis.com with no URL path) caused httpx.InvalidURL because the code was appending the endpoint verb with a colon (host:embedContent). The fix detects bare-host api_base values via urlparse and grafts the path from the default Vertex URL onto the custom host instead.

  • The PSC endpoint branch and Gemini branch are untouched; only the non-PSC Vertex path is affected.
  • An existing parametrized test case (bare host → :{endpoint} assertion) is correctly updated to reflect the new grafted-path expectation; test_standard_proxy_with_googleapis is updated from a bare-host fixture (always broken) to a path-containing fixture that still exercises the old {api_base}:{endpoint} logic.
  • Five new unit tests cover bare host, trailing slash, path-containing gateway, bare-host streaming (?alt=sse appears exactly once), and PSC priority.

Confidence Score: 4/5

Safe to merge; the change only affects the previously-broken bare-host api_base code path and leaves all working configurations untouched.

Narrowly scoped three-line fix with five new tests and correct handling of streaming, PSC, and path-containing gateway cases.

No files require special attention.

Important Files Changed

Filename Overview
litellm/llms/vertex_ai/vertex_llm_base.py Core fix: adds a bare-host detection branch that grafts the default Vertex URL path onto the custom host; PSC and Gemini branches are unaffected and take priority.
tests/test_litellm/llms/vertex_ai/test_vertex_llm_base.py Adds 5 new focused unit tests for the bare-host path; updates one existing parametrized case to match the new (correct) URL shape; all tests use mocked inputs with no real network calls.
tests/test_litellm/llms/vertex_ai/test_vertex_ai_psc_endpoint_support.py Updates test_standard_proxy_with_googleapis to use a path-containing api_base, correctly preserving the api_base:endpoint assertion for path-ful proxies.

Reviews (1): Last reviewed commit: "fix(vertex_ai): build full request path ..." | Re-trigger Greptile

@codecov

codecov Bot commented Jul 7, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by 11.07%

⚡ 2 improved benchmarks
❌ 1 regressed benchmark
✅ 27 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_completion_multi_turn 3.1 ms 4.2 ms -25.26%
⚡ test_completion_simple_message 4.5 ms 3.2 ms +39.45%
⚡ test_completion_with_tools 4.2 ms 3.2 ms +31.48%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing litellm_fix_vertex_bare_api_base_graft (bb589ac) with litellm_internal_staging (a43f128)

Open in CodSpeed

@mateo-berri
mateo-berri merged commit b8248a2 into litellm_internal_staging Jul 7, 2026
77 of 78 checks passed
@mateo-berri
mateo-berri deleted the litellm_fix_vertex_bare_api_base_graft branch July 7, 2026 20:17
@mateo-berri

Copy link
Copy Markdown
Contributor Author

re: codspeed

the "regressed" benchmark is a mocked gpt-4o completion that never touches the vertex-only code changed here, and the symmetric ±30% swings on sibling mock benchmarks are runner noise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants