Skip to content

jinja

jinja

Jinja template validation checks.

Validates Jinja2 template syntax and detects undefined variables that would cause runtime errors.

Attributes

Classes

JinjaSyntaxCheck

Check for Jinja template syntax errors (V001).

Attempts to parse templates and reports syntax errors with line numbers and context. This catches issues like: - Unclosed blocks ({% if ... without {% endif %}) - Unclosed expressions ({{ ... without }}) - Invalid syntax inside blocks

Functions
check
check(config, config_path, raw_yaml)

Check Jinja syntax in templates.

Source code in src/marianne/validation/checks/jinja.py
def check(
    self,
    config: JobConfig,
    config_path: Path,
    raw_yaml: str,
) -> list[ValidationIssue]:
    """Check Jinja syntax in templates."""
    issues: list[ValidationIssue] = []
    env = jinja2.Environment()

    # Check inline template
    if config.prompt.template:
        template_issues = self._check_template(
            config.prompt.template,
            "prompt.template",
            find_line_in_yaml(raw_yaml, "template:"),
            env,
        )
        issues.extend(template_issues)

    # Check external template file
    if config.prompt.template_file:
        template_path = resolve_path(config.prompt.template_file, config_path)
        if template_path.exists():
            try:
                template_content = template_path.read_text()
                template_issues = self._check_template(
                    template_content,
                    f"template_file ({template_path.name})",
                    None,
                    env,
                )
                issues.extend(template_issues)
            except Exception as e:
                issues.append(
                    ValidationIssue(
                        check_id=self.check_id,
                        severity=self.severity,
                        message=f"Could not read template file: {e}",
                        suggestion=f"Check file permissions for {template_path}",
                    )
                )

    return issues

JinjaUndefinedVariableCheck

Check for undefined template variables (V101).

Warns about variables used in templates that aren't defined in the config's variables section or the built-in sheet context. Uses fuzzy matching to suggest corrections for typos.

Functions
check
check(config, config_path, raw_yaml)

Check for undefined variables in templates.

Source code in src/marianne/validation/checks/jinja.py
def check(
    self,
    config: JobConfig,
    config_path: Path,
    raw_yaml: str,
) -> list[ValidationIssue]:
    """Check for undefined variables in templates."""
    issues: list[ValidationIssue] = []
    env = jinja2.Environment()

    # Collect all defined variables
    defined_vars = set(self.BUILTIN_VARIABLES)
    defined_vars.update(config.prompt.variables.keys())

    # Check inline template
    if config.prompt.template:
        var_issues = self._check_undefined_vars(
            config.prompt.template,
            "prompt.template",
            defined_vars,
            env,
        )
        issues.extend(var_issues)

    # Check external template file
    if config.prompt.template_file:
        template_path = resolve_path(config.prompt.template_file, config_path)
        if template_path.exists():
            try:
                template_content = template_path.read_text()
                var_issues = self._check_undefined_vars(
                    template_content,
                    f"template_file ({template_path.name})",
                    defined_vars,
                    env,
                )
                issues.extend(var_issues)
            except Exception as exc:
                issues.append(ValidationIssue(
                    check_id="V101",
                    severity=ValidationSeverity.WARNING,
                    message=f"Cannot read template file '{template_path.name}' "
                            f"for undefined variable check: {exc}",
                    suggestion="Ensure the template file is readable.",
                ))

    return issues

Functions