openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

InvalidURL error when NO_PROXY environment variable contains newline characters

Open yie1d opened this issue 6 months ago • 8 comments

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

yie1d avatar May 24 '26 03:05 yie1d

Hi, I'd like to work on this. I'll submit a PR shortly.

nightcityblade avatar May 24 '26 15:05 nightcityblade

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.

devShaik010 avatar Jun 19 '26 13:06 devShaik010

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.

Muhtasim-Munif-Fahim avatar Jun 19 '26 22:06 Muhtasim-Munif-Fahim

Thanks. I will step back from this since there is already an active fix in progress.

devShaik010 avatar Jun 25 '26 15:06 devShaik010

Stepping back here since there is already an active fix in progress for this issue. I don't want to duplicate effort.

Monkiia avatar Jun 27 '26 03:06 Monkiia

Hi, I'd like to work on this. I'll submit a PR shortly.

nightcityblade avatar Jul 03 '26 03:07 nightcityblade

Working on this in PR #3631.

Xsidz avatar Aug 16 '26 18:08 Xsidz

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.

marcuswood-oai avatar Sep 09 '26 20:09 marcuswood-oai