Skip to content

Commit e0fd25a

Browse files
committed
fix: handle global region correctly for Vertex AI Claude models
- Add custom baseURL for global region to use aiplatform.googleapis.com - Prevents incorrect hostname construction (global-aiplatform.googleapis.com) - Add comprehensive test coverage for global region scenarios Fixes #8571
1 parent eeaafef commit e0fd25a

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/api/providers/__tests__/anthropic-vertex.spec.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,58 @@ describe("VertexHandler", () => {
6262
expect(AnthropicVertex).toHaveBeenCalledWith({
6363
projectId: "test-project",
6464
region: "us-central1",
65+
baseURL: undefined,
6566
})
6667
})
68+
69+
it("should use custom baseURL for global region", () => {
70+
handler = new AnthropicVertexHandler({
71+
apiModelId: "claude-3-5-sonnet-v2@20241022",
72+
vertexProjectId: "test-project",
73+
vertexRegion: "global",
74+
})
75+
76+
expect(AnthropicVertex).toHaveBeenCalledWith({
77+
projectId: "test-project",
78+
region: "global",
79+
baseURL: "https://aiplatform.googleapis.com/v1",
80+
})
81+
})
82+
83+
it("should use custom baseURL for global region with JSON credentials", () => {
84+
const mockCredentials = JSON.stringify({ type: "service_account", project_id: "test" })
85+
handler = new AnthropicVertexHandler({
86+
apiModelId: "claude-3-5-sonnet-v2@20241022",
87+
vertexProjectId: "test-project",
88+
vertexRegion: "global",
89+
vertexJsonCredentials: mockCredentials,
90+
})
91+
92+
expect(AnthropicVertex).toHaveBeenCalledWith(
93+
expect.objectContaining({
94+
projectId: "test-project",
95+
region: "global",
96+
baseURL: "https://aiplatform.googleapis.com/v1",
97+
}),
98+
)
99+
})
100+
101+
it("should use custom baseURL for global region with key file", () => {
102+
handler = new AnthropicVertexHandler({
103+
apiModelId: "claude-3-5-sonnet-v2@20241022",
104+
vertexProjectId: "test-project",
105+
vertexRegion: "global",
106+
vertexKeyFile: "/path/to/keyfile.json",
107+
})
108+
109+
expect(AnthropicVertex).toHaveBeenCalledWith(
110+
expect.objectContaining({
111+
projectId: "test-project",
112+
region: "global",
113+
baseURL: "https://aiplatform.googleapis.com/v1",
114+
}),
115+
)
116+
})
67117
})
68118

69119
describe("createMessage", () => {

src/api/providers/anthropic-vertex.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,16 @@ export class AnthropicVertexHandler extends BaseProvider implements SingleComple
3434
const projectId = this.options.vertexProjectId ?? "not-provided"
3535
const region = this.options.vertexRegion ?? "us-east5"
3636

37+
// For the "global" region, we need to use a custom base URL
38+
// The default would be "global-aiplatform.googleapis.com" which is incorrect
39+
// The correct endpoint for global is "aiplatform.googleapis.com"
40+
const baseURL = region === "global" ? `https://aiplatform.googleapis.com/v1` : undefined // Let the SDK use its default for other regions
41+
3742
if (this.options.vertexJsonCredentials) {
3843
this.client = new AnthropicVertex({
3944
projectId,
4045
region,
46+
baseURL,
4147
googleAuth: new GoogleAuth({
4248
scopes: ["https://www.googleapis.com/auth/cloud-platform"],
4349
credentials: safeJsonParse<JWTInput>(this.options.vertexJsonCredentials, undefined),
@@ -47,13 +53,14 @@ export class AnthropicVertexHandler extends BaseProvider implements SingleComple
4753
this.client = new AnthropicVertex({
4854
projectId,
4955
region,
56+
baseURL,
5057
googleAuth: new GoogleAuth({
5158
scopes: ["https://www.googleapis.com/auth/cloud-platform"],
5259
keyFile: this.options.vertexKeyFile,
5360
}),
5461
})
5562
} else {
56-
this.client = new AnthropicVertex({ projectId, region })
63+
this.client = new AnthropicVertex({ projectId, region, baseURL })
5764
}
5865
}
5966

0 commit comments

Comments
 (0)