From 7d556093c996b788afe67467ae119256da8cd8ac Mon Sep 17 00:00:00 2001 From: VANDRANKI Date: Sun, 4 Oct 2026 16:21:26 -0400 Subject: [PATCH 1/2] Python: Fix Handlebars messages helper ignoring its argument --- .../utils/handlebars_system_helpers.py | 9 +++++--- .../test_handlebars_prompt_template.py | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/python/semantic_kernel/prompt_template/utils/handlebars_system_helpers.py b/python/semantic_kernel/prompt_template/utils/handlebars_system_helpers.py index ea60447f2cb9..74734b280a80 100644 --- a/python/semantic_kernel/prompt_template/utils/handlebars_system_helpers.py +++ b/python/semantic_kernel/prompt_template/utils/handlebars_system_helpers.py @@ -10,12 +10,15 @@ logger: logging.Logger = logging.getLogger(__name__) -def _messages(this, options, *args, **kwargs): +def _messages(this, *args, **kwargs): from semantic_kernel.contents.chat_history import ChatHistory - if not isinstance(this.context["chat_history"], ChatHistory): + # Use the argument passed to the helper, e.g. {{messages my_history}}. + # Fall back to the "chat_history" variable when no argument is given. + chat_history = args[0] if args else this.context.get("chat_history") + if not isinstance(chat_history, ChatHistory): return "" - return this.context["chat_history"].to_prompt() + return chat_history.to_prompt() def _message_to_prompt(this, *args, **kwargs): diff --git a/python/tests/unit/prompt_template/test_handlebars_prompt_template.py b/python/tests/unit/prompt_template/test_handlebars_prompt_template.py index 6efea53f50c7..8dcb6b96d246 100644 --- a/python/tests/unit/prompt_template/test_handlebars_prompt_template.py +++ b/python/tests/unit/prompt_template/test_handlebars_prompt_template.py @@ -479,3 +479,26 @@ async def test_safe_types_are_allowed(): assert "42" in result assert "true" in result + + +async def test_helpers_chat_history_messages_uses_argument(kernel: Kernel): + template = """{{messages history}}""" + target = create_handlebars_prompt_template(template, allow_dangerously_set_content=True) + chat_history = ChatHistory() + chat_history.add_user_message("User message") + rendered = await target.render(kernel, KernelArguments(history=chat_history)) + assert ( + rendered.strip() == """User message""" + ) + + +async def test_helpers_chat_history_messages_prefers_argument_over_chat_history_variable(kernel: Kernel): + template = """{{messages other}}""" + target = create_handlebars_prompt_template(template, allow_dangerously_set_content=True) + wrong = ChatHistory() + wrong.add_user_message("WRONG") + right = ChatHistory() + right.add_user_message("RIGHT") + rendered = await target.render(kernel, KernelArguments(chat_history=wrong, other=right)) + assert "RIGHT" in rendered + assert "WRONG" not in rendered From b5adc4a12337b45474210e0c577a930c9f9af72f Mon Sep 17 00:00:00 2001 From: VANDRANKI Date: Sun, 4 Oct 2026 16:40:20 -0400 Subject: [PATCH 2/2] test: cover bare {{messages}} fallback to the chat_history variable --- .../test_handlebars_prompt_template.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/python/tests/unit/prompt_template/test_handlebars_prompt_template.py b/python/tests/unit/prompt_template/test_handlebars_prompt_template.py index 8dcb6b96d246..fb255676fc35 100644 --- a/python/tests/unit/prompt_template/test_handlebars_prompt_template.py +++ b/python/tests/unit/prompt_template/test_handlebars_prompt_template.py @@ -502,3 +502,14 @@ async def test_helpers_chat_history_messages_prefers_argument_over_chat_history_ rendered = await target.render(kernel, KernelArguments(chat_history=wrong, other=right)) assert "RIGHT" in rendered assert "WRONG" not in rendered + + +async def test_helpers_chat_history_messages_without_argument_uses_chat_history_variable(kernel: Kernel): + template = """{{messages}}""" + target = create_handlebars_prompt_template(template, allow_dangerously_set_content=True) + chat_history = ChatHistory() + chat_history.add_user_message("User message") + rendered = await target.render(kernel, KernelArguments(chat_history=chat_history)) + assert ( + rendered.strip() == """User message""" + )