`BaseClient._prepare_url()` corrupts relative endpoint URLs when `base_url` contains query parameters
Maintainer thường phản hồi trong vòng 1 ngày
@Rewaskc02-lang đang làm issue này rồi.
Từ ngày 25/11/2026.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 78/100
Hướng nghiên cứu
Bắt đầu từ src/openai/_base_client.py tại BaseClient._prepare_url() (khoảng dòng 564–575); _enforce_trailing_slash() ngay phía trên đã có mẫu raw_path.partition(b"?") để làm theo. Tái hiện bằng các đoạn mã trong báo cáo với client._build_request(...) cho /models và /models?after=m1, sau đó kiểm tra các test hiện có trong tests/test_client.py xem đã bao phủ việc gộp URL chưa trước khi thêm trường hợp. Hoàn thành khi cả hai URL được in ra khớp với giá trị mong đợi và mã hóa phần trăm của đường dẫn không thay đổi.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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)
- Ngôn ngữ chính
- Python
- Star
- 32.2k
- Fork
- 5.8k
- Merge trung bình
- 12 giờ 29 phút
- Pull request đã merge (30 ngày)
- 112
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của openai/openai-python
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
openai/openai-python#4338 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
openai/openai-python#4337 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
openai/openai-python#4310 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
openai/openai-python#4309 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 82/100
openai/openai-python#4308 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của openai/openai-python
Issue tương tự
-
triage
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 88/100
kornia/kornia-rs#1416 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
ui
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
PolicyEngine/ai-inequality#64 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
LinearGaussianStateSpace.dist splits states and observations wrongly when the transition varies over timeCó thể đã có người làm @PabloRoque đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
pymc-devs/pymc-extras#839 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày