Skip to content

fix: url construction for trailing v1 base urls#26037

Open
rohan-shettyy wants to merge 2 commits intoBerriAI:litellm_internal_stagingfrom
rohan-shettyy:rohan/fix_url_construction
Open

fix: url construction for trailing v1 base urls#26037
rohan-shettyy wants to merge 2 commits intoBerriAI:litellm_internal_stagingfrom
rohan-shettyy:rohan/fix_url_construction

Conversation

@rohan-shettyy
Copy link
Copy Markdown

@rohan-shettyy rohan-shettyy commented Apr 18, 2026

Relevant issues

Fixes #26009

Pre-Submission checklist

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

  • I have Added testing in the tests/test_litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem
  • I have requested a Greptile review by commenting @greptileai and received a Confidence Score of at least 4/5 before requesting a maintainer review

Delays in PR merge?

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

CI (LiteLLM team)

CI status guideline:

  • 50-55 passing tests: main is stable with minor issues.
  • 45-49 passing tests: acceptable but needs attention
  • <= 40 passing tests: unstable; be careful with your merges and assess the risk.
  • Branch creation CI run
    Link:

  • CI run for the last commit
    Link:

  • Merge / cherry-pick CI run
    Links:

Screenshots / Proof of Fix

Before fix: test failing

PASS: https://api.openai.com                    -> https://api.openai.com/v1/models
PASS: https://api.openai.com/v1                 -> https://api.openai.com/v1/models
FAIL: https://opencode.ai/zen/v1                -> https://opencode.ai/v1/models
      expected                                   -> https://opencode.ai/zen/v1/models
FAIL: https://opencode.ai/zen                   -> https://opencode.ai/v1/models
      expected                                   -> https://opencode.ai/zen/v1/models
PASS: http://localhost:11434/v1                 -> http://localhost:11434/v1/models

After fix: tests passing

tests/litellm/llms/test_openai_get_models_url.py::TestGetModelsUrl::test_plain_base_url PASSED [ 11%]
tests/litellm/llms/test_openai_get_models_url.py::TestGetModelsUrl::test_trailing_slash PASSED [ 22%]
tests/litellm/llms/test_openai_get_models_url.py::TestGetModelsUrl::test_v1_already_present PASSED [ 33%]
tests/litellm/llms/test_openai_get_models_url.py::TestGetModelsUrl::test_v1_with_trailing_slash PASSED [ 44%]
tests/litellm/llms/test_openai_get_models_url.py::TestGetModelsUrl::test_subpath_with_v1 PASSED [ 55%]
tests/litellm/llms/test_openai_get_models_url.py::TestGetModelsUrl::test_subpath_with_v1_trailing_slash PASSED [ 66%]
tests/litellm/llms/test_openai_get_models_url.py::TestGetModelsUrl::test_subpath_without_v1 PASSED [ 77%]
tests/litellm/llms/test_openai_get_models_url.py::TestGetModelsUrl::test_localhost_with_port_and_v1 PASSED [ 88%]
tests/litellm/llms/test_openai_get_models_url.py::TestGetModelsUrl::test_localhost_without_v1 PASSED [100%]

Type

🐛 Bug Fix

Changes

Fix: Preserves sub-paths in api_base and avoids duplicate /v1 segments when calling get_models() in OpenAIGPTConfig. Fixes #26009.
Tests: Adds unit tests for get_models() URL construction across multiple api_base formats.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 18, 2026

Greptile Summary

This PR fixes a URL construction bug in OpenAIGPTConfig.get_models() where a custom api_base containing a sub-path (e.g. https://opencode.ai/zen/v1) would have /v1/models naively appended, resulting in a duplicate /v1 segment or a missing sub-path. The fix strips trailing slashes then checks whether the base already ends with /v1 before deciding whether to append /models or /v1/models, and a new test file covers nine URL permutations.

Confidence Score: 5/5

Safe to merge — targeted one-method fix with comprehensive mock-based test coverage.

The change is isolated to a single method, the logic is straightforward and correct, all test cases pass and are properly mocked (no live network calls), and no existing tests were modified.

No files require special attention.

Important Files Changed

Filename Overview
litellm/llms/openai/chat/gpt_transformation.py Added trailing-slash strip + /v1-awareness to get_models() URL construction; change is minimal and correct.
tests/litellm/llms/test_openai_get_models_url.py New test file covering nine URL scenarios for get_models(); all calls are properly mocked with no real network requests.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["get_models(api_key, api_base)"] --> B{api_base is None?}
    B -->|Yes| C["api_base = 'https://api.openai.com'"]
    B -->|No| D["use provided api_base"]
    C --> E["strip trailing '/'"]
    D --> E
    E --> F{ends with '/v1'?}
    F -->|Yes| G["url = api_base + '/models'"]
    F -->|No| H["url = api_base + '/v1/models'"]
    G --> I["GET url with Bearer token"]
    H --> I
    I --> J{status 200?}
    J -->|Yes| K["return model IDs"]
    J -->|No| L["raise Exception"]
Loading

Reviews (2): Last reviewed commit: "add tests for edge cases" | Re-trigger Greptile

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 18, 2026

Codecov Report

❌ Patch coverage is 75.00000% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
litellm/llms/openai/chat/gpt_transformation.py 75.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@rohan-shettyy rohan-shettyy changed the title fix url construction for trailing v1 base urls fix: url construction for trailing v1 base urls Apr 19, 2026
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.

Wrong uri construction causes multiply /v1 in the api requesr

1 participant