When using google LLM APIs, I frequently encounter...
# ask-ai
r
When using google LLM APIs, I frequently encounter below issue (though if I use LLama 70b model) without any changes to code it works fine google.genai.errors.ClientError: 400 Bad Request. {'message': '{\n "error": {\n "code": 400,\n "message": "Please ensure that function call turn comes immediately after a user turn or after a function response turn.",\n "status": "INVALID_ARGUMENT"\n }\n}\n', 'status': 'Bad Request'}
t
Thanks for reaching out, Mitesh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey, could you share a bit more about the sequence of API calls or interactions in your code? Like, what’s going on right before the function call that triggers this error? Understanding that will really help us figure out why the error pops up with Google LLM APIs but not with the LLama 70b model.
r
I won't be able to share the exact stack trace since it is part of an product logs. However I can say that this usually happens when if I speak sometimes out of turn and cutoff the agent Example: If the agent asks "What is it that today you want to work on - Economics, Research Area or something else?" - Now if I cutoff the agent in mid sentence saying "Economics". I need to call a tool to set few attributes based on the answer. In this scenario the tool calls get messed up and I get the error I shared earlier.
t
Thanks so much for sharing those extra details! Just to make sure I’m on the right track, does this error pop up when you’re using LiveKit’s Google Realtime plugin, or is it happening when you’re working directly with the Google LLM API? Knowing this will really help me figure out the best way to help you out. Sources: livekit-agents/livekit/agents/voice/agent_activity.py
r
large_language_model = google.LLM( model="gemini-2.5-flash-lite", temperature=0.4, tool_choice="auto", top_p=0.7, )
t
Thanks so much for sharing the model configuration! To get to the bottom of this, could you help me out by clarifying a few things? • Are you using the standard LLM integration or the Realtime model with
gemini-2.5-flash-lite
? • Which authentication method are you going with—API key or Vertex AI? • Are there any extra parameters like
thinking_config
or
automatic_function_calling_config
that you’re including that might be triggering the invalid argument error? • If you have it handy, could you share the exact error message or response body from the
INVALID_ARGUMENT
error? Sources: plugins/google/src/beta/gemini_tts.ts | Gemini Live API integration guide | LiveKit Docs | Gemini TTS integration guide | LiveKit Docs | livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti...
r
Standard LLM Integration, using google/gemini API key, no extra configuration added. Unfortunately I tried 2 or 3 times to reproduce the error but now it works. For now I only have google.genai.errors.ClientError: 400 Bad Request. {'message': '{\n "error": {\n "code": 400,\n "message": "Please ensure that function call turn comes immediately after a user turn or after a function response turn.",\n "status": "INVALID_ARGUMENT"\n }\n}\n', 'status': 'Bad Request'}
t
Hey there! This error pops up when the chat messages don’t follow the expected back-and-forth pattern between the user and the model, especially around function calls and their responses. The Google LLM API is pretty strict about how messages should be ordered: • A function call (role
model
) has to come right after a user message (role
user
). • A function call output (role
user
) should follow the function call to complete that interaction. Here’s how you can fix this: 1. Make sure every function call is immediately after a user message or a function response. 2. Consider using tools like LiveKit’s
ChatContext
to help manage and format your chat messages automatically—this can save you a lot of headaches! 3. If you’re building messages by hand, double-check that the last message before sending is from the user. For example, a good message flow looks like this:
Copy code
user → model (function call) → user (function response) → model (assistant reply)
If you want, I’m happy to walk you through it or share some examples to get this working smoothly in your setup. Just let me know! Sources: livekit-agents/livekit/agents/voice/agent_activity.py | plugins/google/src/llm.ts | Testing and evaluation | LiveKit Docs | LiveKit turn detector plugin | LiveKit Docs