Repository navigation
Conversation
…ithout the $ prefix
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No unresolved review issues remain for the literal-dollar prefix change; the separate NamedArgBlock issue is scoped to linked PRs.
No open issues 🎉
What changed in this PR
This PR restores VarBlock’s documented syntax by requiring a literal $ prefix, preventing malformed contents from silently referencing unrelated kernel arguments. The separate NamedArgBlock issue remains scoped to the linked PRs.
Changes:
- Replace
[${1}]with[$]in the variable-block regex. - Add regression cases rejecting
1var,{var, and}var.
| File | Description |
|---|---|
python/tests/unit/template_engine/blocks/test_var_block.py |
Adds three malformed-prefix regression cases. |
python/semantic_kernel/template_engine/blocks/var_block.py |
Restricts the variable prefix to a literal $. |
Review effort: Balanced
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
VarBlock.parse_contentis documented to raiseVarBlockSyntaxErrorwhen the content does not match the variable syntax ([variable] ::= "$" [valid-name], see the BNF inpython/semantic_kernel/template_engine/code_tokenizer.py). However, contents that start with1,{, or}are silently accepted as variable blocks with the first character dropped:while
VarBlock(content="test_var")(any other non-$prefix) correctly raisesVarBlockSyntaxError. The same contents then render as an unrelated kernel-argument substitution (e.g.KernelArguments(var=...)), so a template author who passes raw text into a block gets the value of an unrelated argument instead of a syntax error.This is the same root cause as issue #14491, which documents the identical
[${1}]character-class mistake inNAMED_ARG_REGEX(named_arg_block.py, fixed separately by open PRs #14492/#14147). This PR fixes only the sibling occurrence inVAR_BLOCK_REGEX(var_block.py) so the two files can be fixed without conflicting.Description
In
python/semantic_kernel/template_engine/blocks/var_block.py, the variable prefix was written as the character class[${1}]. Inside a character class,{1}is not a quantifier: it is three literal characters{,1,}. So the class matches any one of$,{,1,}instead of the literal$only.One-character-class change:
[${1}]->[$].No other parsing path is affected:
CodeTokenizeronly createsVarBlocktokens whose first character is$(verified:current_token_type = BlockTypes.VARIABLEis only set whencurrent_char == Symbols.VAR_PREFIX), andNamedArgBlock.parse_contentconstructsVarBlock(content=value, name=...)passing the name explicitly, which skips the regex. Direct construction with raw template text is the only affected path, and that is the documented contract this PR restores.Validation
python/tests/unit/template_engine/blocks/test_var_block.py::test_syntax_errors:1var,{var,}var(idsdigit_prefix,opening_brace_prefix,closing_brace_prefix).main@ aa46edd): the 3 new cases fail withDID NOT RAISE VarBlockSyntaxError; all 21 pre-existing cases pass.$varbehavior.tests/unit/template_engine tests/unit/prompt_template tests/unit/functions tests/unit/kernel tests/unit/test_serialization.py-> 1059 passed, 1 xfailed.Contribution Checklist
ruff checkclean on both changed files)AI assistance
This contribution was prepared with the help of an AI coding assistant (bug discovery, patch drafting, and test writing), reviewed and verified by the human contributor.