You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
InvalidURL error when NO_PROXY environment variable contains newline characters #3303
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 the NO_PROXY environment variable contains newline characters (common in Docker environments, .env files, or shell scripts), creating an OpenAI client raises InvalidURL.
The error occurs because httpx's get_environment_proxies() only splits NO_PROXY by comma ,, not by newline \n. When a newline is present in the value, it becomes part of the hostname and triggers
URL validation failure.
The fix in httpx would be trivial, but the encode/httpx project is currently not actively accepting external PRs or issues.
Suggesting that openai-python sanitize proxy-related environment variables when initializing the internal httpx client.
To Reproduce
import os
os.environ['NO_PROXY'] = 'localhost\n192.168.1.1'
from openai import OpenAI
client = OpenAI(api_key='sk-test')
Error:
httpx.InvalidURL: Invalid non-printable ASCII character in URL, '\n' at position 16
Code snippets
Rootcausetrace:
# 1. stdlib returns raw value with newlinefromurllib.requestimportgetproxiesimportosos.environ['NO_PROXY'] ='localhost\n192.168.1.1'print(getproxies())
# {'no': 'localhost\n192.168.1.1', ...}# 2. httpx splits only by comma, newline stays in hostnamefromhttpx._utilsimportget_environment_proxiesprint(get_environment_proxies())
# {'all://*localhost\n192.168.1.1': None, ...}# 3. httpx URL parser rejects non-printable characterimporthttpxhttpx.Client()
# InvalidURL: Invalid non-printable ASCII character in URL, '\n' at position 16Workaround:
importosforkeyin ("NO_PROXY", "no_proxy"):
val=os.environ.get(key)
ifvaland"\n"inval:
os.environ[key] =",".join(
part.strip() forpartinval.replace("\n", ",").split(",") ifpart.strip()
)
fromopenaiimportOpenAI# now works
If this is still open and there is no active PR in progress, I would like to take a look. The environment sanitization path seems small enough to verify with a focused repro and test.
Implemented in the existing PR for this branch. The client now temporarily normalizes NO_PROXY /
o_proxy values before constructing the default httpx client, so newline-delimited entries no longer crash client initialization on Windows or Unix. I added regression tests for both sync and async constructors.
Thank you for the report. We understand how multiline Docker Compose configuration can lead to this. HTTPX2 expects a comma-separated NO_PROXY list, so please use NO_PROXY=localhost,192.168.1.1 rather than separating the hosts with newlines.
We’re closing this because we’re keeping the SDK consistent with HTTPX2’s proxy parsing. Broader separator support belongs in the transport library; we won’t add SDK-specific normalization.
Activity
nightcityblade commentedon May 24, 2026
Hi, I'd like to work on this. I'll submit a PR shortly.
fix: sanitize NO_PROXY env var newlines before httpx client init
devShaik010 commentedon Jun 19, 2026
If this is still open and there is no active PR in progress, I would like to take a look. The environment sanitization path seems small enough to verify with a focused repro and test.
Muhtasim-Munif-Fahim commentedon Jun 19, 2026
Implemented in the existing PR for this branch. The client now temporarily normalizes NO_PROXY /
o_proxy values before constructing the default httpx client, so newline-delimited entries no longer crash client initialization on Windows or Unix. I added regression tests for both sync and async constructors.
devShaik010 commentedon Jun 25, 2026
Thanks. I will step back from this since there is already an active fix in progress.
Monkiia commentedon Jun 27, 2026
Stepping back here since there is already an active fix in progress for this issue. I don't want to duplicate effort.
nightcityblade commentedon Jul 3, 2026
Hi, I'd like to work on this. I'll submit a PR shortly.
fix(client): sanitize newlines in NO_PROXY env var before httpx clien…
fix: sanitize newlines in NO_PROXY before httpx client init (openai#3303
fix: sanitize newlines in NO_PROXY before httpx client init (openai#3303
fix: sanitize newlines in NO_PROXY before httpx client init (openai#3303
fix(client): sanitize newlines in NO_PROXY env var before httpx clien…
fix: sanitize newlines in NO_PROXY before httpx client init (openai#3303
fix: sanitize newlines in NO_PROXY before httpx client init (openai#3303
fix: sanitize newlines in NO_PROXY before httpx client init (openai#3303
Xsidz commentedon Aug 16, 2026
Working on this in PR #3631.
fix: sanitize newlines in NO_PROXY env var before httpx client init
fix: sanitize newlines in NO_PROXY before httpx client init (openai#3303
marcuswood-oai commentedon Sep 9, 2026
Thank you for the report. We understand how multiline Docker Compose configuration can lead to this. HTTPX2 expects a comma-separated NO_PROXY list, so please use
NO_PROXY=localhost,192.168.1.1rather than separating the hosts with newlines.We’re closing this because we’re keeping the SDK consistent with HTTPX2’s proxy parsing. Broader separator support belongs in the transport library; we won’t add SDK-specific normalization.