Skip to content

fix(adapter): handle Anthropic-compat gateways without /v1/ in base URL - #7

Merged
AlexMikhalev merged 3 commits into
mainfrom
fix/anthropic-endpoint-path-normalization-2026-08-11-v2
Aug 11, 2026
Merged

fix(adapter): handle Anthropic-compat gateways without /v1/ in base URL#7
AlexMikhalev merged 3 commits into
mainfrom
fix/anthropic-endpoint-path-normalization-2026-08-11-v2

Conversation

@AlexMikhalev

Copy link
Copy Markdown

Problem

The Anthropic adapter's get_service_url used format!("{base_url}messages"), which concatenates the base URL with messages without any slash separator. This works for the default Anthropic base URL (https://api.anthropic.com/v1/) because the trailing slash happens to align with the message path. It produces malformed URLs for Anthropic-compat gateways whose base URL doesn't end in /v1/ — e.g. terraphim-llm-proxy's MiniMax provider at https://api.minimax.io/anthropic was producing https://api.minimax.io/anthropicmessages (404).

This affects both Chat and ChatStream service types in the same way, so streaming requests to MiniMax (and any other non-Anthropic-but-compat provider) fail with 404 today.

Fix

Three-way URL construction:

if base_url ends in "messages"             → pass through unchanged
else if base_url ends in "/v1/" or "/v1"   → append "messages" (legacy Anthropic behavior)
else                                       → append "/v1/messages" with proper slash

Tests

6 new unit tests covering all three shapes + the streaming-vs-chat equivalence. 71/71 lib tests pass.

Context

Discovered while validating terraphim-llm-proxy's MiniMax highspeed offerings on 2026-08-11. PR terraphim/terraphim-llm-proxy#19 fixed the config side; this PR fixes the genai adapter side. With both landed, MiniMax highspeed models route correctly under all conditions:

  • Non-streaming via minimax_client::send_request ✅ (working today)
  • Streaming via genai Anthropic adapter ✅ (fixed by this PR)

Acceptance criteria

  • Anthropic default base URL still produces https://api.anthropic.com/v1/messages
  • /v1 suffix without trailing slash works (preserves legacy behavior)
  • Bare host gets /v1/messages appended with proper slash
  • Bare host with trailing slash gets /v1/messages appended after slash trim
  • Fully-formed URL ending in messages passes through unchanged
  • Streaming and chat use identical URL logic
  • cargo test --lib passes all 71 tests (6 new + 65 existing)

Out of scope

The Anthropic adapter also has a separate bug (terraphim-llm-proxy#18) where tools: [] payloads get forwarded to OpenAI-compat upstreams and rejected with tools[0].type:type cannot be empty. That's a request-shape adapter issue in to_web_request_data, not a URL construction issue, and needs a separate fix.

Alex added 3 commits August 11, 2026 12:02
The Anthropic adapter's get_service_url used `format!("{base_url}messages")`,
which concatenates the base URL with `messages` without any slash separator.
This works for the default Anthropic base URL (`https://api.anthropic.com/v1/`)
because the trailing slash happens to align with the message path. It produces
malformed URLs for Anthropic-compat gateways whose base URL doesn't end in
/v1/ — e.g. terraphim-llm-proxy's MiniMax provider at
`https://api.minimax.io/anthropic` was producing
`https://api.minimax.io/anthropicmessages` (404).

This affects both Chat and ChatStream service types in the same way, so
streaming requests to MiniMax (and any other non-Anthropic-but-compat
provider) fail with 404 today.

Fix the URL construction to handle three shapes:
- base_url ends in `messages`           → pass through unchanged
- base_url ends in `/v1/` or `/v1`     → append `messages` (legacy Anthropic behavior)
- base_url is anything else             → append `/v1/messages` with proper slash

Add 6 unit tests covering all shapes and confirming streaming == chat
URL construction. Verified locally via `cargo test --lib` (71/71 pass).

Discovered while validating terraphim-llm-proxy's MiniMax highspeed
offerings on 2026-08-11; see terraphim-llm-proxy#18 (Anthropic route) and
the gitea jeremychone#19 config PR for related context. Streaming was the remaining
gap after PR jeremychone#19 landed.
The previous fix used `format!("{base_url}messages")` when base_url ended
in `/v1`, which produces `https://api.minimax.io/anthropic/v1messages` —
no slash separator, 404.

Real-world gateways like the terraphim-llm-proxy MiniMax provider use
`https://api.minimax.io/anthropic/v1` as the base URL, and the actual
upstream endpoint is `/anthropic/v1/messages`. Split the `/v1` arm
into two:
- base_url ends with `/v1/`  → legacy Anthropic shape, append `messages`
- base_url ends with `/v1`    → insert `/`, append `messages`

Update the regression test to assert `/v1/messages` for the no-trailing-slash case.

This is a follow-up to PR #7 (#7) — the streaming
build I tested locally produced 404 because of this. All 13 anthropic
unit tests pass.
Clippy 1.96 added the `clippy::collapsible_match` lint which flags
`match arm { if cond { body } }` and suggests converting to
`match arm if cond => { body }`.

Apply to two arms in `src/webc/web_stream.rs` (`'['` and `']'`)
that both contain `if depth == 0`. No behavior change.

Required to keep CI passing under `-D warnings` once stable Rust
ships 1.96. PR #7 (anthropic endpoint URL normalization) hits this
lint and fails the Lint/Build/Test job without this fix.
@AlexMikhalev
AlexMikhalev merged commit 164908c into main Aug 11, 2026
1 check passed
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