Skip to content

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

Closed
huangjeff5 wants to merge 2 commits into
feat/py-openai-compatiblefrom
feat/py-openai-compatible-google-cloud
Closed

huangjeff5 wants to merge 2 commits into
feat/py-openai-compatiblefrom
feat/py-openai-compatible-google-cloud

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Stack (merge in order): #151 (Anthropic(backend=GoogleCloud())) → #196 (OpenAICompatible) → #197 (OpenAICompatible(backend=GoogleCloud())) → #198 (deprecate genkit-vertexai)

OpenAICompatible(backend=GoogleCloud(...)) serves Model Garden's OpenAI-compatible models (Llama, Mistral and others) from Google Cloud, billed to your project and authenticated with application default credentials. The backend builds the endpoint and handles the token, so there's no base_url, no key and no token code. garden.model(id) gives the ref for any of these models.

Stacked on #196 (and #151). Review and merge those first.

This is the replacement #198 needs for Llama and the other open models when genkit-vertexai is removed. Refs default to modelgarden/<publisher>/<model>, as they were in ModelGarden, so moving over is a constructor change only — and you can pass a custom name= when you want another namespace (for example, to 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. The ref helper gives the ref for any Model Garden model on the OpenAI-compatible endpoint
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. No gcloud login yet: the error names the fix
# => FAILED_PRECONDITION: OpenAICompatible(backend=GoogleCloud()) found no Google Cloud credentials:
#    run `gcloud auth application-default login` on your machine, ...

Changes

  • New backend= on OpenAICompatible, and a new exported GoogleCloud(project=None, location=None). The endpoint is https://{location}-aiplatform.googleapis.com/v1beta1/projects/{project}/locations/{location}/endpoints/openapi, the same one ModelGarden uses. global gets the unregioned host. base_url= overrides it, for example for Private Service Connect, and auth stays Google's.
    • Project: project=, then GCLOUD_PROJECT, then GOOGLE_CLOUD_PROJECT, then the credentials' project.
    • Location: location=, then GOOGLE_CLOUD_LOCATION, then us-central1 (ModelGarden's default; most open models are regional).
  • The name rule.
    • Without a backend: name is still required, as in JS and Go.
    • With GoogleCloud: name is optional and defaults to 'modelgarden'; any other valid single-segment name (non-empty, no '/') is accepted so multiple regions or projects can coexist in one app.
  • Auth. ADC loads and refreshes on the first call, in a thread, because it can hit the metadata server. After that, an async token function gives the SDK a valid token before each request, refreshing only when the token has expired.
    • Missing or stale login: FAILED_PRECONDITION with the gcloud command that fixes it.
    • No project: FAILED_PRECONDITION naming three ways to set one.
    • Transient network blips: TransportError and retryable RefreshError map to UNAVAILABLE (both on initial load and hourly refresh) so Retry middleware can retry them.
    • Retries: a failed load isn't cached, so the next call retries after you log in.
  • Per-request keys are refused with FAILED_PRECONDITION, before ADC is touched, because every call bills the project. api_key= together with GoogleCloud raises ValueError at setup, and client_options={'api_key': ...} raises TypeError at setup (GoogleCloudServer reserves api_key).
  • The Dev UI lists nothing for Model Garden. Its endpoint has no /models, so no request goes out. Any id resolves when called.
  • New google-cloud extra → google-auth[requests]>=2.14.1, the same bound as genkit-vertexai. Without it, the constructor raises ImportError with the uv add command.
  • README: a "Model Garden on Google Cloud" subsection after OpenAICompatible's main use case, with first-time setup, defaults, custom name=, and the Claude pointer (Anthropic(backend=GoogleCloud())).

Decisions

  • A backend on the generic plugin, not a new plugin: Model Garden's open models are an OpenAI-compatible server whose auth and endpoint we know. The backend fills in exactly those two things, and everything else (chat, embeddings, .model(), config) is the OpenAICompatible path from feat(py): add OpenAICompatible for any OpenAI-compatible server, matching JS and Go #196. This mirrors Anthropic(backend=GoogleCloud()).
  • Keep GoogleCloud local to each plugin, not exported from genkit core: Only genkit-openai and genkit-anthropic need this shape, so duplicating the small config + ADC module is cheaper than expanding genkit core's public API surface.
  • modelgarden default name, custom name= allowed: Llama users' refs don't change when leaving genkit-vertexai, while passing name= lets apps register multiple Google Cloud regions, projects, or PSC endpoints side by side.
  • us-central1 by default, not global: The open models are mostly regional, and ModelGarden defaulted to us-central1. This differs from genkit-anthropic's GoogleCloud, which defaults to global because every Claude model has global quota.
  • No built-in model list: The only existing list (genkit-vertexai's Llama 3.1 and 3.2 entries) is stale. Any id resolves, and a curated list can come later without an API change.
  • No lock around the first credential load: Two first calls racing both load equivalent credentials. An asyncio.Lock would bind to one event loop, and the plugin's clients are per-loop.

Verification

  • packages/genkit-openai: 1,068 passed, including openai_compatible_google_cloud_test.py covering custom names, multi-region/project coexistence, invalid names, endpoint URLs, ADC loading/caching/retry, token refresh/errors, and missing extra error.
  • Repo & static checks: ruff format --check --preview ., ruff check --preview ., ty check ., pyrefly check on changed files, scripts/check_consistency.py, and uv lock --check all pass.
  • Not run: a live call against Model Garden.

Breaking changes

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

@huangjeff5
huangjeff5 force-pushed the feat/py-openai-compatible branch from 7853f01 to 6c14b5a Compare November 30, 2026 21:40
@huangjeff5
huangjeff5 force-pushed the feat/py-openai-compatible-google-cloud branch from 88dd349 to 9a7d5b3 Compare November 30, 2026 21:40
@huangjeff5

Copy link
Copy Markdown
Collaborator Author

Superseded by #259 in a fresh clean stack off latest main (#257 → #258 → #259 → #260 → #261 [Draft]).

@huangjeff5 huangjeff5 closed this Nov 30, 2026
@huangjeff5
huangjeff5 deleted the feat/py-openai-compatible-google-cloud branch November 30, 2026 22:40
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