For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
BlogLog InRequest Demo
HomeProductDevelopersSelf-HostingChangelog
HomeProductDevelopersSelf-HostingChangelog
  • Getting Started
    • Overview
  • Agent Builder
    • Using the Agent Builder
  • Prompts
    • Prompt Engineering
    • Collaboration
    • Custom Models
    • Multimodality
    • Prompt Caching
  • Workflows
    • Introduction
    • Experimenting
      • Overview
      • Agent Node
      • Prompt Node
      • Prompt Deployment Node
      • Templating Node
      • Search Node
      • API Node
      • Code Execution Node
      • Subworkflow Node
      • Map Node
      • Guardrail Node
      • Conditional Node
      • Merge Node
      • Final Output Node
      • Error Node
      • Note Node
      • Node Adornments
    • Integrating
    • Function Calling
  • Evaluation & Test Suites
    • Quantitative Evaluation
    • Evaluating RAG Pipelines
    • Online Evaluations
  • Metrics
    • Out of the Box Metrics
    • Custom Metrics
    • Reusing Metrics in Test Suites
  • Deployments
    • Deployment Lifecycle Management
    • Observability in Production
    • Environments
    • Release Tags
    • Release Reviews
  • Monitoring
    • Monitoring Production Trends
    • Track Workflow Execution Costs
    • Datadog Integration
    • Webhook Integration
    • Execution URLs
  • Documents
    • Uploading Documents
    • Integrating w/ Search API
    • Metadata Filtering
  • Security
    • Data Privacy and Storage
    • HMAC Authentication
    • Role-Based Access Control (RBAC)
    • Static IPs
  • Organizations
    • Manage Organization Access
    • Data Retention Policies
LogoLogo
BlogLog InRequest Demo
On this page
  • Templating Node Interface
  • Pro Tips
  • Troubleshooting Tips
  • Tips - Using Jinja
  • Common Templates
  • String Manipulation
  • Output Only the First n Characters
  • JSON Manipulation
  • Checking LLM Output for Valid JSON
  • Chat History Manipulation
  • Output the Most Recent n Messages in Chat History
  • Search Result Manipulation
  • Citing Sources via Chunk Concatenation Customization
WorkflowsNodes

Templating Node

Was this page helpful?
Previous

Search Node

Next
Built with

The Templating Node allows you to perform custom data transformations on a set of defined inputs to create a new output. You can use this to define constants, manipulate data before feeding into a prompt, or massage a response to a format of your liking.

Templating Node Interface

The Templating Node provides a simple interface for configuring your data transformations:

Templating Node

When you open the Templating Node, you’ll see a detailed configuration interface where you can define your template:

Templating Node Configuration

Check out our Common Data Transformation Templates for some common examples.

Pro Tips

  • XML Tags for LLMs: Use XML tags to help LLMs delimit where long chunks of context start and stop. This can give huge performance boosts.
  • Jinja Flexibility: Remember that you can use Jinja directly in your Prompt Node Blocks as well as in Templating Nodes.

Troubleshooting Tips

Tip: JSON Syntax

You may have a templating node that outputs JSON which seems valid, but yields the following error when you click “Test” or run your workflow:

Tips - Using Jinja

Jinja has a tendency to leave hard-to-see whitespace which can cause issues when doing equality checks in places like Metrics or Conditional Nodes.

Templating Node JSON Error
Use double quotes when working with JSON
Tip: Unexpected Whitespace

Jinja has a tendency to leave hard-to-see whitespace which can cause issues when doing equality checks in places like Metrics or Conditional Nodes.

1{# this example will have invisible whitespace #}
2{% if some_condition %}
3 {{ result A }}
4{% else %}
5 {{ result B }}
6{% endif %}
7
8{# this will give the result you expect #}
9{%- if some_condition -%}
10 {{- result A -}}
11{%- else -%}
12 {{- result B -}}
13{%- endif -%}

Common Templates

The Templating Node supports Jinja2 syntax and is a flexible way of performing light-weight data transformations as part of your Workflow. Here are some common data manipulations you may want to make in a Workflow and how you define them via Templating Nodes.

String Manipulation

Output Only the First n Characters

Useful if you want to ensure that you’re not providing too much context to a prompt.

String Manipulation

Template
1{{ user_input[:10] }}
Example
Inputs:
-------
user_input = "Hello, world!"
Output:
-------
"Hello, wor"

JSON Manipulation

Checking LLM Output for Valid JSON

If you’re trying to extract structured JSON from unstructed text using a prompt, or if you want to use OpenAI’s function-calling functionality, it’s likely you’ll need to check whether an LLM’s response is valid JSON and if so, convert the output string as proper JSON.

You can also extract specific properties from valid JSON strings.

Here’s how to do it:

JSON Manipulation

Template
1{% if maybe_json|is_valid_json_string %}
2 {{ maybe_json }}
3
4 ## to extract specific properties from the JSON
5 {{ json.loads(maybe_json).property }}
6{% else %}
7 {{ {} }}
8{% endif %}
Inputs:
-------
maybe_json = '{"key": "value"}'
Output:
-------
{"key": "value"}

Chat History Manipulation

Output the Most Recent n Messages in Chat History

If you’re building a chatbot and conversations can be long-lived, you may find that your chat histories are too long to fit within the context window of a prompt.

Once simple solution is to only ever include the most recent n messages from the conversation. Here’s how you can do this:

Chat History Manipulation

Template
1{{ chat_history[-2:] }}
Example
Inputs:
-------
chat_history = [
{"role": "USER", "text": "What color is the sky?"},
{"role": "ASSISTANT", "text": "Blue"},
{"role": "USER", "text": "But why"}
]
Output:
-------
[
{"role": "ASSISTANT", "text": "Blue"},
{"role": "USER", "text": "But why"}
]

Search Result Manipulation

Citing Sources via Chunk Concatenation Customization

Search Nodes make it easy to query a vector store for text that’s semantically similar to some input. By default, the chunks of text that are returned are concatenated together into a single string using a configurable separator (e.g. \n\n#####\n\n). The flattened string can then be fed directly to Prompt Nodes as an input variable and referenced within your prompt template.

However, if you want your Prompt to cite its sources and say where it got the info it used to generate its response, then you’ll need more than just the chunk text. You need the name/id/url/etc of the document each chunk came from and you need to provide this info to your Prompt in a consumable form. This is where Templating Nodes come in.

The template below takes in the raw search results and performs custom chunk concatenation, but also pulls in info from the document associated with each chunk.

Search Result Manipulation

Template
1{% for result in search_results -%}
2Source:
3{{ result.document.label }}
4
5Content:
6{{ result.text }}
7{% if not loop.last %}
8
9#####
10
11{% endif %}
12{% endfor %}
Example
Inputs:
-------
search_results = [
{
"text": "Hello, world!",
"score": 0.015,
"keywords": ["hello", "world”],
"document": {
"id": "22df06cf-c876-45ef-a162-4836c410e37b",
"label": "introduction.txt",
"external_id": "introduction.txt"
}
},
{
"text": "The sky is blue.",
"score": 0.005,
"keywords": ["sky", "blue”],
"document": {
"id": "d9655f5f-885e-400e-b000-00b605a03a99",
"label": "description.txt",
"external_id": "description.txt"
}
}
]
Output:
-------
Source:
introduction.txt
Content:
Hello, world!
#####
Source:
description.txt
Content:
The sky is blue.