Skip to content

Support Jinja keyword-map expansion - #648

Merged
ashhart merged 1 commit into
ashhart:mainfrom
akol1:fix/jinja-keyword-map-expansion
Oct 12, 2026
Merged

ashhart merged 1 commit into
ashhart:mainfrom
akol1:fix/jinja-keyword-map-expansion

Conversation

@akol1

@akol1 akol1 commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

Problem

The shared Jinja parser rejects **mapping call arguments. Templates cannot pass dynamically selected options to macros, functions, or filters.

Change

  • Support one final **mapping expression and include it in AST traversal.
  • Expand string-keyed mappings after explicit arguments; reject non-mappings, duplicate keyword names, and invalid argument order.
  • Preserve macro binding/defaults and dict positional-map overwrite semantics. *args remains unsupported.
  • Add behavior tests for scoped mappings, macros, filters, empty mappings, collisions, invalid keys/types, and ordering. No model-family dependency.

Verification

Base: a6aa4782 (upstream main). Zig 0.17.0, macOS arm64.

  • zig build test test-golden native -Dcpu=apple_m1: passed.
  • zig test zig/src/core/template/tests.zig -lc: 13/13 passed.
  • Golden corpus: 309/319 equal, 10 existing known differences, 0 unexpected differences.
  • Standalone template executable: baseline rejects expansion at compile time; candidate renders a macro with expanded kwargs and a merged dictionary as 1:2|{"x": 3, "y": 4}.
  • Lean checker: 542 inherited findings at the pinned base and candidate; zero added findings.

Publication scope

Five files; no model code, checkpoint fixtures, performance claims, generated binaries, or local qualification artifacts. Public noreply commit identity; publication trees and outgoing commit metadata checked for private markers.

@ashhart
ashhart merged commit 6d85786 into ashhart:main Oct 12, 2026
1 of 2 checks passed
@ashhart

ashhart commented Oct 12, 2026

Copy link
Copy Markdown
Owner

Thank you, @akol1. This landed in 1.0.8 as your own commit (d8075fb81), rebased onto our 1.0.8 work; the only conflict was the CHANGELOG. Our one commit on top (1d2343843) puts the expectError call in expectRenderRejected on one line, as zig fmt writes it, and lists you as co-author. Your head commit is merged into the history unchanged (6d85786a0), so GitHub shows this PR as merged.

What we checked:

  • All 23 ** expectations in your four tests agree with transformers' render_jinja_template (Jinja2 3.1.6), the reference our template parity tool uses: the six renders give the same text, and the 17 failures fail there too, including dict(x=1, x=2), which Jinja2 refuses at compile time ("keyword argument repeated"). The 24th, dict(*[]), stays a loud refusal on purpose: Jinja2 accepts star-args, and the native engine still does not, as before. Run against the template engine without your change, three of the four new tests fail, so they pin it.
  • We served it with Nemotron 3.5 Lightning (MLX 4-bit) on an M5 Max. We rewrote the model's own chat_template.jinja with seven ** calls: the extra-keys macro fed a literal map, a tail map and dict(**...), tojson(**json_opts), and the user and system turns through a macro fed dict(**message). Loaded through your --chat-template from server: allow an explicit native chat template #551, /tokenize gave the same token ids as transformers on five conversations (plain, thinking on, multi-turn with non-ASCII text, two tools with an enum and extra schema keys, a tool round; 23 to 409 tokens), and three replies were the same tokens as with the stock template. The server without this change refuses that template at startup: "*args and **kwargs calls are not supported".
  • Replies are otherwise unchanged: 17/17 release prompts gave the same tokens as the build without your three PRs, drafted and "draft": false.

One behaviour change is in the 1.0.8 notes: a call that names the same keyword twice now fails to compile, where dict() and macros used to take the last value. Jinja2 refuses those templates too, so nothing that renders under transformers is affected.

A non-mapping, a non-string key, a repeated name or anything after the ** fails here where it fails in Jinja2, so the only templates still refused for call syntax are the ones using *args.

BobClawblaw pushed a commit to BobClawblaw/TensorFold that referenced this pull request Oct 12, 2026
@ashhart ashhart mentioned this pull request Oct 14, 2026
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
The change landed rebased onto the 1.0.8 candidate as c1904a09f with its author kept; this keeps the PR's head in the history with no change to the tree.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
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