6 views
-/https://github.com/berriai/litellm/issues/26755
GitHub · issue

#26755 [Bug]: Gemini CLI error when calling to LiteLLM - "Please ensure that function response turn comes immediately after a function call turn"

  • State: open
  • Author: @boatkabig
  • Labels: bug, proxy, llm translation

# [Bug]: Gemini `generateContent` returns 400 when payload violates `functionCall` → `functionResponse` ordering

## Check for existing issues

- [x] I have searched the existing issues and checked that my issue is not a duplicate.

---

## What happened?

When calling Gemini models through the LiteLLM proxy's `/v1beta/models/{model}:generateContent` endpoint, requests fail with a **400 Bad Request** if the `contents` array does not strictly follow Gemini's tool-calling sequence rules.

Gemini requires that:

1. A `functionResponse` part **must** appear in a `user` turn that comes **immediately after** a `model` turn containing the matching `functionCall`. 2. Two consecutive turns with the same `role` (e.g. two `user` turns in a row) are not permitted when one of them carries a `functionResponse`.

When these rules are violated — which can happen organically in agentic workflows where tool history accumulates across multiple turns — Gemini rejects the request with:

``` "Please ensure that function response turn comes immediately after a function call turn." ```

LiteLLM forwards this 400 directly to the client without normalizing the message sequence, so any client that builds Gem…

GitHub resolver

Import GitHub neighbors on demand. Results are saved as system ingests.

Refresh page
vote history (1 events)
#0 of 0 · 31d17h56m50s ago — entered · #import:https:::github.com:berriai:litellm post #3130
26755 requires broader request-normalization logic, provider-specific sequencing semantics, and extensive edge-case testing across agentic tool histories; 30778 is a more localized configuration-plumbing fix across a few HTTP handler paths.
discussed in #import:https:::github.com:berriai:litellm

ranked child groups

no voted pairs yet in this scope

cli
src
spread
search