Skip to content

feat(py): keyless GoogleAI init, context.secrets on embedders, and ErrorReason.INVALID_CREDENTIALS - #49

Merged
huangjeff5 merged 11 commits into
mainfrom
fix/py-1.0-pr4-googleai-byok
Nov 12, 2026
Merged

huangjeff5 merged 11 commits into
mainfrom
fix/py-1.0-pr4-googleai-byok

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

A BYOK gateway (one server, each restaurant brings its own Gemini key) couldn't start: GoogleAI() raised ValueError at construction when no env key was set. It now starts keyless and bills each call to the tenant's context.secrets.api_key. ai.embed() also takes context=, so embeddings bill the tenant too. A missing key is no longer silent, and a rejected key carries ErrorReason.INVALID_CREDENTIALS so app code can match on it.

# 1. Boot the gateway without a server key
ai = Genkit(plugins=[GoogleAI()])
# stderr:
# => No Gemini API key found. Set GEMINI_API_KEY (or GOOGLE_API_KEY), pass GoogleAI(api_key=...),
#    or send context={'secrets': {'api_key': ...}} per request. Get a key: https://aistudio.google.com/apikey

# 2. Each request carries the restaurant's own key
response = await ai.generate(
    model=GoogleAI.gemini('gemini-flash-latest'),
    prompt='Suggest a dish without peanuts.',
    context={'secrets': {'api_key': tenant_key}},
)
print(response.text)
# => Mushroom Risotto

# 3. Embeddings bill the same tenant
await ai.embed(
    embedder='googleai/gemini-embedding-001',
    content='Mushroom Risotto',
    context={'secrets': {'api_key': tenant_key}},
)

# 4. The tenant's key is revoked: match on the reason, not the message
if response.error and response.error.reason is ErrorReason.INVALID_CREDENTIALS:
    await notify_tenant('Your Gemini key was rejected. Update it in settings.')
response.error.status, response.error.reason
# => ('UNAUTHENTICATED', <ErrorReason.INVALID_CREDENTIALS: 'INVALID_CREDENTIALS'>)

# 5. No server key and no tenant key: nothing is sent
response.error.status, response.error.reason
# => ('FAILED_PRECONDITION', None)

Main Changes

  • A missing key fails the call, not the constructor: Previously GoogleAI() raised ValueError when GEMINI_API_KEY was unset. Now the plugin loads, skips model listing, and builds the client per call. A call with no plugin key, no credentials, and no tenant key fails with FAILED_PRECONDITION. generate returns it on response.error; embed and generate_operation raise GenkitError.
  • A missing key is visible: With no key from any source, GoogleAI() logs one warning at construction with the setup steps. Before, the README quickstart with no key printed an empty line and exited 0 with nothing on stderr. The request-time message is now one line; it used to start with \n and span several lines, which looked broken in str() and tracebacks.
  • Rejected credentials carry ErrorReason.INVALID_CREDENTIALS: provider_error(...) takes reason=, stored in details['reason'], so it survives boxing (response.error.reason) and shows on raised errors (GenkitError.reason). Gemini sets it for a 400 with ErrorInfo.reason = API_KEY_INVALID or any 401; Vertex for a non-transient RefreshError. Status stays UNAUTHENTICATED. Deep Research, Antigravity, and Lyria returned INVALID_ARGUMENT for a bad key; they now match generate.
  • Env lookup reads three names: GEMINI_API_KEY, then GOOGLE_API_KEY, then GOOGLE_GENAI_API_KEY. Previously only GEMINI_API_KEY was read, so a key exported under GOOGLE_API_KEY (the name the google-genai SDK uses) failed startup.
  • Other app secrets no longer block the call, while explicit None/blank api_key fails closed: Previously any context.secrets without api_key raised INVALID_ARGUMENT, so an app couldn't keep, say, a CRM token there. Omitting api_key now falls back to the plugin key, while an explicit None or blank api_key (such as request.headers.get('x-gemini-key') when the header is missing) raises INVALID_ARGUMENT instead of billing the server key, across genkit-google-genai, genkit-openai, and genkit-anthropic.
  • embed and embed_many accept context=: When omitted, the embedder sees the enclosing flow's context, same as generate. GoogleAI, VertexAI (express mode), and OpenAI embedders read context.secrets.api_key.

Decisions

  • reason is the cross-plugin contract in Python. App code matches on ErrorReason, not provider status codes or message text. Gemini and Vertex go first; the other model plugins follow in Map provider credential failures to ErrorReason.INVALID_CREDENTIALS in all model plugins #101.
  • Only INVALID_CREDENTIALS, no missing-credentials reason. A missing key stays FAILED_PRECONDITION with no reason. The warning and the message already say what to do.
  • The warning uses stdlib logging, not genkit.get_logger. structlog prints to stdout and would mix into program output, and GoogleAI() runs before Genkit(), outside any trace. Stdlib logging falls back to stderr and follows the app's logging config if it has one.
  • No reason on a 403. Gemini also sends 403 for a blocked or disabled service.

Breaking changes

None.

Cross-SDK

  • Keyless startup needs an opt-in in JS, not in Python. JS googleAI() with no key throws when it defines models and embedders, unless you pass googleAI({ apiKey: false }). Python GoogleAI() starts with no opt-in, warns, and fails on the first call with FAILED_PRECONDITION. Python resolves the key on every call (context.secrets.api_key, then the plugin key), so a keyless plugin is the normal BYOK gateway setup.
  • Status for a keyless call with no request key differs. JS with apiKey: false returns INVALID_ARGUMENT. Python returns FAILED_PRECONDITION, the status JS uses for a missing key without apiKey: false.
  • ErrorReason is Python-only. JS and Go branch on status; UNAUTHENTICATED is the same across SDKs.
  • Embedder keys go in context, not options. JS passes options: { apiKey }. Python uses context={'secrets': {'api_key': ...}}, which core telemetry already redacts, so the key never lands in span attributes.
  • The env lookup order is the same as JS getApiKeyFromEnvVar.

