Skip to content

fix(py): GoogleAI keyless init, env key chain, and context.secrets on embedders - #7141

Closed
huangjeff5 wants to merge 2 commits into
mainfrom
fix/py-1.0-pr4-googleai-byok
Closed

huangjeff5 wants to merge 2 commits into
mainfrom
fix/py-1.0-pr4-googleai-byok

Conversation

@huangjeff5

Copy link
Copy Markdown
Contributor

Allows keyless initialization of GoogleAI plugin and adds BYOK secret resolution for model and embedder calls.

Main Changes

  • Keyless initialization: GoogleAI() constructor no longer raises if no key is present in environment; deferred to call time.
  • Context secret resolution: Resolves API keys from context.secrets.api_key, then plugin config, then GEMINI_API_KEY -> GOOGLE_API_KEY -> GOOGLE_GENAI_API_KEY.
  • FAILED_PRECONDITION on missing key: Throws GenkitError(FAILED_PRECONDITION) matching JS when no key is found at invocation time.
  • Embedder BYOK: Forwarded context parameter to embed() and embed_many() so embedders honor request secrets.

… 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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request enables per-request/tenant API keys for embedders in both the Google GenAI and OpenAI plugins by passing the execution context to embedder actions. It also refactors Google AI key resolution to support keyless initialization, fallback environment variables, and BYOK gateways, deferring missing key errors to call time. The review feedback suggests improving type safety and robustness by updating several client-resolution helper methods and callbacks to accept nullable contexts (dict[str, Any] | None) instead of strict dictionaries.

Comment on lines +457 to +458
def _client_for_context(self, context: dict[str, Any]) -> AsyncOpenAI:
key = context_api_key(context)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The context parameter can be None (e.g., when embed is called with context=None). To prevent potential AttributeError crashes in context_api_key, update the type signature to dict[str, Any] | None and safely handle None using context or {}.

Suggested change
def _client_for_context(self, context: dict[str, Any]) -> AsyncOpenAI:
key = context_api_key(context)
def _client_for_context(self, context: dict[str, Any] | None) -> AsyncOpenAI:
key = context_api_key(context or {})


def _embed_client(self) -> AsyncOpenAI:
"""The plugin's client for embedders, which run on the plugin's key only.
def _embed_client(self, context: dict[str, Any]) -> AsyncOpenAI:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Update the type signature of _embed_client to accept dict[str, Any] | None to align with _client_for_context and support nullable contexts.

Suggested change
def _embed_client(self, context: dict[str, Any]) -> AsyncOpenAI:
def _embed_client(self, context: dict[str, Any] | None) -> AsyncOpenAI:

def _create_embedder_action(
name: str,
client_getter: Callable[[], genai.Client],
client_getter: Callable[[dict[str, Any]], genai.Client],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Update the client_getter type signature to accept dict[str, Any] | None to support nullable contexts.

Suggested change
client_getter: Callable[[dict[str, Any]], genai.Client],
client_getter: Callable[[dict[str, Any] | None], genai.Client],

def _create_veo_background_model(
name: str,
client_getter: Callable[[], genai.Client],
client_getter: Callable[[dict[str, Any]], genai.Client],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Update the client_getter type signature to accept dict[str, Any] | None to support nullable contexts.

Suggested change
client_getter: Callable[[dict[str, Any]], genai.Client],
client_getter: Callable[[dict[str, Any] | None], genai.Client],

Comment on lines +775 to +779
def _call_client(self, context: dict[str, Any]) -> genai.Client:
return _call_client(self, context)

def _embed_client(self, context: dict[str, Any]) -> genai.Client:
return _call_client(self, context, prefer_secret=True)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Update the type signatures of _call_client and _embed_client to accept dict[str, Any] | None to align with the module-level _call_client function and support nullable contexts.

Suggested change
def _call_client(self, context: dict[str, Any]) -> genai.Client:
return _call_client(self, context)
def _embed_client(self, context: dict[str, Any]) -> genai.Client:
return _call_client(self, context, prefer_secret=True)
def _call_client(self, context: dict[str, Any] | None) -> genai.Client:
return _call_client(self, context)
def _embed_client(self, context: dict[str, Any] | None) -> genai.Client:
return _call_client(self, context, prefer_secret=True)

Comment on lines +1260 to +1264
def _call_client(self, context: dict[str, Any]) -> genai.Client:
return _call_client(self, context)

def _embed_client(self, context: dict[str, Any]) -> genai.Client:
return _call_client(self, context, prefer_secret=True)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Update the type signatures of _call_client and _embed_client to accept dict[str, Any] | None to align with the module-level _call_client function and support nullable contexts.

Suggested change
def _call_client(self, context: dict[str, Any]) -> genai.Client:
return _call_client(self, context)
def _embed_client(self, context: dict[str, Any]) -> genai.Client:
return _call_client(self, context, prefer_secret=True)
def _call_client(self, context: dict[str, Any] | None) -> genai.Client:
return _call_client(self, context)
def _embed_client(self, context: dict[str, Any] | None) -> genai.Client:
return _call_client(self, context, prefer_secret=True)

@huangjeff5

Copy link
Copy Markdown
Contributor Author

Closing: migrated to standalone repository genkit-ai/genkit-python (PR #49): genkit-ai/genkit-python#49

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