fix: sanitize proxy env vars to prevent InvalidURL from newlines#3450
Open
C1-BA-B1-F3 wants to merge 3 commits into
Open
fix: sanitize proxy env vars to prevent InvalidURL from newlines#3450C1-BA-B1-F3 wants to merge 3 commits into
C1-BA-B1-F3 wants to merge 3 commits into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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,.envfiles, or shell scripts), creating an OpenAI client raiseshttpx.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
Solution
Added
sanitize_proxy_env_vars()utility function that:\n,\r) with commas in proxy environment variables_DefaultHttpxClient.__init__,_DefaultAsyncHttpxClient.__init__, and_DefaultAioHttpClient.__init__before httpx reads themChanges
src/openai/_utils/_utils.py: Addedsanitize_proxy_env_vars()functionsrc/openai/_utils/__init__.py: Exported the new functionsrc/openai/_base_client.py: Call sanitization in default httpx client classestests/test_utils/test_sanitize_proxy_env.py: Added comprehensive testsTesting
All tests pass, including integration test verifying that creating
_DefaultHttpxClientwith newlines inNO_PROXYno longer raises.Fixes #3303