Skip to content

Python: fix VarBlock char-class regex that silently parses contents without the $ prefix - #14841

Open
DZH166 wants to merge 1 commit into
microsoft:mainfrom
DZH166:codex/varblock-regex-20261120
Open

DZH166 wants to merge 1 commit into
microsoft:mainfrom
DZH166:codex/varblock-regex-20261120

Conversation

@DZH166

@DZH166 DZH166 commented Nov 20, 2026

Copy link
Copy Markdown

Motivation and Context

VarBlock.parse_content is documented to raise VarBlockSyntaxError when the content does not match the variable syntax ([variable] ::= "$" [valid-name], see the BNF in python/semantic_kernel/template_engine/code_tokenizer.py). However, contents that start with 1, {, or } are silently accepted as variable blocks with the first character dropped:

VarBlock(content="1var")  # -> VarBlock(name='var')  (no error)
VarBlock(content="{var")  # -> VarBlock(name='var')  (no error)
VarBlock(content="}var")  # -> VarBlock(name='var')  (no error)

while VarBlock(content="test_var") (any other non-$ prefix) correctly raises VarBlockSyntaxError. 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 in NAMED_ARG_REGEX (named_arg_block.py, fixed separately by open PRs #14492/#14147). This PR fixes only the sibling occurrence in VAR_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: CodeTokenizer only creates VarBlock tokens whose first character is $ (verified: current_token_type = BlockTypes.VARIABLE is only set when current_char == Symbols.VAR_PREFIX), and NamedArgBlock.parse_content constructs VarBlock(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

  • New regression cases in python/tests/unit/template_engine/blocks/test_var_block.py::test_syntax_errors: 1var, {var, }var (ids digit_prefix, opening_brace_prefix, closing_brace_prefix).
  • Red (before fix, on main @ aa46edd): the 3 new cases fail with DID NOT RAISE VarBlockSyntaxError; all 21 pre-existing cases pass.
  • Green (after fix): all 24 tests pass, including the pre-existing $var behavior.
  • Wider regression: tests/unit/template_engine tests/unit/prompt_template tests/unit/functions tests/unit/kernel tests/unit/test_serialization.py -> 1059 passed, 1 xfailed.
  • Before/after behavior demo included in the validation logs.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the SK Contribution Guidelines and the pre-submission formatting script raises no violations (ruff check clean on both changed files)
  • All unit tests pass, and I have added new tests where possible
  • I didn't break anyone

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.

@DZH166
DZH166 requested a review from a team as a code owner November 20, 2026 18:06
Copilot AI balanced review requested due to automatic review settings November 20, 2026 18:06
@DZH166
DZH166 deployed to github-app-auth November 20, 2026 18:07 — with GitHub Actions Active

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

1 active deployment
github-app-auth — 4f0394bb Deployed Nov 20, 2026 by DZH166 via add_label #29768
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants