Files
dify-docs/en/guides/workflow/variables.mdx
2025-11-11 17:46:15 +08:00

171 lines
8.0 KiB
Plaintext

---
title: Variables
---
## Introduction
A variable is a labeled container that stores information in your Workflow or Chatflow. Each variable holds a piece of data—whether it's user input, system-generated values, or outputs from previous nodes. When you need to use this information later, you simply reference it by its name.
You'll work with different types of variables, each serving a specific purpose in your app's data flow.
## Variable Types
### Input Variable
- **User input variable**: Collects information from end users when they run your app, defined in a User Input node. For example, a user might provide a name, upload a file, or enter a question for the app to process.
<Tip>
For more about user input variables, see [User Input](/en/guides/workflow/node/user-input).
</Tip>
- **Node input variable**: Defines the data a node receives to perform its task, usually an output variable of another node. For example, the text for an LLM node to process, a query for a Knowledge Retrieval node to search for, or a conditional value for an IF/ELSE node to evaluate.
### Output Variable
- **Node output variable**: Holds the results generated by a node after it completes its task, available for downstream nodes to use as input.
- **Application output variable**: Returns information to the end user, specified in an [Output node](/en/guides/workflow/node/output) (for Workflows) or an [Answer node](/en/guides/workflow/node/answer) (for Chatflows).
### System Variable
System variables are system-defined, globally available parameters.
<Tabs>
<Tab title="Workflow">
| Variable Name | <div style={{width: '70px'}}>Data Type</div> | Description |
|:----------------|:-----------|:-------------|
| `sys.user_id` | String | A unique ID assigned to each user running the app. |
| `sys.app_id` | String | The unique ID of the app. |
| `sys.workflow_id` | String | The unique ID of the workflow. |
| `sys.workflow_run_id` | String | A unique ID generated for each workflow execution. |
| `sys.timestamp` | String | The timestamp indicating when each workflow execution started. |
</Tab>
<Tab title="Chatflow">
| Variable Name | <div style={{width: '70px'}}>Data Type</div> | Description |
|:----------------|:-----------|:-------------|
| `sys.conversation_id` | String | A unique ID for the conversation session. <br/><br/>It groups all related messages under the same conversation so the LLM can maintain topic continuity and context awareness throughout the chat. |
| `sys.dialogue_count` | Number | The total number of dialogue turns in the user's interaction with the chatflow. <br/><br/>Starting from 1, it increments after each round and can be used with an IF/ELSE node to create conditional logic.<br/><br/>For example, you can trigger a LLM-generated summary or analysis when `sys.dialogue_count` reaches a certain number. |
| `sys.user_id` | String | A unique ID assigned to each user running the app. <Note> Conversations are not shared between the web app and backend service APIs. A user with the same `sys.user_id` will have separate conversation histories across different interfaces.</Note>|
| `sys.app_id` | String | The unique ID of the app. |
| `sys.workflow_id` | String | The unique ID of the workflow. |
| `sys.workflow_run_id` | String | A unique ID generated for each chatflow execution. |
</Tab>
</Tabs>
### Environment Variable
Environment variables store sensitive information—such as API keys, database passwords, or other credentials—used in your app.
Instead of hardcoding these values directly into your app, you define them once as environment variables and reference them as needed. This separates sensitive data and the app itself, so you won't risk exposing passwords and keys when sharing your app's DSL.
<Info>
Once set, environment variables cannot be updated during workflow execution.
</Info>
<img src="/images/environment_variable.png" alt="Environment Variable" width="563" />
Supported data types:
- **String**: Text values like API endpoints or usernames.
- **Number**: Numeric values such as IDs, rate limits, or port numbers.
- **Secret**: Sensitive values like passwords or API keys. Secret variables are masked in the interface after saving.
<img src="/images/environment_variable_pwd.png" alt="Secret as Environment Variable" width="563" />
### Conversation Variable
<Info>
Available for Chatflows only.
</Info>
Conversation variables serve as short-term memory for a single chat session. They allow you to store and access dynamic information across multiple dialogue turns within the same conversation.
<Note>
Conversation variables are reset when a new conversation begins.
</Note>
Unlike environment variables that are read-only during execution, conversation variables are read-write. Their values can be updated using a [Variable Assigner](/en/guides/workflow/node/variable-assigner) node as the conversation progresses.
This enables you to track and manage stateful data such as user preferences, accumulated data (e.g., shopping cart items, to-do lists, token cost), or contextual details that the LLM needs to remember and reference consistently throughout the chat.
For example, you can use a conversation variable to store users' language preferences from their first message and ensure the LLM responds in that same language for the rest of the conversation.
<img src="/images/conversation_variable.png" alt="Conversation Variable" width="563" />
<div>
<p style={{ marginBottom: '1px' }}>Supported data types:</p>
<div style={{ display: 'flex', gap: '1rem' }}>
<ul>
<li>String</li>
<li>Number</li>
<li>Object</li>
<li>Boolean</li>
</ul>
<ul>
<li>Array[string]</li>
<li>Array[boolean]</li>
<li>Array[number]</li>
<li>Array[object]</li>
</ul>
</div>
</div>
## Reference Variables
Once a variable is defined, it can be referenced among nodes to pass data throughout your workflow.
You can reference variables in two ways:
- In an **input field**: Click and select the target variable from the dropdown.
- In a **text field**: Type `/` or `{` and select the target variable from the dropdown.
<Info>
A node can only access variables from nodes within its own execution branch. For example, a node in the **If** branch of an **If/Else** node cannot reference variables from the **Else** branch.
</Info>
<Columns cols={2}>
<Frame caption="Reference Variable in Input Field">
<img src="/images/reference_variable_in_input_field.png" alt="Reference Variable in Input Field" width="400"/>
</Frame>
<Frame caption="Reference Variable in Text Field">
<img src="/images/reference_variable_in_text_field.png" alt="Reference Variable in Text Field" width="400"/>
</Frame>
</Columns>
## Variable Inspect
![Variable Inspect](/images/variable_inspect.png)
After you test-run the entire workflow or a single node, the generated values for all related variables are automatically cached. View these cached values in the **Variable Inspect** panel by clicking **View cached variables** at the bottom of the canvas.
In the **Variable Inspect** panel, you can:
- **Centrally inspect** all available variables along with their data types and last-run values.
- **Directly edit** a variable's cached value without re-running the corresponding node or the entire workflow. This is useful for testing how a specific node responds to different inputs—simply modify the variable value and re-run the target node.
<Info>
- Editing a variable value in **Variable Inspect** does not alter the corresponding node's last-run logs. It only provides temporary input for testing purposes.
- Modified variables can be reverted to their original cached values.
</Info>
- **Quickly clear** a single variable's cached value or clear all cached values at once.
{/*
Contributing Section
DO NOT edit this section!
It will be automatically generated by the script.
*/}
---
[Edit this page](https://github.com/langgenius/dify-docs/edit/main/en/guides/workflow/variables.mdx) | [Report an issue](https://github.com/langgenius/dify-docs/issues/new?template=docs.yml)