From f7246fc4211bdd41b161cf4aebfe3bec3c36d088 Mon Sep 17 00:00:00 2001 From: Cookie0103 <258013390+Cookie0103@users.noreply.github.com> Date: Tue, 3 Nov 2026 12:38:06 +0900 Subject: [PATCH] fix(memory): preserve text parts after multimodal user prompts --- evoagentx/memory/context_manager.py | 5 +++ tests/src/memory/test_context_manager.py | 39 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 tests/src/memory/test_context_manager.py diff --git a/evoagentx/memory/context_manager.py b/evoagentx/memory/context_manager.py index b23b11f2..cefdb26a 100644 --- a/evoagentx/memory/context_manager.py +++ b/evoagentx/memory/context_manager.py @@ -97,6 +97,11 @@ def add_user_prompt(self, user_prompt: Union[str, list]): else: self.context[-1]["content"] = last_msg + user_prompt + elif isinstance(self.context[-1]["content"], list): + self.context[-1]["content"] = [ + *self.context[-1]["content"], + {"type": "text", "text": user_prompt}, + ] else: self.context[-1]["content"] += "\n\n" + user_prompt diff --git a/tests/src/memory/test_context_manager.py b/tests/src/memory/test_context_manager.py new file mode 100644 index 00000000..fa05392a --- /dev/null +++ b/tests/src/memory/test_context_manager.py @@ -0,0 +1,39 @@ +"""Regression coverage for consecutive text and multimodal user prompts.""" + +from copy import deepcopy +from unittest.mock import Mock + +import pytest + +from evoagentx.memory.context_manager import ContextManager +from evoagentx.models import BaseLLM + +IMAGE = {"type": "image_url", "image_url": {"url": "https://example.com/image.png"}} +TEXT = {"type": "text", "text": "Describe this image."} + + +@pytest.mark.parametrize("native_tool_calling", [False, True]) +@pytest.mark.parametrize( + "first,second,expected", + [ + ("First", "Second", "First\n\nSecond"), + ("First", [IMAGE], [{"type": "text", "text": "First"}, IMAGE]), + ([IMAGE], [TEXT], [IMAGE, TEXT]), + ([IMAGE], TEXT["text"], [IMAGE, TEXT]), + ], +) +def test_consecutive_user_prompts_preserve_content_parts( + native_tool_calling, first, second, expected +): + """Text following an image remains one text part, never individual characters.""" + llm = Mock(spec=BaseLLM) + llm.supports_native_tool_calling.return_value = native_tool_calling + context = ContextManager(llm=llm) + first, second = deepcopy(first), deepcopy(second) + original_inputs = deepcopy((first, second)) + + context.add_user_prompt(first) + context.add_user_prompt(second) + + assert context.context == [{"role": "user", "content": expected}] + assert (first, second) == original_inputs