Repository navigation
feat(py): serve Claude from Google Cloud with Anthropic(backend=GoogleCloud()) - #151
Closed
huangjeff5 wants to merge 2 commits into
Closed
huangjeff5 wants to merge 2 commits into
huangjeff5 wants to merge 2 commits into
Conversation
This was referenced Nov 30, 2026
huangjeff5
force-pushed
the
exp/py-anthropic-google-cloud
branch
from
November 30, 2026 19:57
9e5a913 to
afc38ba
Compare
huangjeff5
marked this pull request as ready for review
November 30, 2026 19:59
This was referenced Nov 30, 2026
huangjeff5
force-pushed
the
exp/py-anthropic-google-cloud
branch
from
November 30, 2026 21:40
afc38ba to
9330299
Compare
This was referenced Nov 30, 2026
Collaborator
Author
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Anthropic(backend=GoogleCloud(...))serves Claude from Google Cloud, billed to your project and authenticated with application default credentials. Refs,AnthropicConfigand.promptfiles are the same as on the Anthropic API, so changing who pays for Claude means changing one constructor argument.Today, Claude billed to a Google Cloud project goes through
ModelGardeninstead. It has its own namespace (modelgarden/anthropic/claude-...), only takes string refs, and its.promptfiles only work with that billing setup. Under the hood,ModelGardenalready runs Claude throughgenkit_anthropic.AnthropicModelon Anthropic's ownAsyncAnthropicVertexclient. This PR uses that same path from theAnthropicplugin.Changes
backend=onAnthropic, and a new exportedGoogleCloud.GoogleCloud(project=None, location=None)is a frozen, keyword-only dataclass. Leavingbackendout keeps today's behavior. With aGoogleCloud, the plugin builds Anthropic'sAsyncAnthropicVertexinstead ofAsyncAnthropic. The SDK moves the model into the URL (:rawPredict), addsanthropic_versionto the body, and refreshes the Google token.project=, thenGCLOUD_PROJECT, thenGOOGLE_CLOUD_PROJECT, then the ADC project. The ADC lookup runs once, in a thread, on the first generate.location=, thenGOOGLE_CLOUD_LOCATION, thenglobal. Every project that enables Claude getsglobalquota; regional endpoints need regional quota.FAILED_PRECONDITIONnamingGoogleCloud(project=...)andGOOGLE_CLOUD_PROJECT.backend=switches backends. A missingANTHROPIC_API_KEYnever falls back to ADC. The SDK's ownANTHROPIC_VERTEX_PROJECT_IDandCLOUD_ML_REGIONare ignored.api_key=withGoogleCloudraisesValueError. Abackendthat isn't aGoogleCloudraisesTypeError.context.secretskey fails withFAILED_PRECONDITION, before the ADC lookup.FAILED_PRECONDITIONnaminggcloud auth application-default login. Without this, the SDK raises google-auth's error mid-request, and the caller seesINTERNAL: internal error.TransportErrorand retryableRefreshErrorblips map toUNAVAILABLEsoRetrymiddleware can retry them.access_tokenorcredentialsinclient_optionsis used as is, and ADC isn't loaded.claude-haiku-4-5, not the Anthropic API's pinnedclaude-haiku-4-5-20251001. Each backend now picks the id it sends. The Anthropic API sends the catalog's pinned id, and Google Cloud sends the name as given. Without this, Haiku 4.5, Sonnet 4.5 and Opus 4.5 would 404 on Google Cloud.ModelGardenhad the same bug, and now sends the dateless name too.client_optionsis checked against the client that's built. WithGoogleCloud, it takesAsyncAnthropicVertexoptions (access_token,credentials,timeout, ...), and the error names that class.regionandproject_idare reserved forGoogleCloud.task_budgetworks on Google Cloud. The Anthropic API gets the default beta headers; Google Cloud gets none. Vertex rejectstask_budgetwithout its header (400output_config.task_budget: Extra inputs are not permitted), so a settask_budgetbringstask-budgets-2026-03-13on every backend. An explicitbetaslist still replaces the defaults.list_actionsreturns the built-in catalog on Google Cloud. Vertex has no Models API, so the Dev UI listsSUPPORTED_ANTHROPIC_MODELSand makes no call. Any other Claude id still resolves on demand.google-cloudextra →anthropic[vertex], which brings google-auth. Without it,Anthropic(backend=GoogleCloud())raisesImportErroron the constructor line, with theuv addcommand.Decisions
GoogleCloud(...)object, not as more arguments onAnthropic(): Most people on this plugin call the Anthropic API, soAnthropic()'s arguments stay about that path (api_key,base_url).google_cloud=True, project=..., location=...directly onAnthropic(). That showed three Google Cloud arguments to every Anthropic API user. It also needed a runtime check forproject=withoutgoogle_cloud=True, which the Anthropic API would otherwise silently ignore.backend=GoogleCloud(project=..., location=...)keeps that config in one typed object that only Google Cloud users import, and makes the invalid combination impossible to write.AmazonBedrock(region=...)would work the same way, without touchingAnthropic()'s signature.Anthropic.claude('claude-sonnet-5-5')resolves toanthropic/claude-sonnet-5-5on both backends, the same rule docs: propose one GoogleGenAI plugin with an enterprise flag #138 uses for Gemini. The cost is one Claude backend per app, since both register underanthropic/.Anthropic.__init__picks the backend once (_backend_for). After that,AnthropicApiBackendandGoogleCloudBackendeach own their SDK client, who pays for a call (plugin key, per-request key, or ADC project), the model id sent, model listing, and default betas.AnthropicModelonly maps requests and responses, with noisinstancechecks on the client. AddingAmazonBedrock(region=...)would mean one new backend class, one branch in_backend_for, and no change togenerateorAnthropicModel.What differs on Google Cloud
This is documented for users in the README's What's different on Google Cloud section, based on sending every
AnthropicConfigfield to Vertex:output_constrained=Falseworks around it);service_tier/inference_geo, and no Files API, web fetch or code execution betas.Verification
claude-sonnet-5-5inglobal:effort,task_budget, staticlist_actions. The same cases give identical results throughModelGarden.google_cloud=True) revision. Thebackend=refactor and the backend split since then don't change requests, URLs or credentials, and haven't been re-run live.packages/genkit-anthropic417 passed, 5 skipped (live tests, noANTHROPIC_API_KEY);packages/genkit-vertexai53 passed._backend.py,_anthropic_api.pyand_google_cloud.pyare at 100% line coverage._backend_for, both backends,client_kwargsandload_adc), and reran the package tests. Every mutant fails at least one test. The tests for default betas and credentials go throughai.generate, against a fake HTTP API or a stubbed Vertex client.GoogleCloud()andGoogleCloud(project=...)returnFAILED_PRECONDITIONwith the login command. Before this change, the second one returnedINTERNAL.addlicense. New files carry the Apache header.Breaking changes
None.
backendis optional and the direct-API path is unchanged.Open questions
anthropicplugin already documents Vertex throughOpts. JS would need the same option.Follow-ups
Anthropic(backend=AmazonBedrock(...))against thisBackendseam.OpenAICompatible(feat(py): add OpenAICompatible for any OpenAI-compatible server, matching JS and Go #196), addsOpenAICompatible(backend=GoogleCloud())(feat(py): serve Model Garden's OpenAI-compatible models via OpenAICompatible(backend=GoogleCloud()) #197) for Llama/Mistral/other Model Garden models, then deprecatesgenkit-vertexai(feat(py): deprecate genkit-vertexai; remove it in the next release #198) once both replacements are in place.