Registry / web-framework / minijinja

minijinja

JSON →
library2.20.0pypypi✓ verified 85d ago

MiniJinja is an experimental Python binding of the Rust MiniJinja template engine, currently at version 2.19.0. It provides a powerful, minimal dependency template engine with a high degree of compatibility with Jinja2. MiniJinja is noted for its strong sandboxing capabilities and its better positioning for future free-threaded Python adoption, though Jinja2 may perform faster on current Python 3.14 single-threaded environments. The library sees active development with frequent releases.

pip install minijinja
INSTALL
IMPORT
SIG · MINIJINJA
M
minijinja
web-frameworkpythonv2.20.0
Install
1.8s avg
Import
39ms
Disk
19MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.20.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.040s · 21MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.8s · import 0.038s · 21MB
19MB installed
● package 19MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Environment
from minijinja import Environment
context
from minijinja import context
Used for easily creating template contexts from Python keyword arguments.

This example demonstrates how to create a MiniJinja environment, load a template from a string, and render it with a provided context using the `context!` macro for convenience.

from minijinja import Environment, context # Create a Jinja environment env = Environment() # Add a template (can also load from files using a Loader) env.add_template("hello.html", "Hello {{ name }}! Today is {{ day }}.").unwrap() # Get the template and render it with context tmpl = env.get_template("hello.html").unwrap() rendered_output = tmpl.render(context!(name => "World", day => "Thursday")).unwrap() print(rendered_output)
Debug
Known issues
breakingMiniJinja 2.x introduced significant changes to its object model. If you were previously using dynamic objects, the upgrade might be involved and require refactoring. Refer to the official 'UPDATING' guide for migration examples.
fix
Consult the `UPDATING.md` file in the MiniJinja GitHub repository for detailed migration steps, especially concerning `Object`, `SeqObject`, and `StructObject` implementations.
affects: >=2.0.0
gotchaMiniJinja's 'strict undefined' behavior for comparison operators (`==`), string concatenation (`~`), and `in` operator with undefined needles, as well as the `default` filter's handling of explicit undefined fallback arguments, has been aligned to better match Jinja2. This might cause templates to error where they previously succeeded silently if relying on a more lenient handling of undefined values.
fix
Ensure all variables are defined in the template context, or explicitly handle potentially undefined variables using the `default` filter or `if variable is defined` checks.
affects: >=2.19.0
gotchaWhile MiniJinja aims for high Jinja2 compatibility, it does not achieve it at all costs. There might be subtle differences in behavior for certain filters, template whitespace handling, or object iteration (e.g., how maps iterate), which could lead to unexpected results if a template is directly ported without testing.
fix
Thoroughly test existing Jinja2 templates when migrating to MiniJinja. Consult MiniJinja's documentation for specific feature implementations and known divergences from Jinja2's behavior.
affects: All versions
gotchaThe Python bindings (`minijinja-py`) may silently round large Python integers that exceed the capacity of an i64 (64-bit signed integer) when passed into the template context.
fix
If working with very large integers, consider converting them to strings before passing them to the template, or implement custom filters to handle them if arithmetic operations are required within the template.
affects: All versions
gotchaPerformance between MiniJinja and Jinja2 can vary. On Python 3.14 (single-threaded), Jinja2 has been benchmarked as faster (1.54x). However, MiniJinja significantly speeds up (13%) on free-threaded Python, making it potentially more performant for future Python versions that leverage free-threading.
fix
Choose the template engine based on your specific application's performance requirements, Python version, and threading model. For current Python 3.14 applications, benchmark to determine the optimal choice. For future-proofing with free-threading, MiniJinja may be advantageous.
affects: All versions
Errors
Common errors & fixes
minijinja.exceptions.UndefinedError: 'variable_name' is undefined
A variable referenced in the template was not provided in the context passed to `render()`, or was accessed in a 'strict undefined' mode operation while being undefined.
fix
Ensure `variable_name` exists in the dictionary or context object passed to `template.render()`. Alternatively, use `{{ variable_name | default('fallback') }}` or `{% if variable_name is defined %}` to handle potentially missing variables gracefully in the template.
minijinja.exceptions.TemplateSyntaxError: Encountered unknown tag 'mytag'
The template uses a tag, filter, or test that is either misspelled, not part of the standard MiniJinja (or Jinja2) syntax, or is a custom extension not registered with the `Environment`.
fix
Check the tag/filter/test name for typos. Verify if the feature is supported by MiniJinja; if it's a custom or less common Jinja2 feature, it might not be implemented. Register custom extensions using `env.add_filter()`, `env.add_test()`, or `env.add_function()` if applicable.
Unexpected whitespace or extra newlines in rendered output.
Template whitespace control (e.g., around `{% ... %}` blocks) can be tricky to manage, especially when porting from other template engines or due to MiniJinja's default newline stripping.
fix
Utilize whitespace control characters: `{%-` to strip leading whitespace, `-%}` to strip trailing whitespace. For example, `{%- for item in items %}`. MiniJinja, like Jinja2, also removes one trailing newline from the end of the file automatically on parsing; add an extra newline if one is strictly required at the end.
Upgrade
Version history
2.20.0latest on PyPI · released May 20, 2026
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
2
Amazon
1
Resources
minijinja — pip install minijinja · libregistry