llama.cpp Jinja runtime (minja replacement) tojson and filter semantics
Parent: Mac local LLMs: llama.cpp internals · Published reference · snapshot 2026-10-05
↓ Facts as markdownall context files
The README says the engine "was introduced in PR#18462", lives in `common/jinja`, was originally inspired by huggingface.js's jinja package, and avoids C++ operator overloading.
These notes link each claim to its source. A source may be a research report hosted on this site rather than the primary document. A published reference means the content is available; it does not certify independent review or accuracy.Read the editorial policy and follow the sources before relying on a claim.
Facts
- The README says the engine "was introduced in PR#18462", lives in `common/jinja`, was originally inspired by huggingface.js's jinja package, and avoids C++ operator overloading. [source]
- PR 18462 (ngxson, 2025-12-29) says the engine "may (or may not?) replace minja", was tested on 370 templates with 14 failures versus 8 for minja, and leaves input marking implemented but unused until a server follow-up. [source]
- PR 18462 regroups all chat-template workarounds in `common/chat.cpp` under a `workaround` namespace so they run on the data before it enters the runtime. [source]
- Input marking uses `jinja::string` with an `is_input` flag; one-to-one transforms preserve it, and split or join results are marked only if all input parts are marked; it is enabled by `global_from_json` with `mark_input = true`. [source]
- Input-marking caveats: special tokens dynamically built from user input (for example `'<|' + message['role'] + '|>'`) will not function, and a prepended space such as `' ' + message['content']` becomes its own token. [source]
- `tojson` takes `ensure_ascii`, `indent`, `separators` and `sort_keys`; `ensure_ascii` and `sort_keys` default to false when undefined. [source]
- `tojson(sort_keys=true)` throws `not_implemented_exception("tojson sort_keys=true not implemented")`. [source]
- `tojson` item separator defaults to `", "` when `indent` is negative or unset and `","` when an indent is given, and the key separator defaults to `": "`; a `separators` array overrides them. [source]
- With `ensure_ascii=true`, non-ASCII code points become `\uXXXX` escapes (surrogate pairs above U+FFFF) and invalid UTF-8 becomes `�`, applied only inside JSON strings. [source]
- On ints and floats the `safe` and `string` filters are bound to `tojson`, and on strings `tojson` is also bound. [source]
- `apply_filter` aliases `count` to `length`, `d` to `default`, `e` to `escape` and `trim` to `strip` before lookup. [source]
- `apply_filter` coerces non-string inputs to strings for `capitalize`, `lower`, `replace`, `strip`, `title`, `upper` and `wordcount`. [source]
- An unknown filter throws `Unknown (built-in) filter '<name>' for type <type>`. [source]
- `value.cpp` marks as not implemented: string `join`, object `join`, array `unique`, `replace` with a count argument, `format` beyond simple `{}` placeholders, `map` with filter-mapping, and the `escaped` and `filter` tests. [source]
- `dictsort` is always case sensitive (a FIXME ignores `case_sensitive`) and accepts `by` (`value` or key) and `reverse`. [source]
- `selectattr` and `rejectattr` throw `selectattr: unknown test '<name>'` for a test they do not know. [source]
- `value.cpp` defines no builtin named `escape` or `sum` for arrays in the fetched file. [source]
- The engine's test suite has dedicated `tojson ensure_ascii=true` cases, including nested objects and indent 2. [source]
- llama.cpp commit d9f918d2 (2026-08-22), "common: add json.h abstraction (#27511)", is part of the move to decouple the engine from the JSON library. [source]
- Inferred: a Qwen-style template that pipes arguments through `tojson | safe` renders non-ASCII arguments as raw UTF-8 on llama.cpp, whereas a Python `json.dumps` default would escape them; byte-level prefix tests done in Python can differ. [source]
Children
- No children recorded.