Repository navigation
Python: fix(prompt-template): pass positional helper arguments to kernel functions - #14664
Chen Yufeiyang (feiiiiii5) wants to merge 2 commits into
Conversation
…tions The helper computed the positional arguments from the template but only merged kwargs into KernelArguments, so positional values were dropped and defaults won. Map them to the function's template-facing parameters, skip kernel-injected parameters, and reject excess arguments.
There was a problem hiding this comment.
🟡 Changes recommended
The injected-name filter excludes valid prompt-function inputs, causing incorrect argument binding or rejected calls.
1 open issue
What changed in this PR
This PR enables positional arguments in Python template helpers so Jinja2 and Handlebars calls pass supplied values to kernel functions.
Changes:
- Maps positional arguments to declared parameters before applying keyword overrides.
- Skips kernel-injected parameters and rejects excess positional arguments.
- Adds regression tests for synchronous and asynchronous helpers, Handlebars context handling, and injected parameters.
| File | Description |
|---|---|
python/tests/unit/prompt_template/test_template_helper.py |
Adds positional-argument regression coverage. |
python/semantic_kernel/prompt_template/utils/template_function_helpers.py |
Maps and validates positional arguments in template helpers. |
Review effort: Balanced
| parameter_names = [ | ||
| parameter.name | ||
| for parameter in function.parameters | ||
| if parameter.name is not None and parameter.name not in _INJECTED_FUNCTION_PARAMETER_NAMES |
There was a problem hiding this comment.
Fixed in 26d8c9e: injected names are filtered only for method-backed functions. Prompt function inputs retain those names. The regression uses a real KernelFunctionFromPrompt with service/kernel/execution_settings/arguments, one and two positional inputs, and sync/async Jinja plus Handlebars. All 24 cases failed before the fix; the complete prompt_template suite now passes 258 tests.

Motivation and Context
The template helper created by
create_template_helper_from_functioncomputes the positional arguments passed by Jinja2 or Handlebars, but only merges keyword arguments intoKernelArguments. A call such as{{ p_greet('Ada', '?') }}therefore invokes the function with its defaults (Hello, world!) or fails with “parameter is required” even though the template supplied the values. ADR 0023 requires Handlebars kernel-function helpers to support positional arguments.Description
Positional arguments are mapped to the function's template-facing parameters in declaration order before invocation. Parameters injected by the kernel (
kernel,service,execution_settings,arguments) are skipped, and too many positional arguments raiseFunctionExecutionExceptioninstead of being silently ignored. Keyword arguments still override positional ones.Tests cover sync Jinja2, sync Handlebars (including the leading context argument), async Jinja2, too many arguments, and a function with a kernel-injected parameter. The five new cases fail on
483ffb4with the default values/no exception and pass on this branch.tests/unit/prompt_template,tests/unit/contents,tests/unit/template_engine, andtests/unit/textexit 0;ruff checkandruff format --checkare clean.Contribution Checklist