#12877: fix: backfill empty toolName on toolResult messages for Gemini compatibility
agents
stale
Cluster:
Error Handling in Agent Tools
## 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
#15143: fix(transcript-repair): validate tool call id and name to prevent G...
by GreyC · 2026-02-13
86.7%
#12487: fix(agents): strip orphaned tool_result when tool_use is sanitized ...
by skylarkoo7 · 2026-02-09
82.3%
#15509: fix(session): drop tool_use blocks with empty or missing name
by aldoeliacim · 2026-02-13
80.1%
#19094: Fix empty tool_call_id and function names in provider transcript pa...
by yxshee · 2026-02-17
79.9%
#22011: fix(transcript): drop empty toolCallId toolResults during persisten...
by sauerdaniel · 2026-02-20
79.6%
#4844: fix(agents): skip error/aborted assistant messages in transcript re...
by lailoo · 2026-01-30
79.5%
#15767: Fix: Gemini REQUIRED_FIELD_MISSING on corrupted toolResults
by janhcla · 2026-02-13
79.3%
#4852: fix(agents): sanitize tool pairing after compaction and history tru...
by lailoo · 2026-01-30
78.0%
#21166: fix(agents): sanitize tool names in session transcript repair (#8595)
by dinakars777 · 2026-02-19
77.9%
#3647: fix: sanitize tool arguments in session history
by nhangen · 2026-01-29
77.3%