Repository navigation
fix(py/google-genai): URL validation, required tool_choice loop fix, metadata persistence - #7142
huangjeff5 wants to merge 9 commits into
Conversation
…on ModelResponse.custom
GeminiModel filled custom only from prompt_feedback, so google_search,
url_context and response_logprobs returned custom == {}. Copy
groundingMetadata, urlContextMetadata, citationMetadata, logprobsResult and
avgLogprobs from candidate 0 in both generate paths; streaming keeps the
latest value seen on any chunk.
Bugbash: PR4-01
…-in + function tools Gemini 3 rejects google_search, url_context, code_execution and file_search next to function declarations unless toolConfig.includeServerSideToolInvocations is set, and GEMINI_MANAGED_BODY_FIELDS blocked the extra= workaround. The README hero (google_search + @ai.tool()) failed with a 400. - Set the flag on the Gemini API when both tool kinds are present (Vertex AI has no such field; google-genai raises if it is set). - Round-trip the server-side toolCall/toolResponse parts as custom.geminiToolCall/geminiToolResponse (JS parity), with their thought signature. - Disable google-genai AFC whenever tools are sent; Genkit runs tools itself, and AFC logged a warning for every FunctionDeclaration. Bugbash: PR4-02
…0ing with output_schema 'required' maps to Gemini's ANY mode, which only allows a function call. With output_schema the plugin also sent responseMimeType=application/json, which Gemini rejects next to ANY. Without a schema, ANY stayed on every turn, so the loop never produced a final answer and ran to max_turns. - Relax 'required' to 'auto' once the last message carries tool responses: 'required' forces the first call, later turns may answer. - Skip JSON mode and the response schema on ANY turns; they apply on the answer turn. Bugbash: PR3-09
…ric server header PartConverter used the downloaded Content-Type first, so an S3/R2 object served as application/octet-stream overrode an explicit content_type='image/png' and Gemini answered 400. The caller's type now wins; the served type is used only when it is specific, with parameters such as '; charset=' stripped. Bugbash: PR4-03
- A data: URI with no ',' or a bad base64 payload raised a raw ValueError or binascii.Error; it is now INVALID_ARGUMENT. Non-base64 data: URIs are percent-decoded (RFC 2397). - file:// URIs, local paths (/, ./, ~, C:\) and long scheme-less strings (raw base64) were sent as FileData and echoed back in Gemini's 400. They are now INVALID_ARGUMENT without echoing the value. - A CDN that gzips despite Accept-Encoding: identity was rejected. gzip and deflate are now decompressed with a bounded zlib decoder, so the 20MB cap applies to the decompressed size and a small body can't expand past it in one step. br/zstd stay rejected. Bugbash: PR4-04
…project With no project, API key or ADC project, google-genai raised a bare ValueError on the first call (it broke the Veo step of google-genai-media). The plugin client now raises GenkitError(FAILED_PRECONDITION) naming VertexAI(project=...) and GOOGLE_CLOUD_PROJECT. Other ValueErrors pass through unchanged. Bugbash: PR4-07
…r context_cache - CONTEXT_CACHE_SUPPORTED_MODELS allowed only gemini-2.0-flash, gemini-2.0-flash-001 (both retired) and gemini-3-flash-preview, so caching failed client-side on gemini-flash-latest, gemini-pro-latest, gemini-3.8-flash and the other cache-capable models. Drop the allowlist; the API rejects models that can't cache. - GeminiConfig.context_cache was popped and never read. False now ignores metadata['cache'] markers for the call. - Restore the commented-out code_execution conflict check. Bugbash: PR4-08
GeminiConfig(timeout=5) and GoogleAI(timeout=5) sent X-Server-Timeout: 5, and the Gemini API answers 400 to anything under 10 instead of timing out. On the Gemini API the header is now floored at 10 while httpx keeps the exact local deadline, so a 5s timeout succeeds or fails with DEADLINE_EXCEEDED after 5s. Vertex AI accepts shorter values and is unchanged. Bugbash: PR4-09
…coalesce streamed text - PartConverter dropped thought_signature on text, inline_data, fileData, executable_code and code_execution_result parts in both directions, which breaks Gemini 3 multi-turn and compaction. It now round-trips on all of them; a signature-only part becomes empty reasoning (JS parity), and fileData in a response becomes media instead of empty text. - Language/Outcome enum members leaked into custom.executableCode and custom.codeExecutionResult; they are now the wire strings. - The streamed final message kept every fragment plus a trailing empty text part, so post-model guardrails saw split text. Adjacent text and reasoning fragments now merge; a fragment carrying a signature closes its block and keeps the signature. Chunks still stream unchanged. - Parts with no Gemini field (Part.data from a2ui replays) converted to an empty Part(), which Gemini rejects. They are skipped, as are messages left with no parts. Bugbash: PR4-10
There was a problem hiding this comment.
Code Review
This pull request introduces several enhancements to the genkit-google-genai package, primarily focusing on Gemini API compatibility, tool invocation handling, and media processing. Key changes include implementing a 10-second floor for the X-Server-Timeout header for the Gemini API, adding support for server-side tool invocations, and improving the robustness of media downloads by adding decompression support and stricter URI validation. Additionally, the PR includes comprehensive updates to the response handling to include candidate metadata like grounding and citations, and improves stream coalescing for reasoning fragments. I have kept the review comment regarding the use of getattr for avg_logprobs access, as it provides a valuable improvement for SDK version compatibility.
| if candidate.avg_logprobs is not None: | ||
| out['avgLogprobs'] = candidate.avg_logprobs |
There was a problem hiding this comment.
To ensure compatibility with different versions of the google-genai SDK, it is safer to use getattr when accessing the avg_logprobs attribute on the Candidate object. This prevents potential AttributeError exceptions if an older version of the SDK is installed.
| if candidate.avg_logprobs is not None: | |
| out['avgLogprobs'] = candidate.avg_logprobs | |
| avg_logprobs = getattr(candidate, 'avg_logprobs', None) | |
| if avg_logprobs is not None: | |
| out['avgLogprobs'] = avg_logprobs |
|
Closing: migrated to standalone repository genkit-ai/genkit-python (PR #50): genkit-ai/genkit-python#50 |
Fixes for
genkit-google-genaiplugin covering URL validation, tool choice looping, and metadata persistence.Main Changes
tool_choice="required"is combined withoutput_schema.ModelResponse.custom.