---
title: "Throw on missing prompt template variables in C#"
url: https://recursiveai.org/onellm/cookbook/require-template-variables-in-ai-prompts/
description: "Set missingKey: error in a OneLLM prompt definition and catch TemplateException in C# before a missing input reaches the model."
version: "1.0.1"
---

# Throw on missing prompt template variables in C#

**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/require-template-variables-in-ai-prompts/require-template-variables-in-ai-prompts.zip)

Set `missingKey: error` in a OneLLM prompt definition to stop rendering instead of sending `<no value>` to the model.

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

Save as `summary.flow` (also in the zip):

```yaml
name: ticket-summary
model: gpt-6-luna
missingKey: error
initialPrompts:
  - user:
      - text: 'Summarize: {{ .inputs.ticket }}'
```

[The example](https://recursiveai.org/onellm/cookbook/require-template-variables-in-ai-prompts/require-template-variables-in-ai-prompts.zip) is the full program below: unzip it and `dotnet run`.

With no `ticket` input, the example catches `TemplateException` and prints `map has no entry for key "ticket"`.

## The full program

This C# program runs `summary.flow` twice. The first run omits `ticket`; the second provides it. A local scripted provider records which requests reach it. The program prints the recorded prompt, not a model response. It uses no API key or network connection. `gpt-6-luna` is a model name in OneLLM’s OpenAI catalog, used here as a placeholder: `ScriptedProvider` plays its queued reply without calling that model.

To run the flow against OpenAI, choose a supported model in `summary.flow`, set [`OPENAI_API_KEY`](https://recursiveai.org/onellm/docs/getting-started/#keys), and [call `RunScriptAsync` with a plain `Runtime` and the `ticket` input](https://recursiveai.org/onellm/docs/getting-started/#a-script) instead of registering `ScriptedProvider`.

```csharp
using OneLlm;
using OneLlm.Templating;
using OneLlm.Testing;

var provider = new ScriptedProvider("openai");
var runtime = new Runtime();
runtime.Providers.Set("openai", provider);

try
{
    await runtime.RunScriptAsync("summary.flow");
    throw new InvalidOperationException("Expected a missing ticket error.");
}
catch (TemplateException error)
{
    Console.WriteLine(error.Detail);
    Console.WriteLine($"Provider requests: {provider.Requests.Count}");
}

provider.EnqueueText("Acknowledged."); // Local stand-in, not a model reply.
await runtime.RunScriptAsync("summary.flow", new Dictionary<string, object?>
{
    ["ticket"] = "The receipt did not arrive."
});
// Print the prompt the provider actually received, not its scripted reply.
Console.WriteLine(provider.Requests.Single().Messages.Last().GetUserText());
```

```text
map has no entry for key "ticket"
Provider requests: 0
Summarize: The receipt did not arrive.
```

A missing key in a prompt body stops that turn; an earlier turn in the same flow may already have run.

## Variations

**Give an optional input a default.** Add this at the top level of `summary.flow`, alongside `missingKey`, when you want a fallback rather than a required ticket:

```yaml
inputs:
  ticket: No ticket provided.
```

Caller-supplied inputs override definition defaults. Keep `missingKey: error` to catch other missing keys.

## Related recipes

- [Set strict mode when calling `Template.Render` directly](https://recursiveai.org/onellm/cookbook/fail-prompt-template-on-missing-key/)
- [Test prompt template branches](https://recursiveai.org/onellm/cookbook/test-prompt-template-branches/)

---

## The short answer

Set `missingKey: error` at the top level of the prompt definition. Then reference your required inputs under `.inputs` in the template and supply their values to `Runtime.RunScriptAsync`. OneLLM throws `TemplateException` when a prompt tries to read a missing map key. Without the option, a missing value can appear in the prompt as `<no value>`. For plain C# templates without a `.flow` file, [set strict mode on `Template.Render`](https://recursiveai.org/onellm/cookbook/fail-prompt-template-on-missing-key/).

## How does `missingKey: error` differ from a default value?

A default supplies a key even if the caller omits it. If `ticket` must come from the caller, leave it out of the definition’s `inputs` map and pass it explicitly from C#. Otherwise the template can read the definition’s default and has no missing key to report. Also distinguish an absent key from a present but empty value: if an empty ticket is invalid, validate its contents before running the flow. Strict mode checks lookup failures, not whether the input is useful text.

## Does a missing variable stop every request in a prompt flow?

Only requests for turns that haven’t happened yet. OneLLM renders the system prompt before the first provider request, then renders each user prompt before sending that turn. In this example the first user turn is the one that needs `ticket`, so the provider receives no requests. In a flow where only a later turn uses a required input, earlier turns can succeed before the missing input stops the run. Validate all caller-required values up front if you need to reject bad input before any turn. Keep those requirements out of `when` conditions: the [scripted run reference](https://recursiveai.org/onellm/docs/happy-path/#scripted-runs) says “a `when` that fails to render counts as true.”
