Creating flexible, dynamic prompts is essential to getting the most out of LLMs. PromptLayer’s template system allows you to build reusable prompts where values can be inserted at runtime. This guide explains the two formatting options available in our platform: f-string and jinja2.
Jinja2 is a more powerful templating engine that extends beyond basic variable replacement. It’s ideal for complex prompt structures that require conditionals, loops, or data transformations.PromptLayer supports the full Jinja2 spec. Read more about best practices for using Jinja2 here.
Include or exclude sections based on whether variables exist or meet certain conditions:
Copy
Ask AI
Let's analyze this text:{{ text }}{% if key_points %}Focus on these key points:{% for point in key_points %}- {{ point }}{% endfor %}{% else %}Provide a general summary.{% endif %}
Iterate through lists of items to include multiple elements:
Copy
Ask AI
Please analyze the following products:{% for product in products %}- {{ product.name }}: priced at ${{ product.price }}, category: {{ product.category }}{% endfor %}
{% if customer.history %}Based on your purchase history:{% for purchase in customer.history %}- {{ purchase.item }} (purchased on {{ purchase.date }}){% endfor %}Here are our recommendations:{% for item in recommendations %}- {{ item.name }}: {{ item.description }}{% endfor %}{% else %}Welcome new customer! Here are our popular items:{% for item in popular_items %}- {{ item.name }}: {{ item.description }}{% endfor %}{% endif %}
{% if user.experience == "beginner" %}Let me explain {{ topic }} in simple terms...{% elif user.experience == "intermediate" %}As you're familiar with the basics of {{ topic }}...{% else %}Given your advanced understanding of {{ topic }}...{% endif %}
By leveraging these formatting options, you can create versatile prompt templates that adapt to different scenarios, making your PromptLayer workflows more flexible and powerful.
Template variables can also be used within structured output schemas to create dynamic validation rules and response formats. For more information, see our Structured Outputs documentation.