Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/openai/_base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,18 @@ def __init__(
)

def _enforce_trailing_slash(self, url: URL) -> URL:
if url.raw_path.endswith(b"/"):
raw_path = url.raw_path
if url.query:
raw_path = raw_path[: -(len(url.query) + 1)]

if raw_path.endswith(b"/"):
return url
return url.copy_with(raw_path=url.raw_path + b"/")

raw_path += b"/"
if url.query:
raw_path += b"?" + url.query

return url.copy_with(raw_path=raw_path)

def _make_status_error_from_response(
self,
Expand Down
8 changes: 4 additions & 4 deletions src/openai/resources/beta/realtime/realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,8 @@ def _prepare_url(self) -> httpx.URL:
else:
base_url = self.__client._base_url.copy_with(scheme="wss")

merge_raw_path = base_url.raw_path.rstrip(b"/") + b"/realtime"
return base_url.copy_with(raw_path=merge_raw_path)
path = base_url.path.rstrip("/") + "/realtime"
return base_url.copy_with(path=path)

async def __aexit__(
self, exc_type: type[BaseException] | None, exc: BaseException | None, exc_tb: TracebackType | None
Expand Down Expand Up @@ -582,8 +582,8 @@ def _prepare_url(self) -> httpx.URL:
else:
base_url = self.__client._base_url.copy_with(scheme="wss")

merge_raw_path = base_url.raw_path.rstrip(b"/") + b"/realtime"
return base_url.copy_with(raw_path=merge_raw_path)
path = base_url.path.rstrip("/") + "/realtime"
return base_url.copy_with(path=path)

def __exit__(
self, exc_type: type[BaseException] | None, exc: BaseException | None, exc_tb: TracebackType | None
Expand Down
8 changes: 4 additions & 4 deletions src/openai/resources/realtime/realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,8 @@ def _prepare_url(self) -> httpx.URL:
else:
base_url = self.__client._base_url.copy_with(scheme="wss")

merge_raw_path = base_url.raw_path.rstrip(b"/") + b"/realtime"
return base_url.copy_with(raw_path=merge_raw_path)
path = base_url.path.rstrip("/") + "/realtime"
return base_url.copy_with(path=path)
Comment on lines +413 to +414
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve raw path bytes when appending /realtime

_prepare_url() now builds the URL from base_url.path, but in httpx URL.path is URL-decoded; this means inputs with encoded delimiters (for example %2F in a proxy route segment) are decoded before copy_with(path=...) and can be reinterpreted as real path separators, changing the target endpoint. The previous raw_path-based construction preserved exact bytes, so this is a behavior regression for users who rely on encoded path segments in base_url/websocket_base_url.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking this up! , Happy to see this getting fixed.


async def __aexit__(
self, exc_type: type[BaseException] | None, exc: BaseException | None, exc_tb: TracebackType | None
Expand Down Expand Up @@ -599,8 +599,8 @@ def _prepare_url(self) -> httpx.URL:
else:
base_url = self.__client._base_url.copy_with(scheme="wss")

merge_raw_path = base_url.raw_path.rstrip(b"/") + b"/realtime"
return base_url.copy_with(raw_path=merge_raw_path)
path = base_url.path.rstrip("/") + "/realtime"
return base_url.copy_with(path=path)

def __exit__(
self, exc_type: type[BaseException] | None, exc: BaseException | None, exc_tb: TracebackType | None
Expand Down
21 changes: 21 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,27 @@ def test_base_url_env(self) -> None:
client = OpenAI(api_key=api_key, _strict_response_validation=True)
assert client.base_url == "http://localhost:5000/from/env/"

def test_realtime_url_preserves_http_query_param(self) -> None:
client = OpenAI(
base_url="https://proxy.com/forward?target=http://backend.internal/v1",
api_key=api_key,
_strict_response_validation=True,
)
assert client.base_url == "https://proxy.com/forward/?target=http://backend.internal/v1"

url = (
client.realtime.connect(model="gpt-realtime")
._prepare_url()
.copy_with(
params={**client.base_url.params, "model": "gpt-realtime"},
)
)

assert str(url) == (
"wss://proxy.com/forward/realtime?target=http%3A%2F%2Fbackend.internal%2Fv1&model=gpt-realtime"
)
client.close()

@pytest.mark.parametrize(
"client",
[
Expand Down