Prompts are rarely static in practical applications. Inserting user input, retrieved documents, or conversational history into a predefined structure is a common requirement. Hardcoding prompts by manually concatenating strings is difficult and error-prone. This mixes the logic of an application with the prompt's content, making the code hard to read, maintain, and update.
A much better approach is to use a template engine. This separates the fixed structure of your prompt from the dynamic data that populates it. You define a template with placeholders for variables, and at runtime, the engine substitutes these placeholders with actual values. This makes your prompts reusable, easier to manage, and simpler to version.
The template engine uses double curly braces, such as {{variable_name}}, to mark placeholders. To generate a complete prompt, you provide the template string and a dictionary containing the values for your variables.
The render_template function handles this substitution. Let's create a simple system prompt that can be adapted for different AI agent personas.
from kerb.prompt import render_template
template = """You are a {{role}} assistant specialized in {{domain}}.
Your task is to help users with {{task}}."""
variables = {
"role": "helpful",
"domain": "Python programming",
"task": "code review and debugging"
}
final_prompt = render_template(template, variables)
print(final_prompt)
This code produces the following output, with all variables correctly substituted:
You are a helpful assistant specialized in Python programming.
Your task is to help users with code review and debugging.
By changing the values in the variables dictionary, you can reuse the same template to generate prompts for countless other roles without altering the prompt's structure.
Your dynamic data is not limited to simple key-value pairs. You can also pass nested objects, like dictionaries, and access their values using dot notation. This is particularly useful for populating prompts with structured information, such as user profiles or configuration settings.
Consider a scenario where you want to personalize a response based on a user's profile information.
from kerb.prompt import render_template
template = """Generate a personalized response for the user below.
User Profile:
- Name: {{user.name}}
- Role: {{user.role}}
- Interests: {{user.interests}}
Address their interest in {{user.interests}} when responding to the query about {{context}}."""
variables = {
"user": {
"name": "Alice Johnson",
"role": "Senior Developer",
"interests": "Machine Learning, Cloud Architecture"
},
"context": "recommending learning resources"
}
personalized_prompt = render_template(template, variables)
print(personalized_prompt)
The resulting prompt will be correctly populated with the nested user data:
Generate a personalized response for the user below.
User Profile:
- Name: Alice Johnson
- Role: Senior Developer
- Interests: Machine Learning, Cloud Architecture
Address their interest in Machine Learning, Cloud Architecture when responding to the query about recommending learning resources.
While {{...}} is a common syntax, some LLM providers or frameworks may use different delimiters for their placeholders, such as single curly braces ({...}) or percent signs (%...%). The template engine accommodates this through an optional delimiters parameter, which takes a tuple of the opening and closing delimiters.
For instance, to use single-brace placeholders, you would do the following:
from kerb.prompt import render_template
template = "Translate '{text}' from {source_lang} to {target_lang}."
variables = {
"text": "Hello!",
"source_lang": "English",
"target_lang": "Spanish"
}
# Specify single braces as delimiters
final_prompt = render_template(template, variables, delimiters=("{", "}"))
print(final_prompt)
This flexibility ensures you can use the same rendering logic even if your target model or platform has a different templating syntax.
In a production environment, you cannot always guarantee that every variable will be available when a prompt is rendered. A missing variable would typically cause an error, potentially crashing your application.
To build more resilient systems, you can use render_template_safe. This function behaves like render_template but allows you to specify a default fallback value for any missing variables, preventing errors.
from kerb.prompt import render_template_safe
template = "User: {{user_name}}\nContext: {{context}}"
# 'context' variable is missing from this dictionary
variables = {
"system_role": "AI Assistant",
"user_name": "Bob"
}
# Use safe rendering with a default fallback string
safe_prompt = render_template_safe(template, variables, default="[Not provided]")
print(safe_prompt)
Instead of raising an error, the function substitutes the missing placeholder with the default value, producing a usable prompt:
User: Bob
Context: [Not provided]
Was this section helpful?
© 2026 ApX Machine LearningEngineered with