Otera お寺

Otera (お寺, "temple") renders Jinja templates in pure Elixir. It has no native code and no dependencies.

The first goal is the chat templates of Hugging Face models. Otera renders them like apply_chat_template of Python transformers, byte for byte. The tests compare Otera with Python Jinja2 on 112 chat templates and on the core semantics of Jinja2.

The name follows Jinja, which got its name because "temple" sounds like "template". Jinja (神社) is a Shinto shrine. Otera (お寺) is a Buddhist temple.

Otera is in development and is not on Hex yet.

Usage

Otera.render("Hello {{ name }}!", %{name: "World"})
#=> {:ok, "Hello World!"}

Parse a template once to render it many times:

{:ok, template} = Otera.parse(source, trim_blocks: true, lstrip_blocks: true)
Otera.render(template, %{messages: messages})

Otera.render/3 returns {:error, exception} with an Otera.SyntaxError for an error in the template syntax, and an Otera.Error for an error during the render. The kind of an Otera.Error names the Python exception, for example :undefined_error or :type_error, and the message is the message of Jinja2.

Custom filters, tests and functions are Elixir functions:

Otera.render("{{ name|shout }}", %{name: "otera"}, filters: %{"shout" => &String.upcase/1})
#=> {:ok, "OTERA"}

A plain Elixir function gets the positional arguments of the call. For keyword arguments and defaults, give an Otera.Function, which binds the arguments to a Python signature:

shout = Otera.Function.new("shout", ["value", {"times", 1}], fn value, times -> value <> String.duplicate("!", times) end)
Otera.render("{{ 'hi'|shout(times=2) }}", %{}, filters: %{"shout" => shout})
#=> {:ok, "hi!!"}

Chat templates

Otera.ChatTemplate renders a chat template with the environment of transformers: trim_blocks, lstrip_blocks, the functions raise_exception and strftime_now, and the tojson filter of transformers.

Otera.ChatTemplate.render(template, messages,
  add_generation_prompt: true,
  tools: tools,
  variables: %{bos_token: "<|begin_of_text|>", enable_thinking: false}
)

It also supports continue_final_message, which ends the prompt inside the final message, and documents.

strftime_now formats the current local time. To get the same prompt every time, give a fixed time to Otera.ChatTemplate.options/1 and parse the template with these options:

{:ok, template} = Otera.parse(source, Otera.ChatTemplate.options(now: ~N[2025-03-09 14:30:05]))
Otera.ChatTemplate.render(template, messages, add_generation_prompt: true)

Key order

tojson and the iteration over a dict give the keys in order. Python dicts keep the insertion order, but Elixir maps do not. Otera uses these rules:

To get the same prompt as transformers for messages and tool definitions that come from JSON, decode them with Otera.JSON.decode!/1.

Compatibility

Otera follows Python Jinja2 3.1: the lexer and its whitespace control, the operators and their precedence, scoping, macros and call blocks, loops with loop and break and continue, namespaces, the builtin filters and tests, and the Python methods of strings, lists and dicts. Values print like Python values, for example True, None and ['a', 1.5]. Like transformers, Otera renders in the immutable sandbox of Jinja2, so list.append and dict.update raise a security error.

The tags extends, block, include, import and autoescape are not in Otera yet. Without autoescape, safe gives a plain string.

Known differences from Python:

Tests

The tests compare Otera with Python Jinja2. The scripts in scripts/ make the expected results with uv:

scripts/pyproject.toml and scripts/uv.lock pin the versions of Python, Jinja2 and transformers. To make the fixtures again, run these commands from the root of the repository:

uv run --project scripts python -I scripts/fetch_chat_templates.py
uv run --project scripts python -I scripts/generate_chat_expected.py
uv run --project scripts python -I scripts/generate_semantics_expected.py
uv run --project scripts python -I scripts/generate_html_entities.py
uv run --project scripts python -I scripts/generate_unicode_properties.py

The first command changes the corpus. Run it only when you want the new templates of the Hub. Set HF_TOKEN to fetch the gated repositories instead of their public mirrors.

CI tracks the Python reference:

A red PR shows that Python changed its behavior, or that Otera does not render a new template like Python.

License

Apache-2.0