Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(router.py): fix setting httpx mounts #4434

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

krrishdholakia
Copy link
Contributor

Title

Fix setting httpx mounts

Relevant issues

Fixes #4376
Fixes #2127
Fixes #3556

Type

🐛 Bug Fix
🧹 Refactoring

Changes

  • fixes logic for getting/setting mounts and no proxy var

[REQUIRED] Testing - Attach a screenshot of any new tests passing locall

If UI changes, send a screenshot/GIF of working UI fixes

  • new test_is_proxy_set to check if the client set uses the proxy

Copy link

vercel bot commented Jun 27, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
litellm ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 3, 2024 0:55am


# Retrieve NO_PROXY environment variable
no_proxy = os.getenv("NO_PROXY", None)
no_proxy_urls = no_proxy.split(",") if no_proxy else []
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_proxy_environment() is already considering the no_proxy values. Those are the values, which have a None assigned to. So you don't need to read here.

async_proxy_mounts[key] = httpx.AsyncHTTPTransport()
else:
sync_proxy_mounts[key] = httpx.HTTPTransport(proxy=proxy)
async_proxy_mounts[key] = httpx.AsyncHTTPTransport(proxy=proxy)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with this is, that you create Transports here, which are different to the Transport you actually want to use later. With this solution, it would use the proxy Transports for all urls, which must use proxy and the no-proxy Transports for the urls which match the NO_PROXY environment variable. Which means, it would never use the transport you assign later with the argument transport to the httpx.AsyncClient.

Instead you have to do it differently:

  • create the transport you actually want to use for all connections (for example AsyncCustomHTTPTransport)
  • assign this proxy to all urls in proxies, where the value is None
  • for every value different to None, create a new transport of the type you want to use (for example AsyncCustomHTTPTransport) or maybe copy it, and apply the proxy value to it

os.environ["HTTPS_PROXY"] = "https://proxy.example.com:8080"
from openai import AsyncAzureOpenAI

# Function to check if a proxy is set on the client
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two times the same comment

for url in no_proxy_urls: # set no-proxy support for specific urls
sync_proxy_mounts[url] = None # type: ignore
async_proxy_mounts[url] = None # type: ignore
sync_proxy_mounts, async_proxy_mounts = create_proxy_transport_and_mounts()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same logic must also be fixed on other places:

  • litellm\llms\custom_httpx\http_handler.py in create_client of class AsyncHTTPHandler
  • litellm\llms\custom_httpx\http_handler.py in __init__ of class HTTPHandler

Not needed after azure dall-e-2 refactor
Copy link

@anetbnd anetbnd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very good now. I just noticed a inconsistency in one of the test-cases, which can cause issues on systems, which have set HTTP_PROXY, HTTPS_PROXY and NO_PROXY in their environment.

Just a question out of curiosity: Why is the custom async httpx client not needed anymore? I was thinking it is needed for dall-e?

@pytest.mark.asyncio
async def test_is_proxy_set():
"""
Assert if proxy is set
"""
from httpcore import AsyncHTTPProxy
from httpx import AsyncHTTPTransport

os.environ["HTTPS_PROXY"] = "https://proxy.example.com:8080"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be an issue, because it sets/overwrites the proxy settings from the environment and does not reset those.
I would suggest using a setup/teardown function to store/clean and restore the current environment settings. Or to use a fixture for that.

What you basically have to do is:

  1. read the current values of HTTPS_PROXY, HTTP_PROXY and NO_PROXY into a variable
  2. clean all those values and only set HTTP_PROXY as you do in this line here
  3. after the test (even if it fails), reset HTTPS_PROXY, HTTP_PROXY and NO_PROXY to the values, you have read and stored in point 1

Copy link

@anetbnd anetbnd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found still a bug by using no_proxy on other places.

Furthermore I get the error:

Traceback (most recent call last):
  ... (deleted our code here) ...
  File "/llm_api/.venv/lib/python3.12/site-packages/litellm/__init__.py", line 743, in <module>
    from litellm.litellm_core_utils.litellm_logging import Logging
  File "/llm_api/.venv/lib/python3.12/site-packages/litellm/litellm_core_utils/litellm_logging.py", line 62, in <module>
    from ..integrations.langfuse import LangFuseLogger
  File "/llm_api/.venv/lib/python3.12/site-packages/litellm/integrations/langfuse.py", line 7, in <module>
    from packaging.version import Version
ModuleNotFoundError: No module named 'packaging'

Can it be, that packaging is not added to the dependencies in this version?
If I add it to our project dependencies it runs, but the import is inside litellm.

@@ -3378,40 +3375,6 @@ def set_client(self, model: dict):
litellm_params["max_retries"] = max_retries

# proxy support
import os
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is still similar code in litellm\llms\custom_httpx\http_handler.py, which leads to errors, when importing litellm, because it defines mounts by using values from the NO_PROXY. It is in __init__(...) of HTTPHandler and in create_client(...) of AsyncHTTPHandler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants