Skip to content

feat: add opt-in "Follow redirects" connection flag - #449

Open
maxdrift wants to merge 1 commit into
jekalmin:developfrom
maxdrift:develop
Open

feat: add opt-in "Follow redirects" connection flag#449
maxdrift wants to merge 1 commit into
jekalmin:developfrom
maxdrift:develop

Conversation

@maxdrift

Copy link
Copy Markdown

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 3xx before reaching
the 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 redirect
following by default (follow_redirects=False), whereas the OpenAI SDK's
own 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:

Screenshot 2026-06-25 at 12 54 30

Solution

  • Add CONF_FOLLOW_REDIRECTS / DEFAULT_FOLLOW_REDIRECTS (default False).
  • When enabled, get_authenticated_client builds the client with
    create_async_httpx_client(hass, follow_redirects=True) instead of
    get_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.
  • Thread the flag through __init__.py (entry data) and config_flow.py
    (user step), and allow updating it at runtime via the change_config
    service.
  • Surface the flag in the UI (strings.json, translations/en.json,
    services.yaml) and document it in docs/configuration.mdx.

Default is OFF, so existing users see no behavioral change — this is
purely opt-in.

Testing

  • tests/test_helpers.py covers the enabled path, the default (shared
    client) path, and the Azure branch, asserting the correct http client is
    passed to the SDK constructor.
  • ruff check / ruff format --check clean.
  • pytest tests/ passes.

Note: mypy reports one pre-existing error in helpers.py
(Template with a nullable hass argument) that is unrelated to this
change and present on develop.

How to use

Enable Settings → Devices & Services → Extended OpenAI Conversation →
Configure → Follow redirects
, or set it at runtime via the
change_config service.

Screenshot 2026-06-25 at 14 02 46 Screenshot 2026-06-25 at 12 17 38

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.
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.

1 participant