Skip to content

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

Closed
AlexMikhalev wants to merge 3 commits into
mainfrom
fix/anthropic-endpoint-path-normalization-2026-08-11
Closed

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

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 unit tests covering all three shapes + the streaming-vs-chat equivalence. Verified via cargo test --lib — all 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 6 new tests

Out of scope

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

AlexMikhalev and others added 3 commits November 3, 2025 11:53
Resolved conflicts in Z.AI adapter implementation:
- Consolidated duplicate Zai/ZAi adapters into single Zai adapter
- Updated model routing logic for Z.AI models
- Removed obsolete zhipu adapter references
- Added cerebras module to adapters
- Preserved dual endpoint support for Z.AI coding plans

🤖 Generated with [terraphim.ai](https://terraphim.ai)

Co-Authored-By: Claude <noreply@anthropic.com>
- Fixed import paths for adapters after module reorganization
- Removed remaining duplicate ZAi adapter references
- Added missing openrouter module export
- Ensured consistent adapter import structure

🤖 Generated with [terraphim.ai](https://terraphim.ai)

Co-Authored-By: Claude <noreply@anthropic.com>
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`.

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

Copy link
Copy Markdown
Author

Closing in favor of PR #7 — same fix rebased onto current main (v0.6.0-beta.8-fork). The original branch was 9 commits behind main.

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