Nodes/DJZ-Nodes/Prompt Swap
ComfyUI Node

Prompt Swap

Swap 'man' for 'woman' and 'no humans' for 'many monsters' in one node

By MushroomFleet·Created 2 years ago·Updated 5 months ago· 78
Prompt Swap
    • STRING
    text
    target_wordsman, woman, "no humans"
    exchange_wordswoman, man, "many monsters"

    PromptSwap is the find-and-replace node for prompts, and it's the quiet hero of batch variation work. You give it a text, a list of target words, and a list of replacement words, and it swaps every occurrence - case-insensitively, whole-word where it matters, and with support for multi-word phrases in quotes. The defaults are a dead giveaway for how it's meant to be used: manwoman, womanman, "no humans""many monsters". It's a one-node gender-swap / scene-variation machine.

    Why is this so handy? Because the classic way to explore prompt variations is to change one or two tokens at a time and re-queue. With PromptSwap you can generate one base prompt, then use it to produce a whole family of variations - "woman" becoming "man," "soldier," "dancer" - and run them as a batch instead of editing text by hand between queues. It's prompt engineering without the copy-paste.

    How it works

    The parsing is the part that makes it better than a naive str.replace:

    • Targets and replacements are comma-separated, and anything in double quotes is treated as a single phrase ("no humans") rather than a word.
    • The two lists are zipped positionally: first target maps to first replacement, second to second, and so on.
    • Single words are matched whole-word (\bword\b) so "man" doesn't clobber "woman" or "human." Multi-word phrases are matched as exact phrases.
    • Longer targets are processed first, so overlapping phrases resolve predictably.
    • Matching is case-insensitive; replacements keep the case you typed.

    So man, woman / woman, man turns "a man and a woman" into "a woman and a man" cleanly, and quoted phrases let you swap entire expressions.

    The inputs that matter

    • text - the prompt (multiline).
    • target_words - comma-separated; use quotes for multi-word phrases.
    • exchange_words - comma-separated replacements, positionally matched to targets.

    Output: one STRING - the swapped prompt, ready for the encoder or another processing node.

    Installing it

    Part of DJZ-Nodes. ComfyUI Manager → Install Custom Nodes → search DJZ-Nodes → install → restart, or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/MushroomFleet/DJZ-Nodes
    cd DJZ-Nodes
    pip install -r requirements.txt
    

    Pure stdlib (re and logging) - this node doesn't pull in any of the pack's heavier dependencies. If the pack's installed, it works.

    Common issues

    Three things trip people up. First, the lists must line up positionally - if target_words has three entries and exchange_words has two, the third target simply isn't swapped, no error, so count your commas. Second, whole-word matching is a feature but has edges: "city" matches in "city" but not "cities," so plural forms won't swap unless you list them. Third, replacement case follows what you typed in exchange_words, not the matched text - a lowercase replacement will lowercase a capitalized word. And since matching is case-insensitive but replaces with your exact replacement string, "Man" matched by man becomes "woman" (lowercase) unless you typed "Woman" in the list. Check one output before running a 500-queue batch.

    CategoryCustom-Nodes

    Inputs (3)

    NameTypeDefaultDescription
    textSTRING
    target_wordsSTRINGman, woman, "no humans"
    exchange_wordsSTRINGwoman, man, "many monsters"

    Outputs (1)

    NameTypeDescription
    STRINGSTRING