Recursive AI

Fail a C# prompt template on a missing variable

OneLLM is a free, open-source LLM and agent library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Use MissingKeyMode.Error to catch missing prompt data before sending a request.

Terminal
dotnet add package RecursiveAI.OneLlm

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

In a prompt template, write a field as {{ .name }}, with a dot before the field name.

C#
using OneLlm.Templating;
 
TemplateRenderOptions strict = new()
    { MissingKey = MissingKeyMode.Error };
const string prompt = "Greet {{ .name }}.";
Dictionary<string, object?> data = new();
try
{
    string rendered = Template.Render(
        prompt, data, render: strict);
    Console.WriteLine(rendered);
}
catch (TemplateException error)
{
    Console.WriteLine(error.Detail);
}

Unzip the example, then dotnet run.

Text
map has no entry for key "name"

A missing key throws TemplateException in strict mode. Catch it where you can report the bad input, and only pass the prompt to your model client if rendering succeeds.

The full program

The first render has no name. The second uses the same template and options with name set to Ada.

C#
using OneLlm.Templating;
 
TemplateRenderOptions strict = new()
    { MissingKey = MissingKeyMode.Error };
const string prompt = "Greet {{ .name }}.";
Dictionary<string, object?> data = new();
try
{
    string rendered = Template.Render(
        prompt, data, render: strict);
    Console.WriteLine(rendered);
}
catch (TemplateException error)
{
    Console.WriteLine(error.Detail);
}
data["name"] = "Ada";
Console.WriteLine(Template.Render(
    prompt, data, render: strict));
Text
map has no entry for key "name"
Greet Ada.

Variations

Keep the default placeholder instead. Leave out render: strict to see what a missing key looks like in a permissive preview.

C#
using OneLlm.Templating;
 
var data = new Dictionary<string, object?>();
Console.WriteLine(Template.Render("Greet {{ .name }}.", data));
Text
Greet <no value>.

The short answer

Pass TemplateRenderOptions with MissingKey = MissingKeyMode.Error to Template.Render. Put the call before the code that sends the prompt. Looking up an absent field such as .name then stops rendering and names the key, instead of leaving <no value> in the text.

Why is my prompt template missing a variable?

The template engine reads the map you pass as its scope. A field like .name starts at the current scope; .inputs.name expects a nested map under inputs. If your template looks up the nested field but your data only includes a top-level name, you haven’t supplied the structure the template expects. Compare the path named by the template with the keys on the object you actually pass to the renderer, not just the keys on the original request.

A strict missing-key check is not a general input validator. A key that exists but contains null or an empty string is different from a key that isn’t present. If the prompt needs a nonempty name, validate the value in C# as well as enabling strict rendering. Likewise, exercise the inputs for each branch of a conditional template: a field that isn’t looked up in one render won’t fail that render.

How do I use the template missing-key error to find the bad input?

Read TemplateException.Detail for the error text. The error reference also lists Kind, the template name, one-based Line and Column, and byte Offset. An execution error tells you the template parsed but something went wrong while rendering it; a parse error points to the template syntax instead. In an application, keep the template name and location with the failure so you can tell which template to fix when several use the same key.

All onellm recipes