Skip to content

feat(py): share GoogleCloud backend config and ADC helpers in genkit.plugin_api - #195

Closed
huangjeff5 wants to merge 2 commits into
mainfrom
feat/py-google-cloud-backend
Closed

huangjeff5 wants to merge 2 commits into
mainfrom
feat/py-google-cloud-backend

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Adds a shared GoogleCloud(project=None, location=None) backend config and shared Google Cloud ADC/resolution helpers to genkit.plugin_api, so plugins that support Google Cloud backends (Anthropic(backend=GoogleCloud()) in #151, OpenAICompatible(backend=GoogleCloud()) in #188) share one class and one credential-loading path while keeping top-level genkit focused on core primitives.

from genkit import Genkit
from genkit_anthropic import Anthropic, GoogleCloud
from genkit_openai import OpenAICompatible

# Both plugins re-export genkit.plugin_api.GoogleCloud, so one instance works across both
gcp = GoogleCloud(project='bistro-prod')
ai = Genkit(plugins=[
    Anthropic(backend=gcp),
    OpenAICompatible(backend=gcp),
])

Changes

  • Shared GoogleCloud dataclass in genkit.plugin_api (packages/genkit/src/genkit/_core/_google_cloud.py, exported from genkit.plugin_api):
    • Frozen, keyword-only GoogleCloud(project: str | None = None, location: str | None = None).
    • Zero new dependencies in genkit core; google-auth stays optional and is supplied by each plugin's [google-cloud] extra.
    • Plugins re-export genkit.plugin_api.GoogleCloud so genkit_openai.GoogleCloud is genkit_anthropic.GoogleCloud (avoiding import-shadowing TypeErrors when both plugins are used in one app).
  • Shared plugin-author helpers in genkit.plugin_api:
    • resolve_google_cloud_project(config) — checks config.project, then GCLOUD_PROJECT, then GOOGLE_CLOUD_PROJECT, else None (ADC fallback).
    • resolve_google_cloud_location(config, *, default) — checks config.location, then GOOGLE_CLOUD_LOCATION, then the plugin's own default ('global' for Claude, 'us-central1' for Model Garden open models).
    • require_google_auth(*, caller, extra) — fails fast at plugin construction with ImportError naming the uv add '<extra>' command when google-auth isn't installed.
    • load_google_adc(*, caller, scopes=...) — loads and refreshes ADC with cloud-platform scope, mapping missing or stale credentials to GenkitError(status='FAILED_PRECONDITION') with the gcloud auth application-default login fix.
    • GoogleAdcToken(credentials, *, caller) — async bearer-token callable that refreshes expired credentials in a thread and maps refresh failures to FAILED_PRECONDITION.
  • Deptry config (packages/genkit/pyproject.toml): ignores lazy optional google import (DEP001 / DEP003).
  • Unit tests (packages/genkit/tests/genkit/core/google_cloud_test.py): self-contained tests covering export boundaries (genkit.plugin_api only, not top-level genkit), dataclass immutability, project/location precedence, missing extra error, ADC load/refresh error mapping, and GoogleAdcToken caching/refresh (runs without google-auth installed).

Verification

  • uv run --no-sync pytest packages/genkit/tests/genkit/core/google_cloud_test.py packages/genkit/tests/genkit/docs_exports_test.py packages/genkit/tests/genkit/core/public_types_runtime_test.py — 33 passed
  • uv run --no-sync ruff format --check --preview ., ruff check --preview ., ty check ., pyrefly check on changed files — passed
  • cd packages/genkit && uv run --no-sync deptry src — passed
  • scripts/check_consistency.py and uv lock --check — passed

Follow-ups

Breaking changes

None.

@huangjeff5 huangjeff5 changed the title feat(py): share GoogleCloud backend config and ADC helpers in genkit feat(py): share GoogleCloud backend config and ADC helpers in genkit.plugin_api Nov 30, 2026
@huangjeff5

Copy link
Copy Markdown
Collaborator Author

Closing: keeping GoogleCloud local to each plugin rather than adding extra exports to genkit core.

@huangjeff5 huangjeff5 closed this Nov 30, 2026
@huangjeff5
huangjeff5 deleted the feat/py-google-cloud-backend branch November 30, 2026 19:29
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