Python String Function
Run a tiny sandboxed Python function inside your workflow
- output
Sometimes the string you want simply doesn't exist as a node. You need to uppercase something with a twist, strip a prefix, reorder a CSV, or compute a filename. Writing a whole custom node for that is overkill; the Python String Function is the escape hatch. You paste a short Python function body into the node, it runs in a sandbox, and the string it returns comes out the output.
The name matters: it's a string function. You write the body of a function, it gets whatever's in the optional a, b, c inputs, and it returns a string. That's the whole contract.
How it works
The node wraps your code in a function like this:
def _string_function(a, b, c):
<your code here>
_result.append(_string_function(_a, _b, _c))
It runs inside an exec with a trimmed builtins namespace, and a few ground rules you need to know before you write anything clever:
- No imports. The string
importanywhere in your code raises an error immediately. The sandbox does pre-loadrandom,re,json, andnumpyfor you, which covers a lot. - No
eval/exec(they're stripped from the builtins) and no filesystem or network access beyond what's handed in. a,b,care optional string inputs you can wire from anywhere. They arrive as strings; your function does the parsing.- It returns whatever your function returns, coerced to a string.
So a valid minimal example is the default: return "a text". A realistic one:
a = a or "none"
return f"Hello {a} - {len(b)} chars"
There's one more wrinkle worth flagging: the node ships a safe_get/safe_post pair in the sandbox, but they only allow requests to a single hardcoded host (api.naga.ac), so don't plan on it being a general HTTP bridge - it's a narrow, opinionated feature aimed at one API.
The inputs that matter
- python_code - the function body. Multiline. No
importallowed. - a, b, c - optional string parameters, each force-connectable from any string output.
- output - the returned string.
Why you'd reach for it
Use it for string surgery that would take three chained text nodes: collapsing whitespace, re-formatting a number list, building a dynamic filename with re, munging a CSV line with json. If you're doing heavy text logic across a whole workflow, this node can collapse a forest of concat nodes into one readable function.
The honest limits: it's for strings only - no tensors, no images, no side effects. And "sandboxed" here means no imports and no network, not "safe against malicious input" - you're running arbitrary Python in your own ComfyUI process, so don't paste code you don't understand. One subtle gotcha: it re-runs on every queue execution (there's no caching keyed on your code), so if your function is slow or non-deterministic, expect it to re-execute each run.
Install
Ships with Dados Nodes: ComfyUI Manager → "Dados Nodes", or
cd ComfyUI/custom_nodes
git clone https://github.com/dadoirie/ComfyUI_Dados_Nodes.git
cd ComfyUI_Dados_Nodes
pip install -r requirements.txt
then restart. It does use the requests package (via the sandbox's safe-request helpers), which you'll likely already have; pip install -r requirements.txt covers the pack's needs.
Common issues
The most common failure is forgetting the no-import rule and pasting code that starts with import os - the node rejects it by design, not because you broke it. Next most common: forgetting that a, b, c arrive as strings, so a + 1 fails with a type error. And if your function returns nothing, the output is an empty string rather than an error - which can silently feed empty text downstream. Return something, even if it's "", and you'll keep your sanity.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| python_code | STRING | return "a text" | Python code to execute (imports not allowed for security) |
| seed | INT | 00–18446744073709550000 | Seed for wildcard randomization |
| aopt | STRING | Optional string parameter a | |
| bopt | STRING | Optional string parameter b | |
| copt | STRING | Optional string parameter c |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| output | STRING | — |