Skip to content

✨ jinja: add round, sum, min and max filters - #2885

Open
dmitriybabichev wants to merge 1 commit into
huggingface:mainfrom
dmitriybabichev:jinja-format-round
Open

dmitriybabichev wants to merge 1 commit into
huggingface:mainfrom
dmitriybabichev:jinja-format-round

Conversation

@dmitriybabichev

@dmitriybabichev dmitriybabichev commented Nov 18, 2026 •

Copy link
Copy Markdown

The jinja package has no round, sum, min or max filters, so templates using them fail with "Unknown NumericValue filter: round" (or "Unknown ArrayValue filter: sum", etc.) while Python Jinja2 renders them.

This adds all four, following the Jinja2 signatures: round(precision=0, method='common'|'ceil'|'floor'), sum(attribute=None, start=0), min/max(case_sensitive=False, attribute=None).

Details worth checking against Python: round with the default method is Python's round(), so ties go to the even digit (2.5|round is 2.0) and 2.675|round(2) is 2.67; ints stay ints and floats stay floats; sum returns a float as soon as a float is involved; min/max on an empty list give undefined and keep the first of equal items.

Tests are in test/numeric-filters.test.js; expected values were generated with Jinja2 3.1. vitest run passes except e2e.test.js, which fails to resolve @huggingface/hub in my checkout both with and without this change. tsc and oxfmt --check are clean.


Note

Low Risk
Scoped runtime filter additions with dedicated tests; no auth or data-path changes, mainly template compatibility.

Overview
Adds Python Jinja2–compatible built-in filters round, sum, min, and max so templates that use them no longer fail at runtime.

round supports precision, method (common | ceil | floor), and keyword args. Default common uses a new roundHalfEven helper (ties to even, decimal-aware behavior like 2.675|round(2) → 2.67); integers/booleans stay integers when appropriate, floats stay floats. sum works on arrays with optional attribute and start, promoting to float when any operand is float. min / max compare items (or an attribute) via existing compareRuntimeValues, honor case_sensitive, return undefined on empty lists, and keep the first tie.

Filter dispatch in applyFilter routes these four names (with or without call syntax) through applyNumericFilter. New numeric-filters.test.js asserts outputs against Jinja2 3.1 expectations.

Reviewed by Cursor Bugbot for commit 9b977d0. Bugbot is set up for automated code reviews on this repo. Configure here.

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.

1 participant