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
When no websocket_base_url is provided, the SDK derives one from base_url using Python's str.replace():
base_url.replace("https://", "wss://").replace("http://", "ws://")
str.replace() replaces all occurrences, not just the scheme prefix. If http:// appears anywhere else in the URL (query parameter, path segment, etc.), it gets incorrectly rewritten too.
To Reproduce
from openai import OpenAI
client = OpenAI(
api_key="sk-test",
base_url="https://proxy.com/forward?target=http://backend.internal/v1"
)
print(client.websocket_base_url)
Expected result
wss://proxy.com/forward?target=http://backend.internal/v1
Actual result
wss://proxy.com/forward?target=ws://backend.internal/v1
Suggested fix
Replace only the scheme prefix, not all occurrences:
if base_url.startswith("https://"):
return "wss://" + base_url[len("https://"):]
elif base_url.startswith("http://"):
return "ws://" + base_url[len("http://"):]
Environment
- Library version: latest (main)
- File:
src/openai/_client.py lines ~117–119
- Python version: 3.x
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
When no
websocket_base_urlis provided, the SDK derives one frombase_urlusing Python'sstr.replace():str.replace()replaces all occurrences, not just the scheme prefix. Ifhttp://appears anywhere else in the URL (query parameter, path segment, etc.), it gets incorrectly rewritten too.To Reproduce
Expected result
Actual result
Suggested fix
Replace only the scheme prefix, not all occurrences:
Environment
src/openai/_client.pylines ~117–119