Repository navigation
feat(py): serve Claude from Google Cloud with Anthropic(backend=GoogleCloud()) - #257
Merged
Merged
Conversation
This was referenced Nov 30, 2026
huangjeff5
force-pushed
the
feat/py-anthropic-google-cloud
branch
from
December 1, 2026 20:23
3407fdd to
0f85cfc
Compare
huangjeff5
force-pushed
the
feat/py-anthropic-google-cloud
branch
from
December 1, 2026 21:20
026ca2e to
c9ab258
Compare
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(...))runs Claude on Google Cloud using your project's billing and application default credentials. Model refs,AnthropicConfig, and.promptfiles stay identical to the direct Anthropic API, so switching backends is a one-line constructor change.Today, running Claude on Google Cloud requires
ModelGarden, which uses a separatemodelgarden/anthropic/...namespace, only accepts string refs, and ties.promptfiles to Vertex.ModelGardenalready delegates togenkit_anthropic.AnthropicModelvia Anthropic'sAsyncAnthropicVertexclient; this PR exposes that path directly onAnthropic.Changes
backend=parameter andGoogleCloudconfig:GoogleCloud(project=None, location=None)is a frozen, keyword-only dataclass exported fromgenkit_anthropic. Passingbackend=GoogleCloud(...)builds Anthropic'sAsyncAnthropicVertexclient instead ofAsyncAnthropic, which routes requests to:rawPredict, injectsanthropic_version, and refreshes Google tokens.project=, thenGCLOUD_PROJECT, thenGOOGLE_CLOUD_PROJECT, then the ADC project (loaded once in a worker thread on first generate). A missing project fails withFAILED_PRECONDITIONpointing toGoogleCloud(project=...)andGOOGLE_CLOUD_PROJECT.location=, thenGOOGLE_CLOUD_LOCATION, then'global'(projects with Claude enabled get global quota by default; regional endpoints require regional quota).ANTHROPIC_API_KEYnever falls back to ADC, and Anthropic'sANTHROPIC_VERTEX_PROJECT_ID/CLOUD_ML_REGIONenv vars are ignored. Passingapi_key=withGoogleCloudraisesValueError; passing a non-GoogleCloudbackend raisesTypeError(with a module-qualified hint if aGoogleCloudfrom another package is passed); passing acontext.secretskey fails withFAILED_PRECONDITIONbefore ADC lookup.FAILED_PRECONDITIONwith thegcloud auth application-default logincommand instead of surfacing asINTERNAL. Failed loads are not cached (so logging in works without restarting). TransientTransportErrorand retryableRefreshErrormap toUNAVAILABLEforRetrymiddleware. Ifaccess_tokenorcredentialsis passed inclient_options, ADC is skipped.claude-haiku-4-5), while the Anthropic API uses dated catalog IDs (claude-haiku-4-5-20251001). Each backend now formats its own model ID, fixing 404s for Haiku 4.5, Sonnet 4.5, and Opus 4.5 on Google Cloud (and fixing the same bug inModelGarden).client_optionsvalidation: Validated againstAsyncAnthropicVertexwhen usingGoogleCloud(regionandproject_idare reserved forGoogleCloud).task_budgetbeta header: Google Cloud omits default beta headers, and Vertex rejectstask_budgetwithout its header (400 output_config.task_budget: Extra inputs are not permitted). Settingtask_budgetnow addstask-budgets-2026-03-13on both backends unlessbetasis explicitly overridden.list_actionson Google Cloud: Vertex has no Models API, solist_actionsreturnsSUPPORTED_ANTHROPIC_MODELSwithout making a network call. Unlisted Claude IDs still resolve on demand.google-cloudextra: Addsanthropic[vertex]>=0.98.0,<1(which pulls ingoogle-auth[requests]). Ifgoogle-authorgoogle.auth.transport.requestsis missing,Anthropic(backend=GoogleCloud())raisesImportErrorwith theuv addcommand.gcloud, project/API/Model Garden setup, ADC login, Cloud Run IAM roles, and common errors.Decisions
GoogleCloud(...)instead ofAnthropic(...): Most callers use the Anthropic API directly, soAnthropic()shouldn't growgoogle_cloud,project, andlocationkwargs that don't apply to them. Grouping those fields onbackend=GoogleCloud(project=..., location=...)keeps them out of the default signature, avoids invalid combinations likeproject=...withoutgoogle_cloud=True, and leaves room for other backends (e.g.AmazonBedrock) without adding more top-level kwargs.anthropic/namespace on both backends:Anthropic.claude('claude-sonnet-5-5')resolves toanthropic/claude-sonnet-5-5whether billed to Anthropic or Google Cloud, so switching backends doesn't require rewriting refs or.promptfiles.Anthropicdoesn't expose aname=parameter yet (unlikeOpenAICompatible, which already lets you set a custom namespace), so an app registers oneAnthropicinstance for now; adding an optionalname=later is purely additive.backend=GoogleCloud()explicitly (no ADC fallback): Falling back to ADC whenANTHROPIC_API_KEYis missing would silently move billing to a Google Cloud project on a misconfigured environment.Backendinterface:Anthropic.__init__resolves the backend once into eitherAnthropicApiBackendorGoogleCloudBackend. Each backend owns client construction, auth, model ID formatting, model listing, and default betas, soAnthropicModelstays focused on request/response mapping withoutisinstancechecks on the SDK client.What differs on Google Cloud
Documented in the README's What's different on Google Cloud section, based on testing every
AnthropicConfigfield against 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 produce identical results throughModelGarden.google_cloud=True) revision; subsequent refactors did not change request shapes, URLs, or credentials and haven't been re-run live.packages/genkit-anthropic,packages/genkit-vertexai, andtests/version_bounds_guard_test.py— 538 passed, 5 skipped (live tests, noANTHROPIC_API_KEY).GoogleCloud()andGoogleCloud(project=...)returnFAILED_PRECONDITIONwith the login command (previously the second returnedINTERNAL).ruff,ty,pyrefly(0 errors),scripts/check_consistency.py,uv lock --check, andaddlicense -checkall pass (pyrightruns in CI).Breaking changes
None.
backendis optional and the direct-API path is unchanged.Follow-ups
OpenAICompatible(feat(py): add OpenAICompatible for any OpenAI-compatible server, matching JS and Go #258), addsOpenAICompatible(backend=GoogleCloud())(feat(py): serve Model Garden's OpenAI-compatible models via OpenAICompatible(backend=GoogleCloud()) #259) for Llama/Mistral/other Model Garden models, then deprecatesgenkit-vertexai(feat(py): deprecate genkit-vertexai; remove it in the next release #260) once both replacements are in place.anthropicplugin already documents Vertex throughOpts; JS will need a matching option.