Nodes/comfyui_CustomCodeNode/Custom Code (String)
ComfyUI Node

Custom Code (String)

String surgery mid-graph

By What-a-stupid-username·Created about a year ago·Updated about a year ago· 2
Custom Code (String)
    • string0
    I0
    F0.50
    AlwaysExecutefalse
    codestring0 = string0
    string0

    Prompts are just strings, and strings deserve a full language. Custom Code (String) (class CustomCodeNodeString) is the smallest, most obvious node in the comfyui-CustomCodeNode pack: one string in, your Python snippet, one string out. It's for the moments where a text box isn't enough - reformatting a prompt, stripping tags, building a filename, regex-surgery on whatever text is flowing through your workflow.

    It's the string-flavored sibling of Custom Code Node and Custom Code (Image). Same author, same "write the code instead of the plugin" philosophy, same install.

    How it works

    Your code runs via Python's exec(), with string0 available as a local variable holding whatever's wired into the input socket. The node reads string0 back out of the locals when the snippet ends and returns it. The default is the identity:

    string0 = string0
    

    which does nothing but proves the plumbing works. Change it to something real:

    string0 = ", ".join([t.strip() for t in string0.split(",") if t.strip()])
    

    and you've got a tag-cleaner. Or string0.replace("blurry", "sharp"), or f-string templating, or len(string0) written back as a string. The I and F inputs are in scope too, so you can parametrize the snippet from the graph without editing code.

    The inputs that matter

    • code - the snippet. It must assign string0 (or the node errors out).
    • string0 - the optional STRING input. Unwired, it arrives as None, so code like string0.replace(...) will crash; guard for it or wire something in.
    • AlwaysExecute - same caching story as the other two nodes. If your snippet uses time or randomness, or if it just doesn't seem to re-run when upstream text changes, this forces a run every queue pass.

    Installing it

    All three nodes come in one pack, so a single install gets you this one. ComfyUI Manager → search comfyui-CustomCodeNode, or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/What-a-stupid-username/comfyui-CustomCodeNode
    

    Restart ComfyUI. No dependencies to install, no models to download - the pack is pure stdlib.

    Common issues & troubleshooting

    "Error Executing Custom Code" with a traceback pointing at your code - you forgot to assign string0, or you called a method on None. The console shows the offending line with a caret; fix the snippet.

    Worth knowing: of the three nodes in this pack, the String one has the least reliable change-detection (the author's hashing here is noticeably sloppier than the image node's). If you ever see it not re-running when you expect it to, don't debug it - just set AlwaysExecute and move on.

    For prompt engineering on the fly it's a genuinely handy little utility. It won't organize your workflow for you, but when the graph needs a string transformed exactly one way, it beats adding another node pack for the privilege.

    CategoryCustomCodeNode

    Inputs (5)

    NameTypeDefaultDescription
    IINT0
    FFLOAT0.50
    AlwaysExecuteBOOLEANfalseIf True, the node will always execute, even if the input is not changed.
    codeSTRINGstring0 = string0
    string0optSTRING

    Outputs (1)

    NameTypeDescription
    string0STRING