From a22b2b0a674c373610091cdb1cf5219f7069e24b Mon Sep 17 00:00:00 2001 From: Dmitrii Tunikov <106737620+dtunikov@users.noreply.github.com> Date: Fri, 14 Nov 2025 07:20:50 +0100 Subject: [PATCH] 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] --- litellm/responses/mcp/litellm_proxy_mcp_handler.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/litellm/responses/mcp/litellm_proxy_mcp_handler.py b/litellm/responses/mcp/litellm_proxy_mcp_handler.py index 7ad26e0a863..c7322cab092 100644 --- a/litellm/responses/mcp/litellm_proxy_mcp_handler.py +++ b/litellm/responses/mcp/litellm_proxy_mcp_handler.py @@ -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: