Skip to content

template: str.format() with positional fields and brace escapes - #3816

Merged
ashhart merged 5 commits into
ashhart:mainfrom
atakann:template-str-format
Nov 19, 2026
Merged

ashhart merged 5 commits into
ashhart:mainfrom
atakann:template-str-format

Conversation

@atakann

@atakann atakann commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

The chat template core refused every template that calls str.format() ("str.format() is not supported"). It now runs the positional form: {} fields in order, {N} fields by index, and the {{ and }} escapes, with each argument written as Python's str() writes it. A keyword field ({name}), an attribute or index ({0.x}), a conversion ({!r}) and a format spec ({:>4}) are still refused, each by name. The errors Python raises (an index past the arguments, mixing automatic and manual numbering, a single { or }) fail here too. Hy4-flash's chat template builds its 22 special tokens with '<|hy_start{}|>'.format(HYTK), so it could not render.

  • Issue: Refs Hy4-flash on two DGX Sparks: a native CUDA family #3801. First dependency of the Hy4-flash family package; the package waits for the decisions asked there and is not in this pull request.
  • Stacked on: main.
  • atc overlap --json (atc 0.9.6, with ATC_READ_ONLY=1), run in this tree on 2026-11-18, base main 8cbef9777, this head 03acc6b33: complete: false (otherPullRequestsLimit, pullFilesUnavailable, pullRequestsLimit, timeLimit), 0 overlaps listed, so no conflict found, but the list is incomplete. A search of open pull requests and issues for str.format and format() in templates finds none.

Checks on this head

  • Head commit: 03acc6b on main 8cbef97.
  • Lean checker, on the base and on this head: 221 and 221 problems, 0 new (python3 tools/pr_check.py --base origin/main: 0 errors, 1 warning, the Unreleased line, since no number was given). This change carries no CHANGELOG line.
  • Tests run, macOS arm64 (M1 Max), Zig 0.17.0:
    • zig test of zig/src/core/template/tests.zig with the sys module and its dependencies: All 41 tests passed. The same for zig/src/core/template/template.zig: All 41 tests passed.
    • Expected strings come from jinja2's ImmutableSandboxedEnvironment in transformers' environment: the two rendering rows give <aXb>|1z|ba|{}None|x and True|1.5|qq, and the four error rows raise (IndexError, ValueError three times). The three rows the engine refuses on purpose render in Python ( 1|1|'s'); the test names them as refusals.
    • Planted control: with the one-line hook removed from methods.zig, tests.zig gives 39 passed, 2 failed (the new test and the method-coverage property test, which now lists format as supported); restored: 41 passed.
    • zig build test-server-cpu --summary all: 8 of 8 steps succeeded, 1573 of 1580 tests passed, 7 skipped.
    • zig fmt --check build.zig zig: exit 0. git diff --check: exit 0.
    • On the family branch that sits on this change (not in this pull request): the checkpoint's own chat_template.jinja renders five reference prompts (default effort, no_think, max, a system prompt, a second turn) byte for byte against tokenizer.apply_chat_template of tencent/Hy4-flash.
  • CI: green on 03acc6b: fmt, zig, zig-b, zig-windows, zig-macos-dev-tools, pytest, release-paths and both benchmark-clients jobs passed; host-nightly-linux and host-nightly-macos were skipped.
  • Not run, and why: zig build test test-golden on macOS (this Mac has no Metal Toolchain component, so the Metal kernel compiles fail before any test); zig build -Dfixed-cpu test-host on Linux (my two Linux boxes are serving a model today; I leave it to CI and will run it here on request); everything on a GPU (no GPU code is in this pull request).

Receipt

n/a for decode and prompt speed. The method runs only when a chat template calls it, and git grep -F '.format(' -- '*.jinja' '*.j2' finds no such template on this head, so no rendered prompt in the repository changes. A template that calls it was refused at render before and now renders as jinja2 does for the positional form.

  • Environment: TensorFold 8cbef97 plus this head, Zig 0.17.0, macOS arm64, CPU only, no checkpoint.
  • Licence of any new weights or drafter: n/a

Checklist

  • The title and description match the head commit, including the receipt and the stack.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency.
  • Comments and docstrings are one line. No measurements or history in the source.
  • No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.

@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thanks @atakann. Please validate the field grammar before parseInt: {+0}, {-0} and {0_0} currently become positional index zero, whereas Python treats them as keyword names. Add refusal/parity regressions for these and a positive {00} case. The positional rendering, brace handling and provenance integration otherwise look sound.

  • Synthena

@atakann

atakann commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, fixed in 10f735b: a field is an index only when every character is a decimal digit, so {+0}, {-0} and {0_0} are now refused (Python reads them as keyword names), and {00} renders as index 0. Added the refusal cases and the {00} case to the tests and the parity probes, plus the changelog line.

@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thank you @atakann. The field grammar is now checked before parseInt, so {+0}, {-0} and {0_0} are refused as Python does, while {00} renders as index zero. The refusal and parity cases are in the tests and the parity probes.

The only thing left is the CHANGELOG line: please leave the branch as it is and we will place the Unreleased entry when we land it. The branch now conflicts with main; we will rebase it ourselves, keeping your commits. Nothing else is needed from you.

  • Synthena

@ashhart
ashhart force-pushed the template-str-format branch from 10f735b to 58e18e9 Compare November 19, 2026 03:58
@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thanks @atakann. To get this moving, we rebased it onto current main ourselves, keeping every one of your commits as it was (author and message) and fitting it to the code main has gained since; no behaviour changes. It is in the landing queue now, and if anything else is needed from you we will say so here.

  • Synthena

ashhart and others added 5 commits November 19, 2026 18:17
@ashhart
ashhart force-pushed the template-str-format branch from 58e18e9 to 1d5bb69 Compare November 19, 2026 18:17
@ashhart
ashhart merged commit aa763cd into ashhart:main Nov 19, 2026
2 of 11 checks passed
@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thank you @atakann. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes.

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