Skip to content
Open
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
11 changes: 11 additions & 0 deletions src/openai/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@

WORKLOAD_IDENTITY_API_KEY_PLACEHOLDER = "workload-identity-auth"

_PROXY_ENV_VARS = ("HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY", "http_proxy", "https_proxy", "no_proxy")

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 Include ALL_PROXY in the sanitizer

When ALL_PROXY or all_proxy is the configured fallback proxy and contains a trailing or embedded newline, it remains untouched and HTTPX2 still receives the invalid character during default client construction. The existing proxy tests explicitly clear both spellings because HTTPX2 recognizes them, so include them alongside the other supported proxy environment variables.

Useful? React with 👍 / 👎.



def _sanitize_proxy_env_vars() -> None:
for key in _PROXY_ENV_VARS:
val = os.environ.get(key)
if val is not None and "\n" in val:
os.environ[key] = ",".join(p.strip() for p in val.splitlines() if p.strip())
Comment on lines +103 to +104

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 Sanitize carriage-return line endings

When a proxy value contains a lone \r, this guard skips sanitization even though splitlines() would remove it and HTTPX2 rejects it as a non-printable ASCII character. This can occur when shell command substitution reads a CRLF-terminated file and strips the final \n while retaining \r, so the check should recognize carriage returns as well as line feeds.

Useful? React with 👍 / 👎.



def _has_header(headers: Headers, header: str) -> bool:
header = header.lower()
Expand Down Expand Up @@ -262,6 +271,7 @@ def __init__(
parsed[line[:colon].strip()] = line[colon + 1 :].strip()
default_headers = {**parsed, **(default_headers if is_mapping_t(default_headers) else {})}

_sanitize_proxy_env_vars()

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 Sanitize before constructing custom HTTP clients

When callers follow the documented OpenAI(http_client=DefaultHttpx2Client(...)) pattern, Python constructs the HTTPX2 client before entering OpenAI.__init__, so a malformed listed proxy variable raises InvalidURL before this call can sanitize it. The same ordering affects DefaultAsyncHttpx2Client; sanitization therefore also needs to occur in the exported client helpers or otherwise before those clients consume the environment.

Useful? React with 👍 / 👎.

super().__init__(
version=__version__,
base_url=base_url,
Expand Down Expand Up @@ -868,6 +878,7 @@ def __init__(
parsed[line[:colon].strip()] = line[colon + 1 :].strip()
default_headers = {**parsed, **(default_headers if is_mapping_t(default_headers) else {})}

_sanitize_proxy_env_vars()
super().__init__(
version=__version__,
base_url=base_url,
Expand Down