About the Gemini API `400 Please ensure that function call turns come immediately after a user turn or after a function response turn.` error

Hello, community and Google team!

I’ve encountered an issue with the Gemini API that I’ve been trying to resolve. I had previously determined the problem using a code fix, but the actual cause of the error differed from my fix before.

After further testing and research, I discovered that the error occurs when you provide two, four, six, or other even number of user turns. For instance:

user: Hi.
user: Please run the test function.
model: Ok. (RUN THE TEST FUNCTION)
<ERROR 400>

As we understand, the role of a Content Object should be either user, model, or function. However, the error message suggests Please ensure that function call turns come immediately after a user turn or after a function response turn. This implies the model shouldn’t call functions after a model turn.

So, why does the error occur when the previous turn is a role: "user" turn? I suspect that the Gemini API may not use the value of role to identify user and model contents. Instead, it uses the index of the content. Therefore, the first content would be user, the second would be model, and the third would be user again.

As a result, the Gemini API might interpret the previous example like this:

user: Hi.
model: Please run the test function.
user: Ok. (RUN THE TEST FUNCTION)
<ERROR 400>

Notice that the Gemini API doesn’t verify if the function is called by the model; it only checks the functionCall part.

This also explains why the model shouldn’t call functions after a model turn. It might be perceived as the user calling a function.

By the way, if you try to trigger this error in AI Studio, it won’t work because AI Studio automatically creates two user messages into one Content but in two Parts.

I believe this is a bug in the Gemini API that needs to be fixed. Alternatively, if the API is designed to use the content index instead of role, it should provide a clearer error message, such as User turn should be used at the start or immediately after a model turn.

As another proof, I got the following contents:

 [
  {
    "role": "user",
    "parts": [
      {
        "text": "hi."
      }
    ]
  },
  {
    "role": "user",
    "parts": [
      {
        "text": "Hello, nice to meet you."
      }
    ]
  },
  {
    "role": "user",
    "parts": [
      {
        "text": "Repeat the previous message you say."
      }
    ]
  },
  {
    "parts": [
      {
        "text": "Hello, nice to meet you.\n"
      }
    ],
    "role": "model"
  }
]

This error message has changed a number of times over the past year or so (and never making it much clearer). It has also been loosely enforced at times. But it boils down to this:

  • You must start with a “user” role
  • The last thing you send must be either a “user” or “function” role
  • The conversation must alternate between “model” and either (“user” or “function”) roles

And as @OrangiaNebula points out, below, you can just use user roles with “function response” parts.

See also Intro to Functions

Edited to correct role names

2 Likes

Correct, with the adjustment that functionResponse role is spelled function. The function role contains functionResponse in the message, which is a client-to-server message.

The original poster has a point about odd/even. When following the rule, message 0 (even) must be role “user”. Message 1 (odd) must be role “model”. In message 1 there might be a functionCall.
Message 2 (even) may optionally be role “function” if the client chooses to execute the tool named in the functionCall and this scenario is explored in this sample Intro to function calling with the Gemini API  |  Google AI for Developers (it used to say “role”: “function” last week, I had checked; it says “role”: “user” for the functionResponse this week).
The alternative scenario for message 2 is that the client chooses to not return a value to a functionCall. That scenario is explored in the “Extract structured data using function calling” part of the documentation (함수 호출을 사용하여 구조화된 데이터 추출  |  Gemini API  |  Google AI for Developers). In this scenario, the client exploits the functionCall structure and in message 2 sends a fresh, possibly unrelated prompt to the model, using role “user”.

So, as of this week, the documented approach is to strictly alternate between role “user” (even messages, we start counting at 0) and role “model” (odd messages). My test cases use role: “function” for the functionResponse and work, so the approach as of one week ago is still acceptable.

Hope that helps.

2 Likes

Also checked the current version of the cookbook at cookbook/quickstarts/rest/Function_calling_REST.ipynb at main · google-gemini/cookbook · GitHub
It shows (under the heading “Curl example that uses a response from a previous turn”) the even-numbered client-to-server message containing the functionResponse tagged with “role”: “function”, which is consistent with what the documentation was showing a week ago. Normally these two sources are supposed to be in sync.

2 Likes

I’m bumping this thread because this official documentation that you link still says incorrectly that the “role”: “user” -

This produces an error:

Content with role ‘user’ can’t contain ‘functionResponse’ part

I’m adding this information here, including the actual error text, so that if documentation won’t be fixed then at least the solution will be searchable so others will waste less time troubleshooting this.

1 Like