Skip to content

Fix Gemini INVALID_ARGUMENT crash after message trimming#796

Open
pbranchu wants to merge 2 commits intoRightNow-AI:mainfrom
pbranchu:fix/gemini-turn-sanitization
Open

Fix Gemini INVALID_ARGUMENT crash after message trimming#796
pbranchu wants to merge 2 commits intoRightNow-AI:mainfrom
pbranchu:fix/gemini-turn-sanitization

Conversation

@pbranchu
Copy link

Summary

  • Add sanitize_gemini_turns() function to enforce Gemini's strict turn-ordering constraints after message history trimming
  • Merges consecutive same-role turns, drops orphaned functionCall parts (no following functionResponse), drops orphaned functionResponse parts (no preceding functionCall), and removes empty turns
  • Add #[serde(default)] on GeminiContent.parts to handle missing parts in API responses
  • Fix two tests (test_thought_signature_not_echoed_without_metadata and test_gemini_3x_text_and_function_call_both_have_signatures) that were missing required ToolResult messages, which caused sanitization to strip orphaned function calls

Test plan

  • All 32 Gemini driver tests pass
  • Verify no INVALID_ARGUMENT errors from Gemini after long conversations with tool use

🤖 Generated with Claude Code

Philippe Branchu and others added 2 commits March 23, 2026 04:31
Add sanitize_gemini_turns() to enforce Gemini's strict turn-ordering
constraints after message history is trimmed. This merges consecutive
same-role turns, drops orphaned functionCall/functionResponse parts,
and removes empty turns. Also adds #[serde(default)] on GeminiContent.parts
and fixes two tests that were missing required ToolResult messages.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- test_sanitize_drops_orphaned_function_call
- test_sanitize_keeps_valid_function_call_response_pair
- test_sanitize_drops_orphaned_function_response
- test_sanitize_merges_consecutive_same_role
- test_sanitize_empty_input

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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