`BaseClient._prepare_url()` corrupts relative endpoint URLs when `base_url` contains query parameters

Open Beginner friendly
#4,311 4 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@Rewaskc02-lang is already working on this.

Since Nov 25, 2026.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
api

Research direction

Start in src/openai/_base_client.py at BaseClient._prepare_url() (around lines 564–575); _enforce_trailing_slash() just above already shows the raw_path.partition(b"?") pattern to follow. Reproduce with the snippets in the report against client._build_request(...) for /models and /models?after=m1, then check existing tests under tests/test_client.py for URL-merging coverage before adding cases. Done means both printed URLs match the expected values and path percent-encoding is unchanged.

Written by the indexing model from the issue text.

Description

Confirm this is an issue with the Python library and not an underlying OpenAI API
  • This is an issue with the Python library
Describe the bug

In src/openai/_base_client.py, _enforce_trailing_slash() already splits url.raw_path.partition(b"?") so that query parameters on base_url are preserved when normalizing the trailing slash (lines 475–479):

def _enforce_trailing_slash(self, url: URL) -> URL:
    path, separator, query = url.raw_path.partition(b"?")
    if path.endswith(b"/"):
        return url
    return url.copy_with(raw_path=path + b"/" + separator + query)

However, BaseClient._prepare_url() (lines 564–575) concatenates self.base_url.raw_path directly with merge_url.raw_path.lstrip(b"/"):

def _prepare_url(self, url: str) -> URL:
    merge_url = URL(url)
    if merge_url.is_relative_url:
        merge_raw_path = self.base_url.raw_path + merge_url.raw_path.lstrip(b"/")
        return self.base_url.copy_with(raw_path=merge_raw_path)

    return merge_url

Because httpx2.URL.raw_path includes the ?query portion, whenever base_url carries query parameters (common with enterprise proxies, multi-tenant gateways, or query-routed endpoints), every relative HTTP request path is appended onto the end of the query parameter value instead of the URL path:

  • base_url="https://proxy.example.com/v1?tenant=acme" + "/models" becomes https://proxy.example.com/v1/?tenant=acmemodels instead of https://proxy.example.com/v1/models?tenant=acme.
  • If the relative URL also has query parameters (e.g. "/models?after=m1"), the ? is concatenated into the value as ?tenant=acmemodels?after=m1 (or percent-encoded to ?tenant=acmemodels%3Fafter%3Dm1&limit=5 when merged with params).

This affects OpenAI, AsyncOpenAI, and AzureOpenAI(base_url=...) (as well as any callers delegating to BaseClient._prepare_url()).

To Reproduce
  1. Construct OpenAI(api_key="test", base_url="https://proxy.example.com/v1?tenant=acme") (or AsyncOpenAI).
  2. Build a request for a relative endpoint such as "/models" or "/models?after=m1".
  3. Observe that request.url appends "models" to the tenant query value instead of /v1/.
Code snippets
from openai import OpenAI
from openai._models import FinalRequestOptions

client = OpenAI(api_key="test", base_url="https://proxy.example.com/v1?tenant=acme")

req1 = client._build_request(FinalRequestOptions.construct(method="get", url="/models"))
print(req1.url)
# Actual:   https://proxy.example.com/v1/?tenant=acmemodels
# Expected: https://proxy.example.com/v1/models?tenant=acme

req2 = client._build_request(
    FinalRequestOptions.construct(method="get", url="/models?after=m1", params={"limit": "5"})
)
print(req2.url)
# Actual:   https://proxy.example.com/v1/?tenant=acmemodels%3Fafter%3Dm1&limit=5
# Expected: https://proxy.example.com/v1/models?tenant=acme&after=m1&limit=5
Suggested fix

Partition both self.base_url.raw_path and merge_url.raw_path on b"?", join their path components while preserving raw percent-encoding, and re-attach/combine any query parameters:

--- a/src/openai/_base_client.py
+++ b/src/openai/_base_client.py
@@ -569,7 +569,17 @@ class BaseClient(Generic[_HttpxClientT, _DefaultStreamT]):
         # Copied from httpx's `_merge_url` method.
         merge_url = URL(url)
         if merge_url.is_relative_url:
-            merge_raw_path = self.base_url.raw_path + merge_url.raw_path.lstrip(b"/")
+            base_path, _, base_query = self.base_url.raw_path.partition(b"?")
+            rel_path, _, rel_query = merge_url.raw_path.partition(b"?")
+            merge_path = base_path + rel_path.lstrip(b"/")
+            if base_query and rel_query:
+                merge_raw_path = merge_path + b"?" + base_query + b"&" + rel_query
+            elif base_query:
+                merge_raw_path = merge_path + b"?" + base_query
+            elif rel_query:
+                merge_raw_path = merge_path + b"?" + rel_query
+            else:
+                merge_raw_path = merge_path
             return self.base_url.copy_with(raw_path=merge_raw_path)
 
         return merge_url
OS

macOS

Python version

Python 3.14

Library version

openai v3.57.0 (main@f63f14b)

Dominant language
Python
Stars
32.2k
Forks
5.8k
Avg merge
12h 29m
Merged PRs (30d)
112

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from openai/openai-python

All issues in openai/openai-python

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.