Skip to content

fix(anthropic): strip trailing /v1 from base_url in build_anthropic_client (#24833) - #37656

Closed
rodboev wants to merge 4 commits into
NousResearch:mainfrom
rodboev:pr/anthropic-strip-v1-base-url
Closed

fix(anthropic): strip trailing /v1 from base_url in build_anthropic_client (#24833)#37656
rodboev wants to merge 4 commits into
NousResearch:mainfrom
rodboev:pr/anthropic-strip-v1-base-url

Conversation

@rodboev

@rodboev rodboev commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Summary

The Anthropic SDK appends /v1 to base_url when constructing request URLs. When a user configures model.base_url or ANTHROPIC_BASE_URL with a trailing /v1 (the value the older setup guidance suggested), the SDK produces /v1/v1/messages and every request 404s.

The bearer-hook constructor _build_anthropic_client_with_bearer_hook already strips a trailing /v1 at line 612, but its sibling build_anthropic_client — the path used for OAuth, x-api-key, and plain Bearer auth (the common case) — forwarded the value verbatim. This applies the same strip in the main constructor, aligning the two paths.

Fixes #24833

Changes

  • agent/anthropic_adapter.py: strip trailing /v1 or /v1/ from normalized_base_url in build_anthropic_client(), immediately after _normalize_base_url_text(), mirroring the existing idiom in the bearer-hook constructor (+4 lines)
  • tests/agent/test_anthropic_adapter.py: two new tests covering /v1 and /v1/ suffix stripping (+13 lines)

Validation

Scenario Before After
base_url: https://api.anthropic.com/v1 SDK builds /v1/v1/messages → 404 stripped to https://api.anthropic.com → correct
base_url: https://proxy.example.com/anthropic/v1/ /v1/v1/messages → 404 stripped to https://proxy.example.com/anthropic → correct
base_url: https://custom.api.com (no /v1) unchanged unchanged
Azure endpoint (*.azure.com/models/anthropic) unchanged (uses api-version query) unchanged
Azure Foundry endpoint unchanged (bearer-hook path) unchanged
No base_url set default Anthropic endpoint unchanged

Test plan

  • pytest tests/agent/test_anthropic_adapter.py -v --timeout=0 — 156 passed, 1 skipped
  • test_custom_base_url passes (non-/v1 URLs untouched)
  • test_azure_anthropic_endpoint_keeps_context_1m_beta passes (Azure unaffected)
  • test_azure_foundry_anthropic_endpoint_uses_bearer_auth passes (Foundry unaffected)
  • Manual: model.base_url: https://api.anthropic.com/v1 no longer 404s

@alt-glitch alt-glitch added type/bug Something isn't working P3 Low — cosmetic, nice to have comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint provider/anthropic Anthropic native Messages API labels Jun 2, 2026
@rodboev
rodboev force-pushed the pr/anthropic-strip-v1-base-url branch from a6c9cf1 to a42017d Compare June 28, 2026 16:31
@rodboev
rodboev force-pushed the pr/anthropic-strip-v1-base-url branch from 916a06f to 40723c9 Compare June 28, 2026 20:00
@rodboev

rodboev commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

Withdrawing this in favor of #45842, which already landed with the overlapping Anthropic base URL fix. Closing to keep the queue clean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P3 Low — cosmetic, nice to have 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.

Anthropic provider 404s when base_url includes /v1 (SDK adds its own)

2 participants