Repository navigation
Commit b4c5b83
authored
Python: Fix Handlebars messages helper ignoring its argument (#14537)
### 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
- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄1 parent 05b6569 commit b4c5b83
2 files changed
Lines changed: 40 additions & 3 deletions
File tree
- python
- semantic_kernel/prompt_template/utils
- tests/unit/prompt_template
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
17 | 20 | | |
18 | | - | |
| 21 | + | |
19 | 22 | | |
20 | 23 | | |
21 | 24 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
479 | 479 | | |
480 | 480 | | |
481 | 481 | | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
0 commit comments