`BaseClient._prepare_url()` corrupts relative endpoint URLs when `base_url` contains query parameters
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
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"becomeshttps://proxy.example.com/v1/?tenant=acmemodelsinstead ofhttps://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=5when merged withparams).
This affects OpenAI, AsyncOpenAI, and AzureOpenAI(base_url=...) (as well as any callers delegating to BaseClient._prepare_url()).
To Reproduce
- Construct
OpenAI(api_key="test", base_url="https://proxy.example.com/v1?tenant=acme")(orAsyncOpenAI). - Build a request for a relative endpoint such as
"/models"or"/models?after=m1". - Observe that
request.urlappends"models"to thetenantquery 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
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from openai/openai-python
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
openai/openai-python#4338 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
openai/openai-python#4337 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
openai/openai-python#4310 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
openai/openai-python#4309 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 82/100
openai/openai-python#4308 · 1 comment ·
Maintainers usually reply within 1 day
All issues in openai/openai-python
Similar issues
-
ui
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
ARTPARK-SAHAI-ORG/calibrate-frontend#919 ·
Maintainers usually reply within 1 day
-
CI: MSYS2 jobs fail — mingw python3 package renamed upstream to pythonPossibly taken @ronaldtse claimed this today. Open
Difficulty 1/5 Under an hour Newbie friendliness 95/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
pydantic/pytest-examples#102 · 1 comment ·