[Fix] Follow redirects when a custom httpx client is supplied to the OpenAI SDK - #2633
Open
CastilloLuis wants to merge 1 commit into
Open
[Fix] Follow redirects when a custom httpx client is supplied to the OpenAI SDK#2633CastilloLuis wants to merge 1 commit into
CastilloLuis wants to merge 1 commit into
Conversation
CastilloLuis
force-pushed
the
thegridai/follow-redirects-openai-sdk
branch
from
September 6, 2026 14:53
6b90c7f to
0329969
Compare
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.
Problem
OpenAISDKandOpenAISDKRolloutbuild their ownhttpx.Clientand hand it toOpenAI(http_client=...). That silently disables redirect following:httpx.Client()defaultsfollow_redirects=False.follow_redirects=Truewhen it constructs the client —_base_client.pydoesself._client = http_client or SyncHttpxClientWrapper(...), and only that wrapper callskwargs.setdefault("follow_redirects", True).So supplying
http_clientbypasses the SDK default, and any endpoint that answers with a redirect fails. The failure is unhelpful: the raw redirect response surfaces as anAPIStatusErrorcontaining HTML, not a connection error, so it looks like a broken endpoint rather than a client setting.This affects any OpenAI-compatible endpoint that redirects — hosted gateways commonly do — not one specific provider.
Fix
http_client_cfg.setdefault('follow_redirects', True)at both construction sites, so the SDK's own default is preserved. Because it'ssetdefault, a user who explicitly passeshttp_client_cfg=dict(follow_redirects=False)still wins.9 added lines, 6 of which are the explanatory comments.
Reproduction
Against a real endpoint that answers
307, running the exact constructor from_create_fresh_client:For contrast, the same request through a client that openai-python builds itself succeeds — which is what makes this specific to the supplied-client path.
Notes
OpenAISDKRolloutpassesNonewhenhttp_client_cfgis empty, so the SDK builds its own client and that path was already correct; thesetdefaultonly applies where a client is actually constructed.OpenAIclass usesrequests, which follows redirects already, so it is unaffected.http_client_cfg=dict(follow_redirects=True)in the model config), but it is undocumented and the failure mode gives no hint that it's needed.Disclosure: I work on The Grid, an OpenAI-compatible provider whose endpoint redirects — that's how I hit this. The fix isn't provider-specific and I've deliberately not added any Grid config in this PR.