← All Fix Guides
medium
✓ Verified: 2026-11-26 Gemini API v1beta / Gemini 4 Thinking
protocol google gemini-apiagygemini-cli
400 Function Call / Function Response Turn Alignment Error
400 INVALID_ARGUMENT MALFORMED_FUNCTION_CALL
Matching Symptoms
- • Multi-turn agent fails with `400 INVALID_ARGUMENT` stating function response turn must immediately follow function call turn.
- • Truncating conversation history mid-tool-loop leaves an orphaned `functionCall` or `functionResponse`.
- • Missing `thought_signature` when replaying thinking-enabled function call turns.
Root Cause
Gemini requires strict 1:1 pairing between model `functionCall` parts and the immediately following `functionResponse` parts (preserving `thought_signature` on thinking models). Naive history trimming breaks this invariant.
Step-by-Step Fix
- 1 Ensure every
functionCallturn is followed immediately by matchingfunctionResponseparts with identical function names/IDs. - 2 When compacting or sliding conversation history, always slice on complete user->model->tool->model boundaries.
- 3 Preserve raw model parts (including
thought_signature) verbatim when sending multi-turn history back to Gemini 4 models.
Copyable Commands
Run agycraft doctor to check MCP tool schema & transcript loop health (all)
npx agycraft-mcp doctor Verify Fix
Verify
npx agycraft-mcp doctor