From 15b091fb622166d5f3b3407c7a3b09b9ce3fe915 Mon Sep 17 00:00:00 2001 From: Osamaali313 <86572800+Osamaali313@users.noreply.github.com> Date: Sat, 27 Jun 2026 18:18:34 +0300 Subject: [PATCH] Python: fix range handlebars helper crashing on consecutive invalid args _range (backing the {{range}} helper) dropped non-integer args with args.pop(index) while iterating the same list via enumerate(args). Removing an element shifts the remaining items left, so the arg right after a dropped one is skipped and never validated; that leftover string then reaches range(), raising TypeError. A single invalid arg works (existing test: range 'a' 5 -> [0,1,2,3,4]), but two consecutive invalid args (range 'a' 'b' 5) crash, contradicting the helper's drop-invalid-args behavior. Build the converted-args list without mutating during iteration, and also tolerate int(None) (TypeError) in addition to ValueError. All existing outputs are unchanged. Adds a regression test. --- .../utils/handlebars_system_helpers.py | 15 +++++++++------ .../test_handlebars_prompt_template.py | 1 + 2 files changed, 10 insertions(+), 6 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..6919b8c1a297 100644 --- a/python/semantic_kernel/prompt_template/utils/handlebars_system_helpers.py +++ b/python/semantic_kernel/prompt_template/utils/handlebars_system_helpers.py @@ -74,13 +74,16 @@ def _array(this, *args, **kwargs): def _range(this, *args, **kwargs): - args = list(args) - for index, arg in enumerate(args): - if not isinstance(arg, int): + converted = [] + for arg in args: + if isinstance(arg, int): + converted.append(arg) + else: try: - args[index] = int(arg) - except ValueError: - args.pop(index) + converted.append(int(arg)) + except (ValueError, TypeError): + continue + args = converted if len(args) == 1: return list(range(args[0])) if len(args) == 2: 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..2b120d438d78 100644 --- a/python/tests/unit/prompt_template/test_handlebars_prompt_template.py +++ b/python/tests/unit/prompt_template/test_handlebars_prompt_template.py @@ -140,6 +140,7 @@ async def test_it_renders_kernel_functions_arg_from_arguments(kernel: Kernel, de ("range", "0 5 2", "[0, 2, 4]"), ("range", "0 5 1 1", "[]"), ("range", "'a' 5", "[0, 1, 2, 3, 4]"), + ("range", "'a' 'b' 5", "[0, 1, 2, 3, 4]"), ("concat", "'test1' 'test2' 'test3'", "test1test2test3"), ("or", "true false", "true"), ("add", "1 2", "3.0"),