prompty icon indicating copy to clipboard operation
prompty copied to clipboard

TypeScript nunjucks renderer diverges from Jinja2: empty list/map are truthy, filters throw on missing or numeric values

Open ArfiArfath21 opened this issue 1 month ago • 0 comments

🐛 Bug Description

The TypeScript runtime registers jinja2 as an alias of NunjucksRenderer, and nunjucks does not follow the reference semantics that spec/jinja-subset.md requires. Two behaviours diverge from Python's jinja2 (the reference engine) on features the spec lists as REQUIRED:

  1. Empty lists and maps are truthy (§2.2: "empty list/map ... are falsy"). Nunjucks compiles {% if x %} to plain JavaScript truthiness, and [] / {} are truthy in JavaScript. A prompt that guards a loop with {% if docs %} renders the guarded block when docs is empty.
  2. Filters throw instead of coercing (§2.1 / §2.5: an undefined variable renders empty and must never raise). trim and join on an input that was not supplied raise TypeError, and upper / lower / trim on a number or boolean raise TypeError. Jinja2 returns "" for undefined and stringifies numbers.

The same template therefore renders differently in Python and TypeScript, which is exactly the "author once, render anywhere" guarantee the subset spec is meant to protect. None of the current render vectors catch it (the conditional vectors only use true/false, and the filter vectors only use present strings and lists).

Steps to reproduce

e2e.prompty:

---
name: e2e
model:
  id: gpt-4o
  provider: openai
  connection: { kind: key, apiKey: x }
inputs:
  - name: docs
    kind: array
    items: { kind: string }
  - name: q
    kind: string
---
system:
You are helpful.
{% if docs %}
Use these documents:
{% for d in docs %}- {{ d }}
{% endfor %}{% else %}
No documents.
{% endif %}
user:
{{ q | trim }}

Run prepare(agent, { docs: [], q: " hi " }) in the Python and TypeScript runtimes (2.0.3 / 2.0.1 on main at 007abf34).

Filter crashes (TS only), with the Python result in parentheses:

{{ missing | trim }}          TypeError: Cannot read properties of undefined (reading 'replace')   ("")
{{ missing | join(",") }}     TypeError: arr.join is not a function                                ("")
{{ count | upper }}           TypeError: str.toUpperCase is not a function  (count = 30)           ("30")
{{ count | trim }}            TypeError: str.replace is not a function      (count = 30)           ("30")

Expected behavior

Both runtimes render the same prompt:

system: You are helpful.\n\nNo documents.
user:   hi

and {{ missing | trim }} renders "".

Actual behavior

runtime docs=[] system message
Python You are helpful.\n\nNo documents.
TypeScript You are helpful.\n\nUse these documents:

and the filter cases above throw during prepare().

How I found it

I wrote a small differential fuzzer: it generates random templates from the subset grammar (if/elif/else, for with loop.*, comments, trim markers {%- / -%}, the six required filters, whitespace and role-marker text) and renders them with real jinja2, nunjucks (the TS renderer), and the owned jinja_subset engines in Python and TypeScript, then diffs the output. Over 6,000 templates per run:

  • Python jinja2, Python jinja_subset and TS jinja_subset agree on 100% of the in-subset cases, so the owned engines already match the reference.
  • Nunjucks differs only on the two points above (plus list/map stringification, which is outside the subset).
  • Rust's minijinja (checked through its Python bindings) matches Jinja2 on every probe above, so TypeScript is the outlier.

I could not run the Java, C# and Swift renderers locally, so I have not checked those.

Your environment

  • prompty TypeScript 2.0.1 and Python 2.0.3 (main at 007abf34)
  • nunjucks 3.2.4, jinja2 3.1.6, Node 25, macOS

Proposed fix

I have a PR ready that keeps nunjucks but makes it follow the reference for these cases: a Jinja-style truthiness check for conditions, and coercing upper / lower / trim / join. It does not change the renderer's key or its sandbox. I am happy to rework it if you would rather have the TS jinja2 key point at the owned subset engine (#511 / #542 direction), and I can add shared render vectors for empty-collection truthiness and undefined-through-filter so the other runtimes are held to the same behaviour.

ArfiArfath21 avatar Oct 18 '26 18:10 ArfiArfath21