gemini-cli - 💡(How to fix) Fix API Error 400: "function call turn comes immediately after a user turn" during tool execution

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Error Message

throwing a 400 'invalid turn' error during normal usage

RAW_BUFFERClick to expand / collapse

What happened?

logs

What did you expect to happen?

I expected the CLI to internally handle the conversation history and tool call sequences correctly so that it adheres to the Gemini API's strict role-alternation requirements, instead of throwing a 400 'invalid turn' error during normal usage

Client information

  • CLI Version: 0.41.2
  • Git Commit: b0c7a1722
  • Session ID: b2060626-0294-49e2-a2a2-006cc0fced42
  • Operating System: darwin v22.22.1
  • Sandbox Environment: no sandbox
  • Model Version: gemini-3.1-pro-preview-customtools
  • Auth Type: gemini-api-key
  • Memory Usage: 98.1 MB
  • Terminal Name: iTerm2 3.6.9
  • Terminal Background: #15191e
  • Kitty Keyboard Protocol: Supported

Login information

API KEY

Anything else we need to know?

Context for the maintainers: Looking at the logs, it seems the CLI generated a malformed message history where multiple function responses were sent consecutively (e.g., an orphaned function response with ID esrym5y5 right after another function response), violating the API's strict turn sequence rules.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING