ComfyUI-Complex-Prompt
ComfyUI custom nodes for building complex text prompts with dynamic variants, workflow variables, JSON-imported variables, and basic conditional logic.
Nodes
Complex Prompt:
- Name:
Complex Prompt - Class:
ArtemKo7vComplexPrompt - Description: expands a prompt with
dynamicprompts, then replaces$variable_nametokens fromComplexPromptVars - Category:
ArtemKo7v - Output string:
STRING
Complex Prompt Set Variable:
- Name:
Complex Prompt Set Variable - Class:
ArtemKo7vComplexPromptSetVariable - Description: creates or updates a
ComplexPromptVarsobject with an optional condition - Category:
ArtemKo7v - Output vars:
ArtemKo7vComplexPromptVars - Output wasSet:
BOOLEAN
Complex Prompt Parse JSON:
- Name:
Complex Prompt Parse JSON - Class:
ArtemKo7vComplexPromptParseJSON - Description: creates or extends
ComplexPromptVarsfrom a JSON object - Category:
ArtemKo7v - Output vars:
ArtemKo7vComplexPromptVars
Complex Prompt Empty String:
- Name:
Complex Prompt Empty String - Class:
ArtemKo7vComplexPromptEmptyString - Description: returns an empty string for workflow branches that need an explicit blank text input
- Category:
ArtemKo7v - Output string:
STRING
Features
- Expands dynamic prompt syntax through
dynamicprompts, including variants such as{red|green|blue} - Preserves
$variable_nametokens whiledynamicpromptsparses the prompt - Replaces variables after dynamic prompt expansion
- Passes
ComplexPromptVarsbetween nodes - Creates variables from multiline text
- Optionally trims variable values before storing them
- Supports conditional variable assignment with
&&,||,!, comparisons, andint,float,str - Imports variables from JSON strings
- Selects one random item from JSON arrays of strings or numbers
- Supports deterministic randomness through
seed, with ComfyUI randomizing seeds after generation - Leaves unknown
$variable_nametokens unchanged in final prompts - Avoids calling
dynamicpromptsfor plain text that has no dynamic prompt syntax
Installation
Place this folder into your ComfyUI custom_nodes directory.
Install dependencies in the same Python environment used by ComfyUI:
pip install -r requirements.txt
Dependencies:
dynamicprompts>=0.31.0simpleeval>=1.0.7
Restart ComfyUI after installing or updating the node.
Prompt Syntax
Dynamic prompt variants use dynamicprompts syntax:
a {red|green|blue} car
This outputs one selected variant, such as:
a green car
Variables use $variable_name:
$person in a {red|green|blue} car
If person exists in vars, $person is replaced with its value after dynamic prompt expansion. If it does not exist, $person remains unchanged.
Variable tokens match this pattern:
$[A-Za-z_][A-Za-z0-9_]*
Inputs
Complex Prompt:
prompt: multiline text promptseed: seed passed todynamicpromptsvars: optionalComplexPromptVars
Complex Prompt Set Variable:
variable_name: key to store invarsvalue: multiline text; dynamic prompt syntax is expanded before storagecondition: optional expression; empty condition always stores the variableseed: seed passed todynamicpromptstrim: removes whitespace, newlines, and tabs from the beginning and end of the final value before storagevars: optional existingComplexPromptVars
Complex Prompt Parse JSON:
json_text: JSON object whose keys become variable namesseed: seed used when choosing random values from arraysvars: optional existingComplexPromptVars
Complex Prompt Empty String:
- No inputs
Outputs
Complex Prompt:
string: final prompt text after dynamic expansion and variable replacement
Complex Prompt Set Variable:
vars: new or updatedComplexPromptVarswasSet:truewhen the variable was stored,falsewhen the condition prevented the update
Complex Prompt Parse JSON:
vars: new or updatedComplexPromptVars
Complex Prompt Empty String:
string: empty string
Conditions
Conditions are evaluated with simpleeval. $variable tokens are resolved from the input vars.
Example:
(($var1 == "1") && ($var2 == "2")) || ($var3 < 10)
Supported convenience syntax:
$nameis normalized toname&&becomesand||becomesor!becomesnot, except in!=
Available functions:
int(value)float(value)str(value)
Missing variables make the condition evaluate to false. Numeric comparisons are attempted as numbers first; if conversion fails, values are compared as strings.
JSON Variables
Complex Prompt Parse JSON accepts a JSON object:
{
"person": "Ada",
"color": ["red", "green", "blue"],
"age": 37
}
This can produce variables such as:
person = Ada
color = green
age = 37
Supported JSON values:
- Strings
- Numbers
- Non-empty arrays containing only strings or numbers
Ignored JSON values:
- Booleans
null- Objects
- Empty arrays
- Arrays containing unsupported values
json_text must be a JSON object. Invalid JSON and non-object JSON raise an error.
Usage Examples
Create variables, then generate a final prompt:
Set Variable:
variable_name = person
value = {Ada|Grace|Katherine}
Set Variable:
variable_name = vehicle
value = {car|train|airship}
Complex Prompt:
$person near a {red|green|blue} $vehicle
Possible output:
Grace near a blue train
Use JSON as a variable source:
{
"subject": ["robot", "astronaut", "architect"],
"style": "cinematic",
"count": 3
}
Prompt:
$count $subject portraits, $style lighting
Conditionally set a variable:
variable_name = mood
value = dramatic
condition = ($style == "cinematic") && ($count >= 2)
Trim whitespace before storing a variable:
variable_name = person
value =
Ada Lovelace
trim = true
Configuration
The configuration file is stored at:
ComfyUI/user/default/ComfyUI-Complex-Prompt/config.json
The file is created automatically. The current default configuration is empty and does not expose user-facing settings yet.
Notes
- Dynamic prompt expansion runs before variable replacement.
Complex Prompt Set Variablealso expands dynamic prompt syntax invaluebefore storing it.- When
trimis enabled, trimming happens after dynamic prompt expansion and variable replacement. - If
valueis unset, it is stored as an empty string. - Unknown variables remain visible as
$variable_nameinstead of being removed. - Variable names are not currently rejected at input time, but only names matching
[A-Za-z_][A-Za-z0-9_]*can be referenced with$variable_name. - Restart ComfyUI after changing code or installing dependencies.