fix: fallback to messages ping in async_setup_entry when proxy returns 404 on models endpoint

This commit is contained in:
Robot committed 2026-02-26 17:42:24 +00:00
1 parent cfb53c25f6
commit 1492d4b503
1 file changed
+16
@@ -49,6 +49,22 @@ async def async_setup_entry(hass: HomeAssistant, entry: AnthropicConfigEntry) ->
await client.models.list(timeout=10.0)
except anthropic.AuthenticationError as err:
raise ConfigEntryAuthFailed(err) from err
except anthropic.APIStatusError as err:
if err.status_code == 404:
# Proxy doesn't support models endpoint — do a minimal ping instead
try:
await client.messages.create(
model=entry.data.get(CONF_CHAT_MODEL) or "claude-3-haiku-20240307",
max_tokens=1,
messages=[{"role": "user", "content": "hi"}],
timeout=10.0,
)
except anthropic.AuthenticationError as auth_err:
raise ConfigEntryAuthFailed(auth_err) from auth_err
except anthropic.AnthropicError as ping_err:
raise ConfigEntryNotReady(ping_err) from ping_err
else:
raise ConfigEntryNotReady(err) from err
except anthropic.AnthropicError as err:
raise ConfigEntryNotReady(err) from err