Repository navigation
fix(py): GoogleAI keyless init, env key chain, and context.secrets on embedders - #7141
huangjeff5 wants to merge 2 commits into
Conversation
… 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.
There was a problem hiding this comment.
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.
| def _client_for_context(self, context: dict[str, Any]) -> AsyncOpenAI: | ||
| key = context_api_key(context) |
There was a problem hiding this comment.
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 {}.
| 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: |
There was a problem hiding this comment.
| def _create_embedder_action( | ||
| name: str, | ||
| client_getter: Callable[[], genai.Client], | ||
| client_getter: Callable[[dict[str, Any]], genai.Client], |
| def _create_veo_background_model( | ||
| name: str, | ||
| client_getter: Callable[[], genai.Client], | ||
| client_getter: Callable[[dict[str, Any]], genai.Client], |
| 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) |
There was a problem hiding this comment.
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.
| 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) |
| 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) |
There was a problem hiding this comment.
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.
| 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) |
|
Closing: migrated to standalone repository genkit-ai/genkit-python (PR #49): genkit-ai/genkit-python#49 |
Allows keyless initialization of GoogleAI plugin and adds BYOK secret resolution for model and embedder calls.
Main Changes
GoogleAI()constructor no longer raises if no key is present in environment; deferred to call time.context.secrets.api_key, then plugin config, thenGEMINI_API_KEY -> GOOGLE_API_KEY -> GOOGLE_GENAI_API_KEY.GenkitError(FAILED_PRECONDITION)matching JS when no key is found at invocation time.contextparameter toembed()andembed_many()so embedders honor request secrets.