---
title: "Fail a C# prompt template on a missing variable"
url: https://recursiveai.org/onellm/cookbook/fail-prompt-template-on-missing-key/
description: "Fail a C# prompt template when a required variable is missing. Set MissingKeyMode.Error and catch the named key before sending the prompt."
version: "1.0.1"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneLlm) · [GitHub](https://github.com/recursive-ai-oss/onellm-csharp) · [Docs](https://recursiveai.org/onellm/docs/) · [Download example (.zip)](https://recursiveai.org/onellm/cookbook/fail-prompt-template-on-missing-key/fail-prompt-template-on-missing-key.zip)

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

```shell
dotnet add package RecursiveAI.OneLlm
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

In a [prompt template](https://recursiveai.org/onellm/docs/templating/#the-language), write a field as `{{ .name }}`, with a dot before the field name.

```csharp
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](https://recursiveai.org/onellm/cookbook/fail-prompt-template-on-missing-key/fail-prompt-template-on-missing-key.zip), 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`.

```csharp
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.

```csharp
using OneLlm.Templating;

var data = new Dictionary<string, object?>();
Console.WriteLine(Template.Render("Greet {{ .name }}.", data));
```

```text
Greet <no value>.
```

## Related recipes

- [Render a prompt template in C#](https://recursiveai.org/onellm/cookbook/render-prompt-template/)
- [Test LLM code without an API key](https://recursiveai.org/onellm/cookbook/test-llm-code-without-api-key/)

## 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](https://recursiveai.org/onellm/docs/templating/) 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](https://recursiveai.org/onellm/docs/templating/#errors) 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.
