Skip to content

Python: Fix Handlebars messages helper ignoring its argument - #14537

Open
PRABHU KIRAN VANDRANKI (VANDRANKI) wants to merge 2 commits into
microsoft:mainfrom
VANDRANKI:fix/handlebars-messages-helper-arg
Open

PRABHU KIRAN VANDRANKI (VANDRANKI) wants to merge 2 commits into
microsoft:mainfrom
VANDRANKI:fix/handlebars-messages-helper-arg

Conversation

@VANDRANKI

Copy link
Copy Markdown

Motivation and Context

The Handlebars messages helper ignores its argument. {{messages my_history}} always reads chat_history from the context, so it raises KeyError when that variable is missing and renders the wrong history when both exist. The Jinja2 messages(history) helper uses its argument.

Fixes #14535

Description

_messages in handlebars_system_helpers.py now uses the first positional argument. If no argument is given it falls back to the chat_history context variable, so existing {{messages chat_history}} templates behave the same. The unused options parameter was removed. For a non-block helper pybars passes the first argument in that slot, which is why the argument was never visible. A bare {{messages}} used to raise TypeError and now works through the fallback.

Added two tests (argument with a different variable name, and argument preferred over a chat_history variable). Both fail on main. tests/unit/prompt_template, tests/unit/kernel and tests/unit/functions pass. ruff check clean on the touched files, mypy clean on the helper module.

Contribution Checklist

Copilot AI balanced review requested due to automatic review settings October 4, 2026 20:21
@semantic-kernel-automation semantic-kernel-automation Bot added the python Pull requests for the Python Semantic Kernel label Oct 4, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The newly introduced no-argument fallback lacks integration coverage.

Review effort: Balanced
Findings: 1 Low severity

Open (1)
What changed in this PR

Fixes the Handlebars messages helper to honor its argument while retaining context fallback behavior.

Changes:

  • Uses the first helper argument when provided.
  • Adds regression tests for argument selection.
File Description
handlebars_system_helpers.py Corrects chat-history resolution.
test_handlebars_prompt_template.py Tests explicit argument handling.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +484 to +492
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() == """<chat_history><message role="user"><text>User message</text></message></chat_history>"""
)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I added test_helpers_chat_history_messages_without_argument_uses_chat_history_variable in b5adc4a. It renders a bare {{messages}} with only chat_history in the arguments and checks the output, so the no-argument fallback branch is now covered. It passes. It would also pass on main, since bare {{messages}} worked before. It guards the fallback so this change cannot break it.

This branch was successfully deployed

1 active deployment
github-app-auth — b5adc4a1 Deployed Oct 4, 2026 by VANDRANKI via add_label #29240
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests for the Python Semantic Kernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: Handlebars messages helper ignores its argument and always reads chat_history from the context

2 participants