Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Ggml org Llama cpp Jinja Runtime

From Leeroopedia
Knowledge Sources
Domains Template_Engine, Runtime
Last Updated 2026-02-15 00:00 GMT

Overview

Implements the Jinja template runtime that executes compiled AST programs and produces output strings.

Description

This file is the execution engine of the Jinja pipeline, responsible for rendering chat templates with model-specific formatting. Each AST statement/expression type implements execute_impl(context &) which is called via a wrapper execute() that provides error handling with source location. Statements execute recursively: for_statement iterates over arrays/objects, if_statement evaluates conditions, set_statement binds variables, macro_statement creates callable functions. Expressions evaluate to values: binary_expression handles arithmetic/comparison/logic, member_expression accesses properties, call_expression invokes functions/filters, filter_expression applies Jinja filters. Results are gathered as string parts preserving input marking metadata.

Usage

Used as the final stage when processing Jinja chat templates. The runtime takes a parsed AST and a context containing template variables (messages, tools, etc.) and produces the formatted prompt string.

Code Reference

Source Location

Signature

bool g_jinja_debug = false;

namespace jinja {

void enable_debug(bool enable);

static value_string exec_statements(const statements & stmts, context & ctx);
static std::string get_line_col(const std::string & source, size_t pos);
static void ensure_key_type_allowed(const value & val);

// Base execute method with error handling
value statement::execute(context & ctx);
// Subclasses implement:
// value statement::execute_impl(context & ctx);

} // namespace jinja

Import

#include "runtime.h"

I/O Contract

Inputs

Name Type Required Description
stmts const statements & Yes AST statement nodes to execute (from the parsed program)
ctx context & Yes Execution context containing variable bindings (messages, tools, bos_token, etc.)

Outputs

Name Type Description
value_string value Rendered template output as a string value with input marking metadata
exceptions rethrown_exception Error messages with source location (line/column) for debugging template issues

Usage Examples

#include "lexer.h"
#include "parser.h"
#include "runtime.h"

// Full Jinja template rendering pipeline
std::string tmpl = "{% for msg in messages %}{{ msg.role }}: {{ msg.content }}\n{% endfor %}";

// Stage 1: Tokenize
auto lex_result = jinja::lexer::tokenize(tmpl);

// Stage 2: Parse
jinja::parser p(lex_result.tokens, lex_result.source);
auto ast = p.parse();

// Stage 3: Execute
jinja::context ctx(tmpl);
// Set template variables (messages array, etc.)
ctx.set_val("messages", messages_value);

auto result = jinja::exec_statements(ast.body, ctx);
std::string rendered = result->to_str();

// Enable debug logging
jinja::enable_debug(true);

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment