Skip to content

fix(vertex): honor custom base_url as gateway prefix#4200

Open
yyhhyyyyyy wants to merge 2 commits intoQuantumNous:mainfrom
yyhhyyyyyy:fix/vertex-gateway-base-url
Open

fix(vertex): honor custom base_url as gateway prefix#4200
yyhhyyyyyy wants to merge 2 commits intoQuantumNous:mainfrom
yyhhyyyyyy:fix/vertex-gateway-base-url

Conversation

@yyhhyyyyyy
Copy link
Copy Markdown
Contributor

@yyhhyyyyyy yyhhyyyyyy commented Apr 12, 2026

⚠️ 提交说明 / PR Notice

Important

  • 请提供人工撰写的简洁摘要,避免直接粘贴未经整理的 AI 输出。

📝 变更描述 / Description

当前 Vertex 渠道即使配置了自定义 base_url,请求仍会走硬编码的官方上游地址,导致如 Cloudflare AI Gateway 这类自定义网关无法生效。

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 IssuesPRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

(请在此粘贴截图、关键日志或测试报告,以证明变更生效)

  1. 修复前:未真正经过 Cloudflare AI Gateway
ChannelMeta.BaseURL:
https://gateway.ai.cloudflare.com/v1/<CF_ACCOUNT_ID>/<CF_GATEWAY_ID>/<CF_PROVIDER_SLUG>

fullRequestURL:
https://aiplatform.googleapis.com/v1/projects/<VERTEX_PROJECT_ID>/locations/global/publishers/google/models/gemini-3.1-pro-preview:generateContent

final response:
200 OK
object: "chat.completion"
model: "gemini-3.1-pro-preview"

2.修复后:已成功经过 Cloudflare AI Gateway

ChannelMeta.BaseURL:
https://gateway.ai.cloudflare.com/v1/<CF_ACCOUNT_ID>/<CF_GATEWAY_ID>/<CF_PROVIDER_SLUG>

fullRequestURL:
https://gateway.ai.cloudflare.com/v1/<CF_ACCOUNT_ID>/<CF_GATEWAY_ID>/<CF_PROVIDER_SLUG>/v1/projects/<VERTEX_PROJECT_ID>/locations/global/publishers/google/models/gemini-3.1-pro-preview:generateContent

final response:
200 OK
object: "chat.completion"
model: "gemini-3.1-pro-preview"

Summary by CodeRabbit

  • New Features

    • Improved URL construction for Vertex endpoints with robust handling of custom base URLs, regions, and publisher-specific endpoints (Google, Anthropic, Open Source).
  • Bug Fixes

    • Added validation and consistent URL generation for fetch/predict operations, reducing malformed endpoint errors.
  • Tests

    • Expanded test suite covering URL builders, request URL selection, streaming behavior, and error cases.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 12, 2026

Walkthrough

Centralized Vertex API URL construction into new helpers and updated adaptor code to delegate URL building; added tests for custom base URLs and validation; removed inline region/project/model parsing in task fetch logic.

Changes

