Skip to content

[Fix] Follow redirects when a custom httpx client is supplied to the OpenAI SDK - #2633

Open
CastilloLuis wants to merge 1 commit into
open-compass:mainfrom
the-gridai:thegridai/follow-redirects-openai-sdk
Open

[Fix] Follow redirects when a custom httpx client is supplied to the OpenAI SDK#2633
CastilloLuis wants to merge 1 commit into
open-compass:mainfrom
the-gridai:thegridai/follow-redirects-openai-sdk

Conversation

@CastilloLuis

Copy link
Copy Markdown

Problem

OpenAISDK and OpenAISDKRollout build their own httpx.Client and hand it to OpenAI(http_client=...). That silently disables redirect following:

  • httpx.Client() defaults follow_redirects=False.
  • openai-python only applies its own follow_redirects=True when it constructs the client — _base_client.py does self._client = http_client or SyncHttpxClientWrapper(...), and only that wrapper calls kwargs.setdefault("follow_redirects", True).

So supplying http_client bypasses the SDK default, and any endpoint that answers with a redirect fails. The failure is unhelpful: the raw redirect response surfaces as an APIStatusError containing 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's setdefault, a user who explicitly passes http_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:

before (http_client_cfg = {})      APIStatusError: redirect not followed
after  (setdefault applied)        OK -> openai/gpt-oss-120b
user override preserved            follow_redirects=False (not clobbered)

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

  • OpenAISDKRollout passes None when http_client_cfg is empty, so the SDK builds its own client and that path was already correct; the setdefault only applies where a client is actually constructed.
  • The legacy OpenAI class uses requests, which follows redirects already, so it is unaffected.
  • There is a user-side workaround today (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.

@CastilloLuis
CastilloLuis force-pushed the thegridai/follow-redirects-openai-sdk branch from 6b90c7f to 0329969 Compare September 6, 2026 14:53
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.

2 participants