InvalidURL error when NO_PROXY environment variable contains newline characters
Confirm this is an issue with the Python library and not an underlying OpenAI API
- [x] 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
Root cause trace:
# 1. stdlib returns raw value with newline
from urllib.request import getproxies
import os
os.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 hostname
from httpx._utils import get_environment_proxies
print(get_environment_proxies())
# {'all://*localhost\n192.168.1.1': None, ...}
# 3. httpx URL parser rejects non-printable character
import httpx
httpx.Client()
# InvalidURL: Invalid non-printable ASCII character in URL, '\n' at position 16
Workaround:
import os
for key in ("NO_PROXY", "no_proxy"):
val = os.environ.get(key)
if val and "\n" in val:
os.environ[key] = ",".join(
part.strip() for part in val.replace("\n", ",").split(",") if part.strip()
)
from openai import OpenAI # now works
OS
Windows / Linux (any)
Python version
3.11+
Library version
openai: latest
Hi, I'd like to work on this. I'll submit a PR shortly.
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.
Thanks. I will step back from this since there is already an active fix in progress.
Stepping back here since there is already an active fix in progress for this issue. I don't want to duplicate effort.
Hi, I'd like to work on this. I'll submit a PR shortly.
Working on this in PR #3631.
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.