Skip to main content

What is it

Jinja is a powerful templating engine for Python that allows you to create dynamic content by combining static templates with runtime data. It’s widely used for generating HTML, configuration files, and in our case, AI prompts.

Why we use it

We use it to have a clean way to build larger dynamic prompts without cluttering the codebase. Here are the key reasons to use Jinja for dynamic LLM prompt generation: Structure and Readability: Jinja allows you to define a clear and organized structure for your prompts. Instead of concatenating strings in Python code, you can write human-readable templates that explicitly show the prompt’s layout. This makes complex prompts, especially those with multiple sections (e.g., “System Role”, “User Input”, “Examples”, “Constraints”), much easier to design, understand, and debug. Modularity and Reusability: Template inheritance, includes, and macros enable you to define base prompt structures and extend them for specific tasks. Break down large prompts into smaller, manageable files and create reusable snippets of prompt text. This prevents repetition and maintains consistency across many prompts. Conditional Logic: Dynamically include or exclude parts of the prompt based on your application’s logic or user input. For example, include a context section only if relevant information is available, or change the tone based on user subscription level. Dynamic Data Injection: Seamlessly inject variables from your application’s data into the prompt. This is the core of “dynamic” prompt generation, allowing personalized and context-aware prompts. Separation of Concerns: Keeps prompt definitions separate from core application logic. Your Python code handles data preparation, while Jinja handles how that data is formatted into a prompt, making your codebase cleaner and easier to maintain.

Where we use it

The prompts are located in the prompts folder. There is a service class prompt_loader.py located at the services folder which handles the loading of the templates. We use the templates mainly in the AgentNodes where we make the LLM calls but you can use the templates anywhere in the project. You can also leverage them for something like generation email templates.

Further Reading

For advanced Jinja features, custom filters, and template inheritance, refer to the official Jinja documentation.
I