Skip to content

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

Closed
huangjeff5 wants to merge 1 commit into
feat/py-openai-compatiblefrom
feat/py-openai-compatible-google-cloud
Closed

huangjeff5 wants to merge 1 commit into
feat/py-openai-compatiblefrom
feat/py-openai-compatible-google-cloud

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Related PRs: #151 (Anthropic(backend=GoogleCloud())), #187 → #188 (OpenAICompatible + backend=GoogleCloud()), then #185 (deprecate genkit-vertexai, merge last after #151, #187, and #188).

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 #187. Review and merge that first.

This is the replacement #185 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.
    • 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.
  • 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 #187. 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: 648 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 #187.

self,
*,
backend: GoogleCloud,
name: Literal['modelgarden'] | None = None,

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Should allow things other than modelgarden

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done — relaxed the backend: GoogleCloud overload to name: str | None = None, defaulting to 'modelgarden' when omitted while accepting any valid single-segment namespace (so multiple regions/projects can run side by side).

- `OpenAIConfig` is the config for every model. A server may ignore fields it
doesn't support, or reject them.

### Model Garden on Google Cloud

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Should explain OpenAiCompatible's main use case first and explain this after

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Restored OpenAICompatible's class docstring so OpenRouter + local keyless server come first and Model Garden follows after, and reframed this section's opening as a built-in backend on top of OpenAICompatible. Note: in the full README, ## Any OpenAI-compatible server (from base PR #187, lines 60–106) already precedes ### Model Garden on Google Cloud — it was just hidden in #188's stacked diff.



@dataclass(frozen=True, kw_only=True)
class GoogleCloud:

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Is this shared with Anthropic plugin? Look for a way to share this across plugins

@huangjeff5 huangjeff5 Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Keeping GoogleCloud and the ADC helpers local to genkit-openai rather than expanding genkit core exports — only genkit-openai and genkit-anthropic need this shape.

@huangjeff5

Copy link
Copy Markdown
Collaborator Author

Superseded by #197 (re-created to place into the single linear stack #151 → #196 → #197 → #198).

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