Skip to content

Conversation

@karpetrosyan
Copy link
Collaborator

closes #878

@karpetrosyan karpetrosyan requested a review from a team as a code owner June 9, 2025 15:50
@karpetrosyan karpetrosyan changed the title Fix default transport conflict with proxies fix(httpx): resolve conflict between default transport and proxy settings Jun 9, 2025
@RobertCraigie RobertCraigie changed the base branch from main to next June 9, 2025 16:06
@RobertCraigie RobertCraigie changed the base branch from next to main June 9, 2025 16:07
@karpetrosyan karpetrosyan changed the base branch from main to next June 9, 2025 16:51

mounts = tuple(client._client._mounts.items())
assert len(mounts) == 1
assert mounts[0][0].pattern == "https://"
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: can we include a check for the hostname here as well to make sure it's set to example.org?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's actually not a string, but a transport object that hides how it retrieves the response. I decided to avoid checking the hostname to prevent diving into deep internals that might change frequently and break compatibility. I'm only checking the ._mounts property, which I believe will remain stable.

default_transport = AsyncHTTPTransport(**transport_kwargs)

# Prioritize the mounts set by the user over the environment variables.
kwargs["mounts"] = proxy_mounts | kwargs.get("mounts", {})
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: I think the | syntax here is Python 3.9 only? We currently support 3.8

proxy_map = {key: None if url is None else Proxy(url=url) for key, url in get_environment_proxies().items()}

transport_kwargs = {
arg: kwargs[arg] for arg in ("verify", "cert", "trust_env", "http1", "http2", "limits") if arg in kwargs
Copy link
Collaborator

Choose a reason for hiding this comment

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

would it be feasible to add unit tests for all of these args as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I guess it's the same problem here. The transport doesn't expose any attributes, so we could write some references to the internal variables if it's worth it. But I think we can avoid that because we are just testing here whether the arguments were correctly passed to httpx.

Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe the best tests here would just be 2 tests like this?

  1. you can pass the arguments without any errors happening
  2. mock the transport class to just make sure the arguments are passed to it?

I think #2 could be nice to have but #1 would provide the most value imo

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added a simple smoke test to ensure there are no exceptions when providing arguments to the default client. Also suppressed the deprecation warning because httpx deprecated cert and will remove it in the major release.

Copy link
Collaborator

@RobertCraigie RobertCraigie left a comment

Choose a reason for hiding this comment

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

Very nice, thank you!

@RobertCraigie RobertCraigie merged commit a6efded into anthropics:next Jun 10, 2025
@stainless-app stainless-app bot mentioned this pull request Jun 10, 2025
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.

https_proxy not working when upgrade to v0.47.0

2 participants