I am having below issue on inference LLM api raise...
# ask-ai
r
I am having below issue on inference LLM api raise APIStatusError( livekit.agents._exceptions.APIStatusError: provider: google model: gemini-2.5-flash, message: Error 400, Message: Please ensure that function call turn comes immediately after a user turn or after a function response turn., Status: INVALID_ARGUMENT, Details: []. (status_code=400, request_id=None, body=provider: google model: gemini-2.5-flash, message: Error 400, Message: Please ensure that function call turn comes immediately after a user turn or after a function response turn., Status: INVALID_ARGUMENT, Details: []., retryable=True)
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.
I'm here to help you understand why this error is happening and how we can fix it together. The Google Gemini model expects the conversation history to follow a very specific turn order—function calls need to come right after a user message or a function response. Let’s walk through some steps that should help clear this up: 1. Take a Look at Your Conversation History: Try using the
to_chat_ctx
function to see how your conversation history is formatted before it goes to Gemini. It’s important that tool response turns are correctly converted to the "user" role, as Gemini expects. 2. Double-Check the Turn Order: Make sure every function call is coming right after a user message or a function response. If you have back-to-back function calls without a user or tool response in between, that’s likely causing the issue. 3. Review the Last Turn: The last turn in your conversation should be a user message. The system usually adds a dummy user message (just a single period) if needed, but if you want to troubleshoot, you can temporarily disable this by setting
inject_dummy_user_message=False
. 4. Confirm Function Response Formatting: Make sure your tool responses are properly formatted with the role "tool" and include the call ID and function name. This helps Gemini understand the flow. 5. Try a Simple Test: Create a minimal example with a user message followed by a function call to verify the basic flow is working correctly. If you’re using the standard LiveKit integration with the
google.LLM
class, it should handle these details automatically. But if you hit any bumps or have more questions, please don’t hesitate to reach out—I’m here to support you every step of the way! Sources: Google Gemini LLM plugin guide | LiveKit docs | livekit-plugins/livekit-plugins-google/README.md