Repository navigation
Python: fix(prompt-template): pass positional helper arguments to kernel functions - #14664
Chen Yufeiyang (feiiiiii5) wants to merge 5 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
| @pytest.mark.parametrize("input_name", ["service", "kernel", "execution_settings", "arguments"]) | ||
| @pytest.mark.parametrize("values", [("first",), ("first", "second")]) | ||
| @pytest.mark.parametrize("mode", ["jinja_sync", "jinja_async", "handlebars"]) | ||
| async def test_prompt_function_preserves_named_inputs_for_positional_binding(kernel, mocker, input_name, values, mode): |
There was a problem hiding this comment.
The new regression test requests the mocker fixture, but this repository does not install pytest-mock. All 24 parameterized cases fail during setup with fixture 'mocker' not found, both locally and in the current unit/coverage runs (for example https://github.com/microsoft/semantic-kernel/actions/runs/40984533020). Please use unittest.mock.patch.object or the existing monkeypatch fixture so these cases run without a new test dependency.
There was a problem hiding this comment.
Fixed in the latest push: the test now takes monkeypatch instead of mocker, creates the AsyncMock locally and installs it with monkeypatch.setattr(KernelFunctionFromPrompt, "invoke", invoke). The repo does not install pytest-mock, which is why the 24 parameterized cases failed at setup. pytest tests/unit/prompt_template/test_template_helper.py → 37 passed locally.
The repo does not install pytest-mock, so the new parameterized test errored at setup (fixture mocker not found) in the unit and coverage jobs. Use the built-in monkeypatch fixture with an AsyncMock instead; all 24 cases pass.

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