`BaseClient._prepare_url()` corrupts relative endpoint URLs when `base_url` contains query parameters
I maintainer di solito rispondono entro 1 giorno
@Rewaskc02-lang ci sta già lavorando.
Dal 25/11/2026.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 78/100
Direzione di ricerca
Inizia da src/openai/_base_client.py in BaseClient._prepare_url() (intorno alle righe 564–575); _enforce_trailing_slash() appena sopra mostra già il pattern raw_path.partition(b"?") da seguire. Riproduci con gli snippet del report contro client._build_request(...) per /models e /models?after=m1, poi controlla i test esistenti in tests/test_client.py per la copertura dell'unione degli URL prima di aggiungere casi. È completato quando entrambi gli URL stampati corrispondono ai valori attesi e la codifica percentuale del percorso resta invariata.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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)
- Lingua principale
- Python
- Stelle
- 32.2k
- Fork
- 5.8k
- Merge medio
- 12h 29m
- PR unite (30g)
- 112
Preparare l'ambiente
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di openai/openai-python
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
openai/openai-python#4338 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
openai/openai-python#4337 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
openai/openai-python#4310 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
openai/openai-python#4309 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 82/100
openai/openai-python#4308 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di openai/openai-python
Issue simili
-
ui
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
ARTPARK-SAHAI-ORG/calibrate-frontend#919 ·
I maintainer di solito rispondono entro 1 giorno
-
CI: MSYS2 jobs fail — mingw python3 package renamed upstream to pythonForse già presa @ronaldtse l’ha presa oggi. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 95/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
pydantic/pytest-examples#102 · 1 commento ·