Cohort / File(s) Summary
URL Builder Foundation
relay/channel/vertex/url_builder.go, relay/channel/vertex/url_builder_test.go
New exported helpers and constants for normalizing base URLs, regions, API versions, and building publisher-specific model and open-source chat-completions endpoints; comprehensive tests for version deduplication, custom bases, and API-key query behavior.
Task Adaptor Refactoring
relay/channel/task/vertex/adaptor.go, relay/channel/task/vertex/adaptor_test.go
Introduced buildFetchOperationURL to parse upstream operation names and validate parts, removed inline parsing in FetchTask, and delegated submit/predict URL construction to centralized builders; added tests for custom base URL and validation errors.
Channel Adaptor Refactoring
relay/channel/vertex/adaptor.go
Replaced manual region/global branching and inline endpoint strings with calls to BuildGoogleModelURL, BuildAnthropicModelURL, and BuildOpenSourceChatCompletionsURL, preserving API-key query handling.

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant TaskAdaptor
  participant URLBuilder
  participant VertexAPI

  Client->>TaskAdaptor: submit/fetch request (includes upstream name, baseURL, credentials)
  TaskAdaptor->>URLBuilder: BuildGoogleModelURL / BuildAnthropicModelURL / buildFetchOperationURL
  URLBuilder-->>TaskAdaptor: returns constructed endpoint URL
  TaskAdaptor->>VertexAPI: HTTP request to constructed URL (with query/key as needed)
  VertexAPI-->>TaskAdaptor: operation / response
  TaskAdaptor-->>Client: return result
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

  • fix veo3 adapter #1794: Modifies the same TaskAdaptor URL construction and fetch logic in relay/channel/task/vertex/adaptor.go.
  • veo #1792: Overlaps with changes to BuildRequestURL and FetchTask in the TaskAdaptor.
  • feat: vertex veo (#1450) #1659: Alters Vertex task adaptor URL-building logic that touches the same code paths.

Suggested reviewers

  • Calcium-Ion

Poem

🐰 I hopped through code with nimble toes,
Collected URLs where wild logic grows.
Trimmed the slashes, fixed the route,
Now Vertex endpoints sing — no doubt! 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title directly and accurately summarizes the main change: fixing Vertex to honor a custom base_url as a gateway prefix instead of ignoring it.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@relay/channel/task/vertex/adaptor.go`:
- Around line 233-241: The code currently allows a missing project when baseURL
is provided, causing malformed endpoints; update the logic in the adaptor where
extractProjectFromOperationName(upstreamName) is used to require a non-empty
project regardless of baseURL: validate project (from
extractProjectFromOperationName) with strings.TrimSpace and return an error like
"cannot extract project from operation name" if empty, before calling
vertexcore.BuildGoogleModelURL (which uses vertexcore.DefaultAPIVersion,
project, region, modelName, "fetchPredictOperation"); keep the existing
modelName check and ensure both modelName and project are validated and fail
fast rather than relying on BuildAPIBaseURL behavior.

In `@relay/channel/vertex/url_builder.go`:
- Around line 28-32: The code currently appends "/"+version to any normalized
base URL; change this in the normalizeVertexBaseURL usage so you only append
when the normalized base does not already include the version: trim spaces and
slashes from both normalized and version, normalize trailing slashes on
normalized, then check if normalized already has the version as the final path
segment (e.g., strings.HasSuffix(normalized, "/"+version) or normalized ==
version) and only do normalized = normalized + "/" + version when that check is
false; update the logic around normalizeVertexBaseURL, baseURL, and version to
perform these defensive checks.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 64bc7259-1c4b-4253-926b-6a759a848ff7

📥 Commits

Reviewing files that changed from the base of the PR and between b4df995 and 59f0b6f.

📒 Files selected for processing (5)
  • relay/channel/task/vertex/adaptor.go
  • relay/channel/task/vertex/adaptor_test.go
  • relay/channel/vertex/adaptor.go
  • relay/channel/vertex/url_builder.go
  • relay/channel/vertex/url_builder_test.go

Comment thread relay/channel/task/vertex/adaptor.go
Comment thread relay/channel/vertex/url_builder.go Outdated
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
relay/channel/task/vertex/adaptor.go (1)

85-99: ⚠️ Potential issue | 🟡 Minor

Fail fast when credentials are missing project_id before building submit URL.

At Line 98, URL construction proceeds even if adc.ProjectID is empty. With a custom gateway base URL, that can generate an invalid path and surface as a confusing upstream error instead of a clear local validation error.

Suggested patch
 func (a *TaskAdaptor) BuildRequestURL(info *relaycommon.RelayInfo) (string, error) {
 	adc := &vertexcore.Credentials{}
 	if err := common.Unmarshal([]byte(a.apiKey), adc); err != nil {
 		return "", fmt.Errorf("failed to decode credentials: %w", err)
 	}
+	if strings.TrimSpace(adc.ProjectID) == "" {
+		return "", fmt.Errorf("missing project_id in credentials")
+	}
 	modelName := info.UpstreamModelName
 	if modelName == "" {
 		modelName = "veo-3.0-generate-001"
 	}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@relay/channel/task/vertex/adaptor.go` around lines 85 - 99, The code calls
vertexcore.BuildGoogleModelURL using adc.ProjectID even when adc.ProjectID may
be empty, which can produce invalid URLs; update the adaptor code (the block
that decodes credentials into adc and then calls BuildGoogleModelURL) to
validate adc.ProjectID after unmarshalling and return a clear error (e.g.,
"missing project_id in credentials") if it's empty before calling
vertexcore.BuildGoogleModelURL or proceeding with URL construction, so callers
get a fast, descriptive failure instead of an upstream error.
🧹 Nitpick comments (1)
relay/channel/vertex/url_builder_test.go (1)

83-194: Consider consolidating adaptor URL tests into a table-driven structure.

The setup/assert pattern is repeated across multiple tests, which makes expansion noisier than needed.

♻️ Refactor sketch
+func assertRequestURL(t *testing.T, info *relaycommon.RelayInfo, want string) {
+	t.Helper()
+	adaptor := &Adaptor{}
+	adaptor.Init(info)
+	got, err := adaptor.GetRequestURL(info)
+	if err != nil {
+		t.Fatalf("GetRequestURL returned error: %v", err)
+	}
+	if got != want {
+		t.Fatalf("GetRequestURL() = %q, want %q", got, want)
+	}
+}
+
-func TestAdaptorGetRequestURLUsesCustomBaseForGemini(t *testing.T) {
-	t.Parallel()
-	...
-}
+func TestAdaptorGetRequestURLCases(t *testing.T) {
+	t.Parallel()
+	tests := []struct {
+		name string
+		info *relaycommon.RelayInfo
+		want string
+	}{
+		// gemini/claude/open-source/api-key cases...
+	}
+	for _, tc := range tests {
+		tc := tc
+		t.Run(tc.name, func(t *testing.T) {
+			t.Parallel()
+			assertRequestURL(t, tc.info, tc.want)
+		})
+	}
+}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@relay/channel/vertex/url_builder_test.go` around lines 83 - 194, Consolidate
the four near-duplicate tests (TestAdaptorGetRequestURLUsesCustomBaseForGemini,
TestAdaptorGetRequestURLUsesCustomBaseForClaude,
TestAdaptorGetRequestURLUsesCustomBaseForOpenSource,
TestAdaptorGetRequestURLPreservesAPIKeyQueryWithCustomBase) into a single
table-driven test that iterates test cases describing RelayInfo/ChannelMeta
inputs and expected URLs; for each case create an Adaptor, call
adaptor.Init(info) and adaptor.GetRequestURL(info), assert error nil and compare
got vs want. Reuse mustMarshalCredentials where needed and include fields
IsStream, OriginModelName, ChannelMeta.ApiVersion, ChannelBaseUrl, ApiKey and
ChannelOtherSettings.VertexKeyType in the table rows so each scenario (gemini
streaming, claude, open-source, API-key query) is covered. Ensure each case
t.Run with a descriptive name and keep t.Parallel() per subtest.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@relay/channel/task/vertex/adaptor.go`:
- Around line 85-99: The code calls vertexcore.BuildGoogleModelURL using
adc.ProjectID even when adc.ProjectID may be empty, which can produce invalid
URLs; update the adaptor code (the block that decodes credentials into adc and
then calls BuildGoogleModelURL) to validate adc.ProjectID after unmarshalling
and return a clear error (e.g., "missing project_id in credentials") if it's
empty before calling vertexcore.BuildGoogleModelURL or proceeding with URL
construction, so callers get a fast, descriptive failure instead of an upstream
error.

---

Nitpick comments:
In `@relay/channel/vertex/url_builder_test.go`:
- Around line 83-194: Consolidate the four near-duplicate tests
(TestAdaptorGetRequestURLUsesCustomBaseForGemini,
TestAdaptorGetRequestURLUsesCustomBaseForClaude,
TestAdaptorGetRequestURLUsesCustomBaseForOpenSource,
TestAdaptorGetRequestURLPreservesAPIKeyQueryWithCustomBase) into a single
table-driven test that iterates test cases describing RelayInfo/ChannelMeta
inputs and expected URLs; for each case create an Adaptor, call
adaptor.Init(info) and adaptor.GetRequestURL(info), assert error nil and compare
got vs want. Reuse mustMarshalCredentials where needed and include fields
IsStream, OriginModelName, ChannelMeta.ApiVersion, ChannelBaseUrl, ApiKey and
ChannelOtherSettings.VertexKeyType in the table rows so each scenario (gemini
streaming, claude, open-source, API-key query) is covered. Ensure each case
t.Run with a descriptive name and keep t.Parallel() per subtest.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4df6d6d2-ea15-4cc6-8ed4-40b9fb47fbad

📥 Commits

Reviewing files that changed from the base of the PR and between 59f0b6f and 3e52e8a.

📒 Files selected for processing (4)
  • relay/channel/task/vertex/adaptor.go
  • relay/channel/task/vertex/adaptor_test.go
  • relay/channel/vertex/url_builder.go
  • relay/channel/vertex/url_builder_test.go
✅ Files skipped from review due to trivial changes (1)
  • relay/channel/task/vertex/adaptor_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • relay/channel/vertex/url_builder.go

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