From 9c2fe85791bb46a6af67e2ea79a774c1e29852da Mon Sep 17 00:00:00 2001 From: Xsidz Date: Sun, 16 Aug 2026 23:39:19 +0530 Subject: [PATCH] fix(client): sanitize newlines in proxy env vars before httpx sees them Fixes #3303: NO_PROXY (and other proxy env vars) can contain newline characters in Docker/dotenv environments. httpx splits NO_PROXY only by comma, so a newline becomes part of the hostname and triggers InvalidURL. Replace newlines with commas at client construction time. --- src/openai/_client.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/openai/_client.py b/src/openai/_client.py index 5f980c8cb6..f8126f6da9 100644 --- a/src/openai/_client.py +++ b/src/openai/_client.py @@ -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") + + +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()) + def _has_header(headers: Headers, header: str) -> bool: header = header.lower() @@ -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() super().__init__( version=__version__, base_url=base_url, @@ -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,