Skip to main content

Module chat_template

Module chat_template 

Source
Expand description

The Jinja environment chat templates render in — transformers’ semantics, not minijinja’s defaults.

transformers renders chat_template in a jinja2 sandbox with trim_blocks + lstrip_blocks, loop controls, and a handful of helpers it injects. The one that matters most is tojson: it is NOT jinja2’s built-in (which HTML-escapes), but

def tojson(x, ensure_ascii=False, indent=None, separators=None, sort_keys=False):
    return json.dumps(x, ensure_ascii=ensure_ascii, indent=indent,
                      separators=separators, sort_keys=sort_keys)

minijinja’s own tojson differs on every axis a tool prompt touches: compact separators ({"a":1} against {"a": 1}), </>/&/' escaped as \u003c…, no ensure_ascii/separators/sort_keys keywords (MiniCPM5’s tojson(ensure_ascii=False) was a hard render error), and — without minijinja’s preserve_order — map keys sorted. Every tool declaration therefore reached the model in a shape it was never trained on, or not at all. py_json_dumps reproduces json.dumps byte for byte, including float repr and the ensure_ascii surrogate-pair escapes.

Structs§

DumpsOptions
Options of Python’s json.dumps that transformers’ tojson exposes.

Functions§

py_float_repr
Python’s float.__repr__: the shortest round-trip digits, fixed notation for decimal exponents in (-4, 16], otherwise d.ddde±XX with at least two exponent digits; json.dumps spells the non-finite values NaN / Infinity / -Infinity.
py_json_dumps
Python json.dumps(value, **opts) over a template value.
py_repr
Python repr(x) for template values.
py_str
Python str(x) for template values (strings verbatim, the rest repr).