feat: add opt-in "Follow redirects" connection flag - #449
Open
maxdrift wants to merge 1 commit into
Open
Conversation
Home Assistant's shared httpx client disables redirect following by
default (follow_redirects=False), while the OpenAI SDK's own default
client follows redirects. When an OpenAI-compatible host returns an
HTTP 3xx before the API (e.g. a reverse proxy, gateway, or SSO
front-end), the integration surfaces the redirect body as a
connection error:
Sorry, I had a problem talking to OpenAI:
<html><body>You are being redirected.</body></html>
Add an opt-in "Follow redirects" flag (default false) on the main
config entry and the change_config service. When enabled, the
integration builds the http client with HA's
create_async_httpx_client(hass, follow_redirects=True) so the SDK
reuses the same shared client lifecycle (auto-cleanup) while opting
into redirect following.
Touches:
- const.py: new CONF_FOLLOW_REDIRECTS / DEFAULT_FOLLOW_REDIRECTS.
- helpers.py: get_authenticated_client gains follow_redirects and
swaps in create_async_httpx_client when set; both Azure and
non-Azure branches use the selected client.
- __init__.py / config_flow.py: thread the flag from entry data and
the user step.
- services.py: support updating the flag at runtime via
change_config.
- strings.json / translations/en.json / services.yaml: surface the
flag in the UI.
- docs/configuration.mdx: document the new option, including the
error message users will recognize.
- tests/test_helpers.py: cover both branches and the Azure path.
Default is OFF so existing users see no behavioral change.
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.
First off, thanks for maintaining this integration — what drew me to it is the ability to point it at any OpenAI-compatible host (TheGrid.ai, homelab gateways, etc.) rather than being locked to OpenAI/OpenRouter.
Small opt-in contribution back; happy to iterate on feedback.
Summary
Adds an opt-in Follow redirects flag to the integration's connection
settings so users behind a reverse proxy, gateway, or SSO front-end can
talk to OpenAI-compatible hosts that issue an HTTP
3xxbefore reachingthe actual API.
Discovered while testing this integration with TheGrid.ai, which performs a 307 redirect (within the same host)
Problem
Home Assistant's shared httpx client (
get_async_client) disables redirectfollowing by default (
follow_redirects=False), whereas the OpenAI SDK'sown default client follows redirects. Because this integration reuses HA's
shared client, any host that responds with a redirect surfaces the redirect
body as a connection error instead of following it:
Solution
CONF_FOLLOW_REDIRECTS/DEFAULT_FOLLOW_REDIRECTS(defaultFalse).get_authenticated_clientbuilds the client withcreate_async_httpx_client(hass, follow_redirects=True)instead ofget_async_client(hass). This keeps HA's shared client lifecycle(automatic cleanup) while opting into redirect following. Applies to both
the Azure and non-Azure branches.
__init__.py(entry data) andconfig_flow.py(user step), and allow updating it at runtime via the
change_configservice.
strings.json,translations/en.json,services.yaml) and document it indocs/configuration.mdx.Default is OFF, so existing users see no behavioral change — this is
purely opt-in.
Testing
tests/test_helpers.pycovers the enabled path, the default (sharedclient) path, and the Azure branch, asserting the correct http client is
passed to the SDK constructor.
ruff check/ruff format --checkclean.pytest tests/passes.How to use
Enable Settings → Devices & Services → Extended OpenAI Conversation →
Configure → Follow redirects, or set it at runtime via the
change_configservice.