Nodes/ComfyUI-QHNodes/🐟Dynamic Expression
ComfyUI Node

🐟Dynamic Expression

Run a raw Python expression inside your workflow

By liuqianhonga·Created 2 years ago·Updated about a year ago· 5
🐟Dynamic Expression
  • arg1
  • arg2
  • arg3
  • arg4
  • arg5
  • *
expression# Example: # return arg1 + arg2

Every workflow eventually hits a spot where you need some small piece of logic ComfyUI doesn't have a node for - combine two strings a certain way, do arithmetic on a value, branch on a condition - and building a whole custom node for a one-liner is overkill. DynamicExpression is the escape hatch: you write a Python expression as text, feed it up to five arguments, and it runs.

How it works

You write an expression using return, referencing up to five inputs by name (arg1 through arg5), and the node executes it and passes the result along. The README's own examples show the range: simple arithmetic (return arg1 + arg2), string formatting (return f'Hello, {arg1} {arg2}!'), building a list or dict from the arguments, and conditional expressions (return 'Even' if arg1 % 2 == 0 else 'Odd'). It's genuinely arbitrary Python - there's no restricted subset here, it's a real expression evaluator.

The inputs and outputs that matter

  • expression (STRING, multiline, required) - your Python code, using return to produce a value. Defaults to a commented example rather than a real expression, so you'll always need to write your own.
  • arg1 through arg5 (all optional, type * - accepts anything) - the values your expression can reference by name. Leave unused ones unwired.

Output: a single, untyped result - whatever your return produces, which could be a number, string, list, or dict depending on what you wrote.

How to install it

Search ComfyUI-QHNodes in ComfyUI Manager, or:

cd ComfyUI/custom_nodes
git clone https://github.com/liuqianhonga/ComfyUI-QHNodes.git

Restart. Main-repo node, nothing extra to install.

Common issues & troubleshooting

Think about what you're running before you run someone else's workflow. This is the one node in the pack worth pausing on before you drag it into a graph you didn't build yourself. Custom nodes already run arbitrary Python with full access to your machine - that's how the ComfyUI_LLMVISION malware incident happened in 2024, where a node distributed through normal channels compromised machines on import, eventually leading to an FBI case. This node makes that same capability explicit and text-editable: if you load a shared workflow that includes a DynamicExpression node with an expression you didn't write, read it before you hit run, the same way you'd think twice before pasting an unfamiliar shell command.

Expression errors with no useful traceback. Since this is evaluating whatever text you typed, a syntax error or an undefined variable name (referencing arg3 when you only wired arg1 and arg2) will fail the node - double-check every argN your expression references is actually connected.

Return type doesn't match what the next node expects. Because the output is untyped (*), ComfyUI won't stop you from wiring a dict output into a node expecting a STRING - that mismatch surfaces as a runtime error downstream instead of a connection-time warning, so it's worth testing the expression in isolation (a simple text-display node after it) before wiring it deep into a longer chain.

Category🐟QHNodes

Inputs (6)

NameTypeDefaultDescription
expressionSTRING# Example: # return arg1 + arg2
arg1opt*
arg2opt*
arg3opt*
arg4opt*
arg5opt*

Outputs (1)

NameTypeDescription
**