---
title: "Test prompt template conditionals in C#"
url: https://recursiveai.org/onellm/cookbook/test-prompt-template-branches/
description: "Test both branches of a prompt template in C#. Render OneLLM if/else with true and false and assert the exact prompt text without calling a model."
version: "1.0.1"
---

# Test prompt template conditionals 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/test-prompt-template-branches/test-prompt-template-branches.zip)

Render with `true` and `false`, then compare the text. No model needed.

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

The conditional uses Go-template syntax: `.brief` reads the `brief` key from the dictionary.

```csharp
using OneLlm.Templating;

const string prompt =
    "{{if .brief}}One line." +
    "{{else}}Explain the steps.{{end}}";
Check(true, "One line.");
Check(false, "Explain the steps.");

void Check(bool brief, string expected)
{
    Dictionary<string, object?> values =
        new() { ["brief"] = brief };
    var actual =
        Template.Render(prompt, values);
    if (actual != expected)
        throw new Exception(actual);
    Console.WriteLine(actual);
}
```

If the condition changes and a render no longer matches its expected text, `Check` throws and the program exits with an error.

Unzip [the example](https://recursiveai.org/onellm/cookbook/test-prompt-template-branches/test-prompt-template-branches.zip), then `dotnet run`.

```text
One line.
Explain the steps.
```

## Variations

**Catch a missing condition key.** Add this after the two `Check` calls. The empty dictionary has no `brief` key, so strict mode throws instead of rendering the `else` branch.

```csharp
var missing = new Dictionary<string, object?>();
try
{
    _ = Template.Render(prompt, missing,
        render: new()
        { MissingKey = MissingKeyMode.Error });
    throw new Exception("Expected a missing key error.");
}
catch (TemplateException error)
{
    Console.WriteLine(error.Detail);
}
```

The variation prints one more line after the two rendered prompts:

```text
map has no entry for key "brief"
```

## Related recipes

- [Render a prompt template in C#](https://recursiveai.org/onellm/cookbook/render-prompt-template/)
- [Fail a C# prompt template on a missing variable](https://recursiveai.org/onellm/cookbook/fail-prompt-template-on-missing-key/)

## The short answer

Call `Template.Render` with a dictionary that makes the condition true and a second dictionary that makes it false. Compare each rendered prompt to its own expected text. OneLLM’s renderer takes plain values and runs apart from the model provider, so neither check requires a key or a prediction. A failed comparison points to the prompt or its input values, not to a change in model behavior.

## How do you unit test both sides of an if/else prompt template?

Start with inputs that differ only in the value used by the condition. In this example, the top-level `brief` key is a C# Boolean. Each test case supplies the expected text explicitly rather than reimplementing the branch logic inside the assertion. For an `else if`, add a case where the first condition fails and the next succeeds. When a branch adds a new input lookup, put that value in the cases that exercise it; testing only the other branch won’t catch a misspelled key there.

Use an exact comparison for a short prompt. It detects the wrong branch, but also extra spaces or line breaks introduced around the template actions. For a long prompt, assert the branch-specific instruction and the absence of its alternative if the rest of the text does not matter to this test. If your project uses a unit test runner, move each case into a parameterized test so it reports which input failed.

## Why does a conditional prompt render the wrong branch?

Check the input type and key path first. The [template language](https://recursiveai.org/onellm/docs/templating/#the-language) follows Go template rules: a nonempty string such as `"false"` is not the Boolean `false`. If the value comes from configuration or a form, convert it to a Boolean before building the prompt scope. Also distinguish a missing key from a present key with a false value. Strict missing-key mode catches the former; false is legitimate data and should still select the `else` text.
