Skip to content

fix(cli): model validation fails for anthropic_messages and Cloudflare-protected endpoints#12950

Closed
Wangshengyang2004 wants to merge 1 commit into
NousResearch:mainfrom
Wangshengyang2004:fix/anthropic-model-validation
Closed

fix(cli): model validation fails for anthropic_messages and Cloudflare-protected endpoints#12950
Wangshengyang2004 wants to merge 1 commit into
NousResearch:mainfrom
Wangshengyang2004:fix/anthropic-model-validation

Conversation

@Wangshengyang2004

Copy link
Copy Markdown
Contributor

Summary

The /model command fails to switch to providers using api_mode: anthropic_messages
and to some OpenAI-compatible endpoints behind Cloudflare, because validate_requested_model()
always probes with Authorization: Bearer (OpenAI-style auth) via urllib.request
without a User-Agent header.

Root causes

  1. Anthropic Models API uses different auth: Anthropic's GET /v1/models
    requires x-api-key + anthropic-version headers, not Authorization: Bearer.
    The response shape (data[].id) is identical to OpenAI, but the probe always
    failed with auth errors.

  2. Cloudflare blocks requests without User-Agent: urllib.request does not
    set a User-Agent header by default. Some third-party OpenAI-compatible
    endpoints behind Cloudflare return 403, causing the probe to fail even though
    the endpoint works fine with normal HTTP clients.

  3. Hard rejection on probe failure: When the /models probe failed for
    anthropic_messages providers, validate_requested_model() returned
    accepted: False, blocking the model switch entirely. Many Anthropic-compatible
    proxies do not implement the Models API at all, so this was a permanent block.

Changes

hermes_cli/models.py

  • probe_api_models(): Added api_mode parameter. When api_mode == "anthropic_messages", sends x-api-key and anthropic-version headers
    instead of Authorization: Bearer. Always sets User-Agent: Hermes/1.0.
  • fetch_api_models(): Propagates api_mode to probe_api_models().
  • validate_requested_model(): Added api_mode parameter.
    • For anthropic_messages mode: attempts probe with correct Anthropic auth.
      If the proxy implements the Models API (like the official Anthropic endpoint),
      the model is verified normally. If the probe fails (common for third-party
      proxies), the model is still accepted with an informational warning
      instead of being rejected.
    • Custom provider "custom" branch: same logic — tries Anthropic auth first,
      falls back to accept on failure.

hermes_cli/model_switch.py

  • switch_model(): Passes api_mode through to validate_requested_model().

How to test

  1. Configure a custom provider with api_mode: anthropic_messages in
    ~/.hermes/config.yaml under custom_providers.
  2. Run /model <model-name> with that provider — it should accept and switch.
  3. Configure a custom provider with a Cloudflare-protected OpenAI-compatible
    endpoint — the /model probe should now succeed.
  4. Existing tests: pytest tests/hermes_cli/ -v passes (614 passed; 2
    pre-existing failures unrelated to this change).

Platforms tested

  • Linux (ARM64, Orange Pi 4 Pro)
  • Python 3.11

…are-protected endpoints

- probe_api_models: add api_mode param; use x-api-key + anthropic-version
  headers for anthropic_messages mode (Anthropic's native Models API auth)
- probe_api_models: add User-Agent header to avoid Cloudflare 403 blocks
  on third-party OpenAI-compatible endpoints
- validate_requested_model: pass api_mode through from switch_model
- validate_requested_model: for anthropic_messages mode, attempt probe with
  correct auth; if probe fails (many proxies don't implement /v1/models),
  accept the model with an informational warning instead of rejecting
- fetch_api_models: propagate api_mode to probe_api_models
@Wangshengyang2004 Wangshengyang2004 force-pushed the fix/anthropic-model-validation branch from 7acbf7a to a53489f Compare April 20, 2026 09:54
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/cli CLI entry point, hermes_cli/, setup wizard provider/anthropic Anthropic native Messages API labels Apr 22, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Related to #9721 (custom HTTP headers for Cloudflare) and #12906 (User-Agent on /v1/models probe, closed). This PR addresses both Anthropic auth and Cloudflare in one fix.

@alt-glitch

Copy link
Copy Markdown
Collaborator

Related to #9721 and #12906.

@teknium1

Copy link
Copy Markdown
Contributor

Thanks for this fix, @Wangshengyang2004! The changes from this PR have already landed on main.

This is an automated hermes-sweeper review.

Evidence:

  • Commit 647900e81 (the exact commit from this PR's head) is confirmed reachable from main via git merge-base --is-ancestor.
  • hermes_cli/models.py lines 2336–2339: _HERMES_USER_AGENT is set on all probes; x-api-key + anthropic-version headers are sent when api_mode == 'anthropic_messages'.
  • hermes_cli/models.py lines 2780+: validate_requested_model() now accepts with an informational warning when the probe fails for anthropic_messages providers.
  • A follow-on commit 2303dd868 by a separate contributor added a native anthropic provider branch using _fetch_anthropic_models() (referenced in the alt-glitch comments as a related fix for [Bug] Cannot set custom HTTP headers for custom providers — Cloudflare 403 #9721/fix(cli): set User-Agent on /v1/models probe (Cloudflare 1010) #12906).

Closing as implemented. 🙏

@teknium1 teknium1 closed this Apr 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/cli CLI entry point, hermes_cli/, setup wizard P2 Medium — degraded but workaround exists provider/anthropic Anthropic native Messages API type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants