#2669·instructor

context= templating silently deletes unmatched {{ }} text instead of erroring

Author: PUNITH-VCreated Sep 20, 2026Updated Sep 24, 2026

Describe the bug

I expected only the variables I pass in context to be replaced in the message, with everything else left as-is. Instead, any {{ }} text that doesn't match a context variable gets silently removed — no error, no warning, nothing left in its place.

To Reproduce

python
from instructor.v2.core.templating import handle_templating
from instructor import Mode

kwargs = {
    "model": "gpt-4o-mini",
    "messages": [
        {"role": "user", "content": "Explain what {{ jinja_example }} means, and greet {{ user_name }}."},
    ],
}

result = handle_templating(kwargs, Mode.TOOLS, context={"user_name": "Ada"})
print(result["messages"][0]["content"])
# Output: "Explain what  means, and greet Ada."

Expected behavior

Either an error/warning when a template variable has no matching key in context, or the literal {{ jinja_example }} text left untouched.

Root cause

Jinja's .render(**context) doesn't raise an error when a {{ }} variable has no matching key in context. Instead, it substitutes Jinja's internal Undefined object, which silently converts to an empty string when the template is rendered to text. So any unmatched {{ }} in the message just disappears instead of failing loudly.

Suggested fix

Either raise an error when a {{ }} in the message has no matching key in context, or support an explicit way to escape literal braces (e.g. Jinja's own {% raw %}...{% endraw %} block) so they're left untouched instead of silently disappearing.

Happy to open a PR for this if the direction looks right — let me know if you'd prefer the strict-error approach or the escape-syntax approach (or something else).