-
Notifications
You must be signed in to change notification settings - Fork 3
fix(google-vertex): run anthropic on vertex #125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,13 @@ | ||
| import type { ModelAPI } from '../../api' | ||
| import { AnthropicAPI } from '../../api/anthropic' | ||
| import { GoogleAPI, type GoogleRequest } from '../../api/google' | ||
| import { DefaultProviderProxy } from '../default' | ||
| import { GoogleAPI } from '../../api/google' | ||
| import { DefaultProviderProxy, type JsonData } from '../default' | ||
| import { authToken, getProjectId } from './auth' | ||
|
|
||
| export class GoogleVertexProvider extends DefaultProviderProxy { | ||
| protected usageField = 'usageMetadata' | ||
| flavor: 'default' | 'anthropic' = 'default' | ||
| shouldStream: boolean = false | ||
|
|
||
| url() { | ||
| if (this.providerProxy.baseUrl) { | ||
|
|
@@ -54,6 +55,15 @@ export class GoogleVertexProvider extends DefaultProviderProxy { | |
| * @param region - The region to replace in the path. | ||
| */ | ||
| private replacePath(projectId: string, region: string): null | string { | ||
| const pathWithoutQuery = this.restOfPath.split('?')[0] | ||
|
|
||
| // Handle Anthropic client format: /v1/messages | ||
| if (pathWithoutQuery === 'v1/messages' && this.requestModel) { | ||
| // Always use streamRawPredict for Anthropic on Vertex (it handles both streaming and non-streaming) | ||
| const action = 'streamRawPredict' | ||
| return `/v1/projects/${projectId}/locations/${region}/publishers/anthropic/models/${this.requestModel}:${action}` | ||
| } | ||
|
|
||
| // Regex with capture groups: version (optional), publisher (optional), model | ||
| // Path may or may not start with / and may or may not have version | ||
| const regex = | ||
|
|
@@ -78,12 +88,31 @@ export class GoogleVertexProvider extends DefaultProviderProxy { | |
|
|
||
| async prepRequest() { | ||
| const requestBodyText = await this.request.text() | ||
| let requestBodyData: GoogleRequest | ||
| let requestBodyData: JsonData | ||
| try { | ||
| requestBodyData = JSON.parse(requestBodyText) | ||
| } catch (_error) { | ||
| return { error: 'invalid request JSON' } | ||
| } | ||
|
|
||
| const pathWithoutQuery = this.restOfPath.split('?')[0] | ||
| if (pathWithoutQuery === 'v1/messages') { | ||
| this.flavor = 'anthropic' | ||
| if (!('model' in requestBodyData)) { | ||
| return { error: 'model not found in Anthropic request body' } | ||
| } | ||
| const model = requestBodyData.model as string | ||
| this.requestModel = model | ||
|
|
||
| // Remove the model from the request body since Google Vertex doesn't expect it | ||
| delete requestBodyData.model | ||
|
|
||
| // Update requestBodyText without the model field | ||
| const updatedRequestBodyText = JSON.stringify(requestBodyData) | ||
|
|
||
| return { requestBodyText: updatedRequestBodyText, requestBodyData, requestModel: model } | ||
| } | ||
|
Comment on lines
+98
to
+114
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we okay with this level of intrusiveness? |
||
|
|
||
| const m = /\/models\/(.+?):/.exec(this.restOfPath) | ||
| if (m) { | ||
| return { requestBodyText, requestBodyData, requestModel: m[1] } | ||
|
|
@@ -92,6 +121,15 @@ export class GoogleVertexProvider extends DefaultProviderProxy { | |
| } | ||
| } | ||
|
|
||
| protected isStreaming(responseHeaders: Headers, requestBodyData: object): boolean { | ||
| if (this.flavor === 'anthropic') { | ||
| this.shouldStream = !!('stream' in requestBodyData && requestBodyData.stream === true) | ||
| } else { | ||
| this.shouldStream = super.isStreaming(responseHeaders, requestBodyData) | ||
| } | ||
| return this.shouldStream | ||
| } | ||
|
|
||
| async requestHeaders(headers: Headers): Promise<void> { | ||
| const token = await authToken(this.providerProxy.credentials, this.options.kv, this.options.subFetch) | ||
| headers.set('Authorization', `Bearer ${token}`) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
proxy-vcr/proxy_vcr/cassettes/google-vertex-google-vertex-anthropic-client.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| interactions: | ||
| - request: | ||
| body: '{"max_tokens":1024,"messages":[{"role":"user","content":"What is the capital | ||
| of Brazil?"}],"anthropic_version":"vertex-2023-10-16"}' | ||
| headers: | ||
| accept: | ||
| - '*/*' | ||
| accept-encoding: | ||
| - gzip, deflate | ||
| anthropic-version: | ||
| - vertex-2023-10-16 | ||
| connection: | ||
| - keep-alive | ||
| content-length: | ||
| - '131' | ||
| host: | ||
| - aiplatform.googleapis.com | ||
| user-agent: | ||
| - python-httpx/0.28.1 | ||
| method: POST | ||
| uri: https://aiplatform.googleapis.com/v1/projects/pydantic-ai/locations/global/publishers/anthropic/models/claude-sonnet-4:streamRawPredict | ||
| response: | ||
| body: | ||
| string: !!binary | | ||
| H4sIAAAAAAAC/2SQTU4DMQyFr2Jlw2aKZlD5myVCFSC6KewQGqUZ01qkzjR2KKXqkTgFFyNT0QrE | ||
| ytb7np+StzGL0KI3tXHephYHEphRB8PBSXlyWp5WQ1MYajNfyKx5i/relNXNcjwb3T9WMr5+oPNJ | ||
| 9zC+0JHLRl132FtRxM4wCzH4XrAiJGpZs+QCK+atftrs/YrvPdmN2jzOEZztSK2H8AJX0X6QB5J+ | ||
| k69PT/YYbhWm6OwCQXt3SKxxfSSHO2KoLs/KAiJ23jriGUwoQItwZxkphgIst7CyAtKhoxdy1vs1 | ||
| TBN5BQ0gGN8QMu7zGVeHZEe6Pjbb58KIhq6JaHNj+dXIbaMpsvkBgsuE7PL3OHlfmLRrpN4Y4i5p | ||
| o+EVWUxdDXMj1s2xcTlKKXDz11DuecbtfxaS/laG5Xb7DQAA//8DAC9RKlfTAQAA | ||
| headers: | ||
| Alt-Svc: | ||
| - h3=":443"; ma=2592000,h3-29=":443"; ma=2592000 | ||
| Content-Encoding: | ||
| - gzip | ||
| Content-Type: | ||
| - application/json | ||
| Transfer-Encoding: | ||
| - chunked | ||
| Vary: | ||
| - Origin | ||
| - X-Origin | ||
| - Referer | ||
| X-Content-Type-Options: | ||
| - nosniff | ||
| X-Frame-Options: | ||
| - SAMEORIGIN | ||
| X-XSS-Protection: | ||
| - '0' | ||
| date: | ||
| - Thu, 06 Nov 2025 12:34:32 GMT | ||
| request-id: | ||
| - req_vrtx_011CUreHywvBewqNvth7FFMK | ||
| server: | ||
| - hypercorn-h11 | ||
| x-vertex-ai-cache-creation-input-tokens: | ||
| - '0' | ||
| x-vertex-ai-cache-read-input-tokens: | ||
| - '0' | ||
| x-vertex-ai-input-tokens: | ||
| - '14' | ||
| x-vertex-ai-output-tokens: | ||
| - '40' | ||
| x-vertex-ai-received-request-id: | ||
| - 65cdc8b3-f380-49b1-953b-85431149ff5c | ||
| status: | ||
| code: 200 | ||
| message: OK | ||
| version: 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem to cause any issue. I just put it up.