fix(mcp): Fix Gemini conversation format issue with MCP auto-execution (#16592)

When using MCP tools with require_approval='never' and Gemini models,
the follow-up call after tool execution was failing with:

  'Please ensure that function call turn comes immediately after a user
   turn or after a function response turn.'

This was caused by adding an empty assistant message between the user
message and function calls, which violates Gemini's conversation format
requirements.

Changes:
- Only add assistant message to follow-up input if it contains actual content
- Allow function calls to come directly after user messages (as Gemini requires)
- Add explanatory comments about Gemini's format requirements

This fix allows MCP auto-execution to work correctly with Gemini models
while maintaining compatibility with other models.

Fixes: #[issue-number-if-any]
This commit is contained in:
Dmitrii Tunikov 2025-11-14 07:20:50 +01:00 • committed by GitHub
parent eca226286a
commit a22b2b0a67
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -526,8 +526,10 @@ class LiteLLM_Proxy_MCP_Handler:
else:
assistant_message_content.append(content)
# Add assistant message with content and function calls
if assistant_message_content or function_calls:
# Add assistant message only if there's actual content (not empty)
# For example, gemini requires that function call turns come immediately after user turns,
# so we should not add empty assistant messages
if assistant_message_content:
follow_up_input.append(
{
"type": "message",
@ -536,9 +538,9 @@ class LiteLLM_Proxy_MCP_Handler:
}
)
# Add function calls after assistant message
for function_call in function_calls:
follow_up_input.append(function_call)
# Add function calls (these can come directly after user message for LLM)
for function_call in function_calls:
follow_up_input.append(function_call)
# Add tool results (function call outputs)
for tool_result in tool_results: