← Back to PRs

#12877: fix: backfill empty toolName on toolResult messages for Gemini compatibility

by aldoeliacim open 2026-02-09 20:55 View on GitHub →
agents stale
## Problem Gemini strictly validates `function_response.name` and rejects requests with a 400 `INVALID_ARGUMENT` error when `toolName` is empty. Tool results can be persisted with missing/empty `toolName`, causing failures when falling back to Gemini models. ## Solution Add `backfillToolResultNames()` in `session-transcript-repair.ts` that: 1. Builds a map of toolCallId → name from assistant messages 2. Fills in empty/missing `toolName` on toolResult messages from the matching tool call 3. Falls back to `'unknown'` for orphaned results (no matching call found) Called during `sanitizeSessionHistory` before sending to any provider. ## Tests Added 4 test cases covering: - Backfill from matching tool call (empty string) - Backfill from matching tool call (undefined) - Fallback to 'unknown' for orphaned results - No-op when toolName already populated Fixes #12832 <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR adds a transcript repair step to backfill missing/empty `toolName` fields on `toolResult` messages to satisfy Gemini’s strict `function_response.name` validation. The new `backfillToolResultNames()` function scans assistant tool-call blocks to build an id→name map, then fills in blank/missing names on corresponding `toolResult` messages (falling back to `'unknown'` when no match exists). `sanitizeSessionHistory` is updated to run this backfill before provider-specific sanitizers (e.g., OpenAI reasoning block downgrade). Tests were added to cover the backfill behavior across empty/undefined names, orphaned results, and no-op cases. <h3>Confidence Score: 3/5</h3> - This PR is likely safe but has behavior changes that may affect non-Google providers. - Core change is small and well-tested, but the backfill is applied unconditionally to all transcripts and may change semantics by forcing `'unknown'` tool names, and it does not align with existing aborted/error turn handling in tool repair logic. - src/agents/session-transcript-repair.ts; src/agents/pi-embedded-runner/google.ts <!-- greptile_other_comments_section --> <!-- /greptile_comment -->

Most Similar PRs