Recursive AI

Escape double curly braces in a C# prompt template

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

In a OneLLM prompt, {{ raw "{{" }} prints two opening braces without stopping substitutions.

In a regular C# interpolated string ($"...") or a string.Format format string, write {{ for {; in a OneLLM prompt template, use raw.

Terminal
dotnet add package RecursiveAI.OneLlm

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

C#
using OneLlm.Templating;
 
var prompt = """
    Example: {{ raw "{{" }} name }}
    Hello {{ .name }}!
    """;
var scope = new Dictionary<string, object?>
{
    ["name"] = "Ada"
};
Console.WriteLine(Template.Render(prompt, scope));

Unzip the example, then dotnet run.

Text
Example: {{ name }}
Hello Ada!

The full program

C#
using OneLlm.Templating;
 
var prompt = """
    Example: {{ raw "{{" }} name }}
    Hello {{ .name }}!
    """;
var scope = new Dictionary<string, object?>
{
    ["name"] = "Ada"
};
Console.WriteLine(Template.Render(prompt, scope));
 
// Markdown code spans already preserve delimiters.
var markdown = "Show `{{ .name }}` to {{ .name }}.";
Console.WriteLine(Template.Render(markdown, scope));
Text
Example: {{ name }}
Hello Ada!
Show `{{ .name }}` to Ada.

An action is an instruction between {{ and }}. The first action prints the opening braces as text; the second reads Ada from scope, the values passed into the template. The last line shows that a Markdown code region (inline code or a fenced code block) preserves braces without a raw call.

An unmatched opening delimiter outside a code region can cause TemplateException; its position refers to the original prompt text.

Variations

Print the entire example action literally. Replace the Example: line inside prompt:

Text
Example: {{ raw "{{ name }}" }}
Text
Example: {{ name }}
Hello Ada!

The short answer

Write {{ raw "{{" }} in a OneLLM prompt to print two opening curly braces. The raw function returns its string unchanged. Text such as name }} after that action is literal; an action such as {{ .name }} elsewhere in the same prompt still reads the name value from the scope. Nothing needs to call a model: Template.Render takes the template and its values and returns a string.

How do you escape curly braces in a C# string without changing the template?

There are two readers here: C# reads the string literal, then OneLLM reads the resulting prompt. Use a non-interpolated string for the prompt. The example uses a C# raw string (triple quotes with no $) so the double quotes in raw need no C# escaping. A regular C# string can hold the same prompt if you escape the inner quotes. Neither approach changes what OneLLM treats as an action. Doubling braces is a C# formatting rule, not how you escape an opening delimiter in a OneLLM prompt.

For a prompt that includes a placeholder example in prose, wrap the example in a single raw action as in the variation. When the literal part is just an opening delimiter, pass only that delimiter. There’s no need to replace every brace in the prompt: doing so would also change the actions you want to run.

How do you keep double curly braces literal inside Markdown code?

OneLLM’s template engine preserves braces inside Markdown inline code and fenced code blocks by default. If the prompt already uses code formatting to show an example, leave the braces alone in that region. In the full program, the name action inside backticks remains visible as template syntax; the one after to is evaluated. The backticks remain in the output.

Use raw outside code regions, where OneLLM reads delimiters normally. Conversely, if you expect a substitution inside a code example and get the literal action instead, check whether backticks surround it. The parser option EscapeCodeRegions can turn off the default behavior for a template that needs substitutions inside code regions.

All onellm recipes