LLMs, GenAI, RAG, agents, evaluation · insight-1515
Render prompt templates in strict mode and unit-test empty and missing variables
- Evidence type
- documented fact
- Difficulty
- easy
- Roles
- ML engineer, Applied AI
- Industries
- cross-industry
Summary: situation, action, reason, boundary
Situation
A prompt template inserts retrieved passages and a customer name, and a failed lookup leaves both empty.
Action
Use a template mode that raises on undefined variables, render every template in tests with missing, empty, very long and brace-containing values, and branch in code before the call when required context is absent.
Reason
Jinja's default undefined value renders as an empty string, while StrictUndefined raises an error when it is printed or iterated.
Boundary
Any prompt assembled from variables or retrieved content.
Symptom or risk
The model receives an empty evidence block or a literal None, answers from priors anyway, and the failure looks like a model quality problem.
Exceptions
Genuinely optional blocks, which should be explicit conditionals rather than silent blanks.
Trade-offs
Strict mode surfaces more errors during development and at runtime, which you must handle.
Pitfalls
User text that itself contains template braces or your delimiter strings; and expecting strict mode to catch None or empty strings, which are defined values and still need an explicit check.
Lessons and concepts
Claims and their sources
The Jinja documentation states that the default Undefined can be printed and iterated and renders as an empty string, whereas StrictUndefined raises an error on printing, iteration and comparison.