π The fast, Pythonic way to build MCP servers and clients.
fix: GoogleGenaiSamplingHandler leaks thought parts and gives unhelpful errors on empty responses (#3849)
Bill Eastond5a3d546622d
diff --git a/src/fastmcp/client/sampling/handlers/google_genai.py b/src/fastmcp/client/sampling/handlers/google_genai.pyindex de46cb1a4..614138722 100644--- a/src/fastmcp/client/sampling/handlers/google_genai.py+++ b/src/fastmcp/client/sampling/handlers/google_genai.py@@ -322,7 +322,18 @@ def _response_to_create_message_result("""Convert Google GenAI response to CreateMessageResult (no tools)."""if not (text := response.text):candidate = _get_candidate_from_response(response)- msg = f"No content in response: {candidate.finish_reason}"+ # Check if the response only contained thinking+ has_thoughts = (+ candidate.content+ and candidate.content.parts+ and all(getattr(p, "thought", False) for p in candidate.content.parts)+ )+ if has_thoughts:+ msg = (+ "Model returned only thinking/reasoning content with no response text."+ )+ else:+ msg = f"No content in response (finish_reason={candidate.finish_reason})"raise ValueError(msg)return CreateMessageResult(@@ -365,7 +376,7 @@ def _response_to_result_with_tools(if candidate.content and candidate.content.parts:for part in candidate.content.parts:# Note: Skip thought parts from thinking_config - not relevant for MCP responses- if part.text:+ if part.text and not part.thought:content.append(TextContent(type="text", text=part.text))elif part.function_call is not None:fc = part.function_call@@ -380,7 +391,9 @@ def _response_to_result_with_tools()if not content:- raise ValueError("No content in response from completion")+ finish = candidate.finish_reason if candidate else "unknown"+ msg = f"No content in response from completion (finish_reason={finish})"+ raise ValueError(msg)return CreateMessageResultWithTools(content=content,diff --git a/tests/client/sampling/handlers/test_google_genai_handler.py b/tests/client/sampling/handlers/test_google_genai_handler.pyindex 263f01fc5..d1b86d6c1 100644--- a/tests/client/sampling/handlers/test_google_genai_handler.py+++ b/tests/client/sampling/handlers/test_google_genai_handler.py@@ -460,3 +460,142 @@ def test_convert_tool_strips_titles():assert "title" not in schemaassert "title" not in schema["properties"]["query"]assert schema["properties"]["query"]["type"] == "string"+++# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ+# Tests for thought-part filtering and improved error messages+# (PR #3849)+# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ+++def test_thought_parts_filtered_on_tool_path():+ """Thought parts should be excluded; only the real text part should appear."""+ mock_candidate = MagicMock(spec=Candidate)+ mock_candidate.content = MagicMock()+ mock_candidate.content.parts = [+ Part(text="thinking about the problem...", thought=True),+ Part(text="Here is the real answer"),+ ]+ mock_candidate.finish_reason = "STOP"++ mock_response = MagicMock(spec=GenerateContentResponse)+ mock_response.candidates = [mock_candidate]++ result = _response_to_result_with_tools(mock_response, model="gemini-2.5-flash")++ assert len(result.content) == 1 # ty: ignore[invalid-argument-type]+ assert isinstance(result.content[0], TextContent) # ty: ignore[not-subscriptable]+ assert result.content[0].text == "Here is the real answer" # ty: ignore[not-subscriptable]+++def test_thought_only_response_on_tool_path_raises():+ """When the response contains ONLY thought parts the tool path should raise+ a ValueError whose message includes the finish_reason."""+ mock_candidate = MagicMock(spec=Candidate)+ mock_candidate.content = MagicMock()+ mock_candidate.content.parts = [+ Part(text="deep thinking...", thought=True),+ ]+ mock_candidate.finish_reason = "STOP"++ mock_response = MagicMock(spec=GenerateContentResponse)+ mock_response.candidates = [mock_candidate]++ with pytest.raises(ValueError, match="finish_reason=STOP"):+ _response_to_result_with_tools(mock_response, model="gemini-2.5-flash")+++def test_thought_only_response_on_non_tool_path_raises():+ """When the non-tool path receives only thoughts the error message should+ mention 'thinking/reasoning content'."""+ mock_candidate = MagicMock(spec=Candidate)+ mock_candidate.content = MagicMock()+ mock_candidate.content.parts = [+ Part(text="internal reasoning...", thought=True),+ ]+ mock_candidate.finish_reason = "STOP"++ mock_response = MagicMock(spec=GenerateContentResponse)+ mock_response.text = None # No real text available+ mock_response.candidates = [mock_candidate]++ with pytest.raises(ValueError, match="thinking/reasoning content"):+ _response_to_create_message_result(mock_response, model="gemini-2.5-flash")+++def test_safety_filtered_response_on_tool_path_raises():+ """A safety-filtered response (no parts) should raise with the finish_reason+ included in the error message."""+ mock_candidate = MagicMock(spec=Candidate)+ mock_candidate.content = MagicMock()+ mock_candidate.content.parts = [] # Empty parts after safety filtering+ mock_candidate.finish_reason = "SAFETY"++ mock_response = MagicMock(spec=GenerateContentResponse)+ mock_response.candidates = [mock_candidate]++ with pytest.raises(ValueError, match="finish_reason=SAFETY"):+ _response_to_result_with_tools(mock_response, model="gemini-2.5-flash")+++def test_safety_filtered_response_on_non_tool_path_raises():+ """A safety-filtered response on the non-tool path should include+ finish_reason in the error."""+ mock_candidate = MagicMock(spec=Candidate)+ mock_candidate.content = MagicMock()+ mock_candidate.content.parts = []+ mock_candidate.finish_reason = "SAFETY"++ mock_response = MagicMock(spec=GenerateContentResponse)+ mock_response.text = None+ mock_response.candidates = [mock_candidate]++ with pytest.raises(ValueError, match="finish_reason=SAFETY"):+ _response_to_create_message_result(mock_response, model="gemini-2.5-flash")+++def test_normal_response_text_and_function_call():+ """A normal response with both real text and a function call should+ produce both TextContent and ToolUseContent in the result."""+ mock_candidate = MagicMock(spec=Candidate)+ mock_candidate.content = MagicMock()+ mock_candidate.content.parts = [+ Part(text="Let me look that up."),+ Part(function_call=FunctionCall(name="lookup", args={"q": "test"})),+ ]+ mock_candidate.finish_reason = "STOP"++ mock_response = MagicMock(spec=GenerateContentResponse)+ mock_response.candidates = [mock_candidate]++ result = _response_to_result_with_tools(mock_response, model="gemini-2.5-flash")++ assert len(result.content) == 2 # ty: ignore[invalid-argument-type]+ assert isinstance(result.content[0], TextContent) # ty: ignore[not-subscriptable]+ assert result.content[0].text == "Let me look that up." # ty: ignore[not-subscriptable]+ assert isinstance(result.content[1], ToolUseContent) # ty: ignore[not-subscriptable]+ assert result.content[1].name == "lookup" # ty: ignore[not-subscriptable]+ assert result.content[1].input == {"q": "test"} # ty: ignore[not-subscriptable]+ assert result.stopReason == "toolUse"+++def test_thought_with_function_call_keeps_function_call():+ """When thinking parts accompany a function call, only the function call+ should appear in the content (thought parts filtered out)."""+ mock_candidate = MagicMock(spec=Candidate)+ mock_candidate.content = MagicMock()+ mock_candidate.content.parts = [+ Part(text="reasoning about what tool to use...", thought=True),+ Part(function_call=FunctionCall(name="get_weather", args={"city": "NYC"})),+ ]+ mock_candidate.finish_reason = "STOP"++ mock_response = MagicMock(spec=GenerateContentResponse)+ mock_response.candidates = [mock_candidate]++ result = _response_to_result_with_tools(mock_response, model="gemini-2.5-flash")++ assert len(result.content) == 1 # ty: ignore[invalid-argument-type]+ assert isinstance(result.content[0], ToolUseContent) # ty: ignore[not-subscriptable]+ assert result.content[0].name == "get_weather" # ty: ignore[not-subscriptable]+ assert result.stopReason == "toolUse"