Skip to content

fix(forward): don't repeat the version segment a base URL ends with - #212

Merged
fylorn merged 1 commit into
mainfrom
fix/base-url-version-segment
Sep 26, 2026
Merged

fylorn merged 1 commit into
mainfrom
fix/base-url-version-segment

Conversation

@fylorn

@fylorn fylorn commented Sep 26, 2026

Copy link
Copy Markdown
Contributor

Summary

An upstream whose base URL ends with its version segment, as provider docs usually give it (https://bedrock-mantle.us-east-1.api.aws/v1, https://api.openai.com/v1), got every request and every connection check sent to .../v1/v1/..., because the client's path (/v1/chat/completions) was appended as it was. The configuration reference already says to write the base URL up to the version segment, so the documented form did not work.

tw_dialect::url::upstream_url now drops the path's first segment when the base URL's path ends with the same version segment:

base URL path upstream URL
https://api.openai.com/v1 /v1/chat/completions https://api.openai.com/v1/chat/completions
https://generativelanguage.googleapis.com/v1beta /v1beta/models/m:streamGenerateContent .../v1beta/models/m:streamGenerateContent
https://relay.example/anthropic /v1/messages unchanged: .../anthropic/v1/messages
https://x.example/v1 /v1beta/models/m unchanged: two different segments
https://x.example/api /api/items unchanged: only version segments count
http://v1:8080 /v1/messages unchanged: v1 is the host, not a path segment

Only v + digits, optionally followed by alpha/beta and digits, counts as a version segment. The same name elsewhere says nothing about it being the same segment.

The enterprise edition builds its upstream URLs with the same function, so it gets this when it moves to a version that includes it.

Test plan

  • cargo fmt --all --check
  • cargo clippy --workspace --all-targets -- -D warnings
  • cargo test --workspace (new cases in tw-dialect url::tests)

🤖 Generated with Claude Code

Provider docs often give the base URL with its version segment
(`https://api.openai.com/v1`), and clients send paths that start with
it (`/v1/chat/completions`). The two were joined as they were, so such
an upstream got `.../v1/v1/chat/completions` and a 404 on every request
and every connection check, although the configuration reference says
to write the base URL up to the version segment.

`upstream_url` now drops the path's first segment when the base URL's
path ends with the same version segment (`v1`, `v2`, `v1beta`, ...).
Any other segment is joined as before: the same name there says nothing
about it being the same segment. A base URL without a path is not
affected, including a host that happens to be called `v1`.

Both gateways build upstream URLs with this function, so the enterprise
edition gets the same behaviour when it moves to this version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fylorn
fylorn merged commit e60a43d into main Sep 26, 2026
4 checks passed
@fylorn
fylorn deleted the fix/base-url-version-segment branch September 26, 2026 11:50
@fylorn fylorn mentioned this pull request Sep 26, 2026
2 tasks done
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