Skip to content

Commit eed4e91

Browse files
committed
fix(dspy): return MCP text blocks even when the only one is empty
_convert_mcp_tool_result chose between text and non-text content with `tool_content or non_text_contents`. A single text block collapses to its string, so one empty block read as falsy and the result fell through to the image, while two empty blocks returned ['', '']. Key the choice on whether any text block exists, so text wins in both cases.
1 parent f1260ff commit eed4e91

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎dspy/utils/mcp.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def _convert_mcp_tool_result(
4848
if result_mode == "structured" and _field_is_set(call_tool_result, "structured_content", "structuredContent"):
4949
return _get_field(call_tool_result, "structured_content", "structuredContent")
5050

51-
return tool_content or non_text_contents
51+
return tool_content if text_contents else non_text_contents
5252

5353

5454
def convert_mcp_tool(

‎tests/utils/test_mcp.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,25 @@ def test_structured_result_mode_returns_every_json_value_exactly(field_style, st
6565
assert type(converted) is type(structured)
6666

6767

68+
@pytest.mark.extra
69+
@pytest.mark.parametrize("result_mode", ["text", "structured"])
70+
@pytest.mark.parametrize(
71+
("texts", "expected"),
72+
[
73+
pytest.param(["chart"], "chart", id="one-text"),
74+
pytest.param([""], "", id="one-empty-text"),
75+
pytest.param(["", ""], ["", ""], id="two-empty-texts"),
76+
],
77+
)
78+
def test_text_blocks_win_over_images_even_when_empty(result_mode, texts, expected):
79+
from mcp.types import CallToolResult, ImageContent, TextContent
80+
81+
image = ImageContent(type="image", data="iVBORw0KGgo=", mimeType="image/png")
82+
content = [TextContent(type="text", text=text) for text in texts] + [image]
83+
84+
assert _convert_mcp_tool_result(CallToolResult(content=content), result_mode=result_mode) == expected
85+
86+
6887
@pytest.mark.extra
6988
@pytest.mark.parametrize("field_style", ["camel", "snake"])
7089
def test_structured_result_mode_falls_back_when_field_is_absent(field_style):

0 commit comments

Comments
 (0)