Skip to content

feat(py): serve Model Garden's OpenAI-compatible models via OpenAICompatible(backend=GoogleCloud()) - #259

Merged
huangjeff5 merged 1 commit into
feat/py-openai-compatible-pluginfrom
feat/py-openai-google-cloud
Dec 1, 2026
Merged

huangjeff5 merged 1 commit into
feat/py-openai-compatible-pluginfrom
feat/py-openai-google-cloud

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Stack (merge in order): #257 (Anthropic(backend=GoogleCloud())) → #258 (OpenAICompatible) → #259 (OpenAICompatible(backend=GoogleCloud())) → #260 (deprecate genkit-vertexai)

OpenAICompatible(backend=GoogleCloud(...)) serves Model Garden's OpenAI-compatible models (Llama, Mistral, and others) on Google Cloud using your project's billing and application default credentials. The backend constructs the Vertex OpenAI endpoint and refreshes Google tokens automatically.

This provides the replacement #260 needs for open Model Garden models before genkit-vertexai is deprecated. Refs default to modelgarden/<publisher>/<model> (matching ModelGarden), so migrating only changes the plugin constructor. Passing a custom name= lets you run multiple regions or projects side by side.

# uv add 'genkit-openai[google-cloud]'
from genkit import Genkit
from genkit_openai import GoogleCloud, OpenAICompatible

# 1. Model Garden through your gcloud login
garden = OpenAICompatible(backend=GoogleCloud(project='bistro-prod', location='us-central1'))
ai = Genkit(plugins=[garden])

# 2. Pass any Model Garden OpenAI-compatible model ID
res = await ai.generate(model=garden.model('meta/llama-3.3-70b-instruct-maas'), prompt='Suggest a dish.')
print(res.text)
# => Grilled salmon with lemon and dill

# 3. Pass a custom name to run multiple regions or projects side by side
eu_garden = OpenAICompatible(name='garden-eu', backend=GoogleCloud(project='eu-prod', location='europe-west4'))
eu_garden.model('meta/llama-3.3-70b-instruct-maas')
# => ModelRef(name='garden-eu/meta/llama-3.3-70b-instruct-maas', ...)

# 4. Missing gcloud login returns FAILED_PRECONDITION with the login command
# => FAILED_PRECONDITION: OpenAICompatible(backend=GoogleCloud()) found no Google Cloud credentials:
#    run `gcloud auth application-default login` on your machine, ...

Changes

  • New backend= parameter and GoogleCloud config: GoogleCloud(project=None, location=None) builds https://{location}-aiplatform.googleapis.com/v1beta1/projects/{project}/locations/{location}/endpoints/openapi (unregioned host for global).
    • Project resolution: project=, GCLOUD_PROJECT, GOOGLE_CLOUD_PROJECT, then the ADC project. Location resolution: location=, GOOGLE_CLOUD_LOCATION, then 'us-central1'.
    • base_url= overrides the generated endpoint (e.g. for Private Service Connect) while keeping Google auth; when base_url is set, project is not required, and blank base_url values raise ValueError.
  • Default name='modelgarden' with GoogleCloud: name remains required when no backend is given. With GoogleCloud, it defaults to 'modelgarden' and accepts any valid single-segment name for multi-region/project setups.
  • ADC loading and token refresh: load_adc loads and refreshes credentials in a worker thread on first call; AdcToken serializes token refreshes under a threading.Lock and guards against empty/None tokens.
    • Missing or expired credentials return FAILED_PRECONDITION with the gcloud auth application-default login command; failed loads aren't cached.
    • Missing project returns FAILED_PRECONDITION pointing to GoogleCloud(project=...), GOOGLE_CLOUD_PROJECT, and gcloud config set project.
    • Transient TransportError and retryable RefreshError map to UNAVAILABLE (both on initial load and hourly refresh) for Retry middleware.
  • Auth and header guards: Rejects api_key= (ValueError), client_options={'api_key': ...} (TypeError), pinned Authorization in default_headers (ValueError), and foreign GoogleCloud instances (module-qualified TypeError) at setup. Clears ambient OPENAI_ORG_ID and OPENAI_PROJECT_ID. Per-request context.secrets fails with FAILED_PRECONDITION before touching ADC.
  • No /models listing: Model Garden's OpenAI endpoint has no /models route, so list_actions returns an empty list without making a request; any model ID resolves on demand.
  • New google-cloud extra: Adds google-auth[requests]>=2.14.1. If google-auth or google.auth.transport.requests is missing, construction raises ImportError with the uv add command.
  • README: Adds a "Model Garden on Google Cloud" section covering setup, defaults, base_url= override, custom name=, and a pointer to Anthropic(backend=GoogleCloud()) for Claude.

Decisions

  • Backend option on OpenAICompatible: Model Garden's open models are an OpenAI-compatible server with Google Cloud auth and URL formatting. Attaching GoogleCloud as a backend reuses the entire chat, embedding, config, and .model() path from feat(py): add OpenAICompatible for any OpenAI-compatible server, matching JS and Go #258 and mirrors Anthropic(backend=GoogleCloud()).
  • Plugin-local GoogleCloud class: Only genkit-openai and genkit-anthropic need this config. Keeping the small dataclass + ADC helper local avoids expanding genkit core's public API surface.
  • Default namespace 'modelgarden' with optional name=: Existing ModelGarden Llama/Mistral refs keep working unchanged, while custom name= supports multiple regions, projects, or PSC endpoints in one app.
  • Default location 'us-central1': Most open Model Garden models are regional and ModelGarden already defaulted to us-central1 (unlike genkit-anthropic's GoogleCloud, which defaults to 'global' because Claude has global quota).
  • No static model catalog: genkit-vertexai's old Llama 3.1/3.2 list is stale. Resolving any ID on demand works immediately, and a curated list can be added later without breaking changes.
  • threading.Lock in AdcToken: Token refresh runs in worker threads and clients are per-event-loop, so threading.Lock serializes concurrent refreshes without binding to a single asyncio loop.

Verification

  • Unit tests: packages/genkit-openai, packages/genkit-anthropic, packages/genkit-vertexai, and tests/version_bounds_guard_test.py — 1,648 passed, 5 skipped (live tests).
  • Static & repo checks: ruff, ty, pyrefly (0 errors), scripts/check_consistency.py, uv lock --check, and addlicense -check all pass (pyright runs in CI).
  • Not run: live call against Model Garden.

Breaking changes

None. backend is optional, and OpenAICompatible without it behaves as in #258.

@pavelgj pavelgj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@huangjeff5
huangjeff5 force-pushed the feat/py-openai-google-cloud branch from fb5bcd4 to 7e00071 Compare December 1, 2026 21:20
@huangjeff5
huangjeff5 force-pushed the feat/py-openai-google-cloud branch from 7e00071 to 327e7de Compare December 1, 2026 21:37
@huangjeff5
huangjeff5 enabled stacked auto-merge (squash) December 1, 2026 21:52
@huangjeff5
huangjeff5 merged commit 2e26285 into main Dec 1, 2026
21 checks passed
@huangjeff5
huangjeff5 deleted the feat/py-openai-google-cloud branch December 1, 2026 22:08
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.

2 participants