@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr4-googleai-byok branch from ef30008 to 673a6a0 Compare November 9, 2026 19:08
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr4-googleai-byok branch from 673a6a0 to 5db462d Compare November 9, 2026 19:54
@huangjeff5
huangjeff5 changed the base branch from main to fix/py-1.0-pr3-generation-loop November 9, 2026 19:54
@huangjeff5
huangjeff5 added this pull request to stack #59 November 9, 2026 19:58
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr4-googleai-byok branch from 5db462d to fa0c24a Compare November 9, 2026 20:02
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr4-googleai-byok branch from fa0c24a to a81192d Compare November 9, 2026 20:21
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr4-googleai-byok branch from a81192d to 616e6b0 Compare November 9, 2026 21:22
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr4-googleai-byok branch 2 times, most recently from 4ec7d77 to 3b16545 Compare November 9, 2026 21:51
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr4-googleai-byok branch from 3b16545 to 11af4df Compare November 9, 2026 22:03
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr4-googleai-byok branch from 11af4df to 774cfea Compare November 9, 2026 23:18
@huangjeff5 huangjeff5 changed the title fix(py): GoogleAI keyless init, env key chain, and context.secrets on embedders fix(py/google-genai): keyless GoogleAI init and per-request BYOK secret resolution Nov 10, 2026
@huangjeff5
huangjeff5 requested a review from hilariie November 10, 2026 04:06
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr4-googleai-byok branch from 774cfea to f6891e1 Compare November 10, 2026 07:44
@huangjeff5 huangjeff5 changed the title fix(py/google-genai): keyless GoogleAI init and per-request BYOK secret resolution fix(py): keyless GoogleAI init and context.secrets on embedders Nov 10, 2026
@huangjeff5
huangjeff5 removed this pull request from stack #59 November 10, 2026 18:16
@huangjeff5
huangjeff5 added this pull request to stack #75 November 10, 2026 18:16
@huangjeff5
huangjeff5 removed this pull request from stack #75 November 10, 2026 18:16
@huangjeff5
huangjeff5 added this pull request to stack #76 November 10, 2026 18:16
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr4-googleai-byok branch from f6891e1 to 609a844 Compare November 10, 2026 18:37
@huangjeff5
huangjeff5 removed this pull request from stack #88 November 11, 2026 06:22
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr4-googleai-byok branch from a677005 to feed839 Compare November 11, 2026 06:22
@huangjeff5
huangjeff5 added this pull request to stack #89 November 11, 2026 06:23
@huangjeff5 huangjeff5 changed the title fix(py): keyless GoogleAI init and context.secrets on embedders feat(py): keyless GoogleAI init, context.secrets on embedders, and ErrorReason.INVALID_CREDENTIALS Nov 11, 2026
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr4-googleai-byok branch from cefd754 to da79841 Compare November 11, 2026 17:31
@huangjeff5
huangjeff5 removed this pull request from stack #89 November 11, 2026 18:06
… embedders

PR4-05 (D6):
- GoogleAI reads GEMINI_API_KEY -> GOOGLE_API_KEY -> GOOGLE_GENAI_API_KEY and
  no longer raises in __init__ when no key is set.
- Each Gemini, Veo, and embedder call resolves the key as context.secrets.api_key,
  then the plugin key/credentials, else GenkitError(FAILED_PRECONDITION) with the
  setup help. The plugin client is only built when the plugin has auth, so a
  keyless plugin never hits genai.Client(api_key=None). The resolved key is
  always passed to genai.Client explicitly.
- init()/list_actions() skip model listing when there is no key and no
  credentials.
- context_api_key returns None when secrets has no api_key (falls back to the
  plugin key, like genkit-openai/anthropic).
- One missing-key error for the package: Interactions models
  (api_key_for_context) share the same status and text.

PR4-06 (google + core):
- Genkit.embed()/embed_many() take context=, passed to the embedder as
  ctx.context (omitted, the enclosing action's context applies).
- GoogleAI/VertexAI embedders build a tenant client from
  context.secrets.api_key, even when the plugin has its own key.
PR4-06: the embedder resolves its client the same way as models
(context.secrets key, then the plugin key, else FAILED_PRECONDITION), so
ai.embed(context=...) bills the tenant instead of the plugin key.
…plugin key

Dropping the 'secrets has no api_key' guard let '' and whitespace fall
through string_secret as None, so a tenant with a missing header ran on
the operator's key. Raise INVALID_ARGUMENT like genkit-openai and
genkit-anthropic; None and app-only secrets still fall back.
GoogleAI docstring said a keyless call raises; generate returns the
failure on response.error by default. Ollama README said embed() takes
no context. Drop internal PR tags from the BYOK test.
…e 0.12 migration guide

The guide covers 0.12 -> 0.13. The 1.0 notes go to the release notes.
@huangjeff5
huangjeff5 changed the base branch from fix/py-1.0-pr3d-prompt-loader-sse to main November 11, 2026 18:37
@huangjeff5
huangjeff5 force-pushed the fix/py-1.0-pr4-googleai-byok branch from da79841 to 1485113 Compare November 11, 2026 18:38
@huangjeff5
huangjeff5 added this pull request to stack #109 November 11, 2026 22:25
@huangjeff5
huangjeff5 merged commit 4cb64e4 into main Nov 12, 2026
20 checks passed
@huangjeff5
huangjeff5 deleted the fix/py-1.0-pr4-googleai-byok branch November 12, 2026 05:02
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