Skip to content

fix: sanitize proxy env vars to prevent InvalidURL from newlines#3450

Open
C1-BA-B1-F3 wants to merge 3 commits into
openai:mainfrom
C1-BA-B1-F3:fix/no-proxy-newline-sanitization
Open

fix: sanitize proxy env vars to prevent InvalidURL from newlines#3450
C1-BA-B1-F3 wants to merge 3 commits into
openai:mainfrom
C1-BA-B1-F3:fix/no-proxy-newline-sanitization

Conversation

@C1-BA-B1-F3

Copy link
Copy Markdown

Summary

Fixes #3303 - InvalidURL error when NO_PROXY environment variable contains newline characters.

Problem

When proxy environment variables (like NO_PROXY) contain newline characters (common in Docker environments, .env files, or shell scripts), creating an OpenAI client raises httpx.InvalidURL.

This occurs because httpx's get_environment_proxies() only splits 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.

Reproduction

import os
os.environ['NO_PROXY'] = 'localhost\n192.168.1.1'

from openai import OpenAI
client = OpenAI(api_key='sk-test')
# Raises: httpx.InvalidURL: Invalid non-printable ASCII character in URL, '\n' at position 16

Solution

Added sanitize_proxy_env_vars() utility function that:

  • Replaces newlines (\n, \r) with commas in proxy environment variables
  • Strips whitespace from each entry
  • Removes empty entries
  • Is called in _DefaultHttpxClient.__init__, _DefaultAsyncHttpxClient.__init__, and _DefaultAioHttpClient.__init__ before httpx reads them

Changes

  • src/openai/_utils/_utils.py: Added sanitize_proxy_env_vars() function
  • src/openai/_utils/__init__.py: Exported the new function
  • src/openai/_base_client.py: Call sanitization in default httpx client classes
  • tests/test_utils/test_sanitize_proxy_env.py: Added comprehensive tests

Testing

All tests pass, including integration test verifying that creating _DefaultHttpxClient with newlines in NO_PROXY no longer raises.

Fixes #3303

Guard against empty type args when using bare dict or list annotations
(no type parameters, e.g.  instead of ).

Previously:
- construct_type() raised ValueError on bare dict
- construct_type() raised IndexError on bare list
- _transform_recursive() raised IndexError on bare dict

Now these cases gracefully return the data as-is, matching the behavior
of parameterized types when the data already matches.

Fixes openai#3338, openai#3341
…I responses

Fixes openai#3179

The API can return null for the action field in web search calls (e.g.,
when the search is still in progress or the action hasn't been
determined yet). This change makes the action field Optional[Action]
with a default of None, matching the actual API behavior.

This allows users to safely check action.type without getting an
AttributeError when action is None.
When NO_PROXY or other proxy environment variables contain newline
characters (common in Docker, .env files, or shell scripts), httpx's
get_environment_proxies() only splits by comma and fails with InvalidURL.

This fix adds sanitize_proxy_env_vars() that removes newlines from proxy
environment variables before httpx reads them.

Fixes openai#3303
@C1-BA-B1-F3 C1-BA-B1-F3 requested a review from a team as a code owner June 26, 2026 01:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

InvalidURL error when NO_PROXY environment variable contains newline characters

1 participant