Recursive AI

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.

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

Terminal
dotnet add package RecursiveAI.OneLlm

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

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

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

C#
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"

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

All onellm recipes