Skip to main content

Prompt Template

Use the Prompt Template core component to create a prompt that supplies instructions and context to an LLM or agent, separate from other input like chat messages and file uploads.

Prompts are structured input that use natural language, fixed values, and dynamic variables to provide baseline context for the LLM. For example:

  • Define a consistent structure for user queries, making it easier for the LLM to understand and respond appropriately.
  • Define a specific output format for the LLM, such as JSON or structured text.
  • Define a role for the LLM, such as You are a helpful assistant or You are an expert in microbiology.
  • Allow the LLM to reference chat memory.

The Prompt Template component can also output variable instructions to other components later in the flow.

Prompt Template parameters

NameDisplay NameDescription
templateTemplateInput parameter. Create a prompt template with dynamic variables in curly braces, such as {VARIABLE_NAME}.

If your template includes literal text and variables, you can use double curly braces to escape literal curly braces in the template and prevent interpretation of that text as a variable. For example: This is a template with {{literal text in curly braces}} and a {variable}.

If your template contains many literal curly braces, such as JSON structures, consider using Mustache templating instead. For more information, see Use Mustache templating in prompt templates.

use_double_bracketsUse Double BracketsWhen enabled, use Mustache syntax {{variable}} instead of f-string syntax {variable}. For more information, see Use Mustache templating in prompt templates.

Define variables in prompts

Variables in a Prompt Template component dynamically add fields to the Prompt Template component so that your flow can receive definitions for those values from other components, Langflow global variables, or fixed input.

For example, with the Message History component, you can use a {memory} variable to pass chat history to the prompt. However, the Agent component includes built-in chat memory that is enabled by default. For more information, see Memory management options.

The following steps demonstrate how to add variables to a Prompt Template component:

  1. Create a flow based on the Basic prompting template.

    This template already has a Prompt Template component, but the template only contains natural language instructions: Answer the user as if you were a GenAI expert, enthusiastic about helping them get started building something fresh.

    This prompt defines a role for the LLM's chat interactions, but it doesn't include variables that help you create prompts that adapt dynamically to changing contexts, such as different users and environments.

  2. Click the Prompt Template component, and then add some variables to the Template field.

    Variables are declared by wrapping the variable name in curly braces, like {variable_name}. For example, the following template creates context and user_question variables:


    _10
    Given the context:
    _10
    _10
    {context}
    _10
    _10
    Answer the question:
    _10
    _10
    {user_question}

    If your template includes literal text and variables, you can use double curly braces to escape literal curly braces in the template and prevent interpretation of that text as a variable. For example: This is a template with {{literal text in curly braces}} and a {variable}.

    If your template contains many literal curly braces, such as JSON structures, consider using Mustache templating instead. For more information, see Use Mustache templating in prompt templates.

  3. Click Check & Save to save the template.

    After adding the variables to the template, new fields are added to the Prompt Template component for each variable.

  4. Provide input for the variable fields:

    • Connect the fields to other components to pass the output from those components to the variables.
    • Use Langflow global variables.
    • Enter fixed values directly into the fields.

You can add as many variables as you like in your template. For example, you could add variables for {references} and {instructions}, and then feed that information in from other components, such as Text Input, URL, or Read File components.

Use Mustache templating in prompt templates

F-string escaping can become confusing when you mix escaped braces with variables in the same template. For example:


_10
Generate a response in this JSON format:
_10
{{"name": "{name}", "age": {age}, "city": "{city}"}}
_10
_10
The user's name is {name}, age is {age}, and they live in {city}.

The characters {{ and }} are escaped literal braces for the JSON structure, but {name} is a variable. This can make prompts error-prone and difficult to parse. Use Mustache in your prompt templates to make the differences clearer.

To enable Mustache templating, do the following:

  1. In the Prompt Template component, enable Use Double Brackets.

  2. In your prompt template, change the variables from {variable} to {{variable}}. Mustache uses { } for literal braces and {{variable}} for variables.


    _10
    Generate a response in this JSON format:
    _10
    {"name": "{{name}}", "age": {{age}}, "city": "{{city}}"}
    _10
    _10
    The user's name is {{name}}, age is {{age}}, and they live in {{city}}.

  3. Click Check & Save. The component lints the template code and returns Prompt is ready if there are no errors. Your prompt is now ready to use in a flow.

Langflow supports variable replacement with double brackets, but does not support the full Mustache engine. The prompt component validation rejects syntax for other Mustache features such as loops and conditionals.

See also

Search