Nodes/ComfyUI-Prompt-Iterator/Prompt Template Iterator
ComfyUI Node

Prompt Template Iterator

Prompt Template Iterator

By mingchoi·Created 8 months ago·Updated 8 months ago· 1
Prompt Template Iterator
    • prompt
    • index
    keywordscat dog bird
    templateA photo of a {keyword}

    You finally got a prompt that does what you want. Now you need the same shot with fifteen different subjects, and you are not going to hand-edit and re-queue it fifteen times. That's the exact moment Prompt Template Iterator exists for: you write the prompt once with a {keyword} hole in it, type your subjects into the node, and every time you hit Queue it fills the hole with the next one. No copy-paste, no counting your queue runs by hand.

    It's one of two nodes in mingchoi/ComfyUI-Prompt-Iterator, a tiny MIT-licensed utils pack. The sibling, Prompt Iterator, cycles whole multi-line prompts; this one keeps the template fixed and swaps only the keyword. It's the one you reach for when your variation is a single slot - "a photo of a {keyword} in a garden" is literally the default template - rather than a full prompt rewrite.

    How it actually works

    Under the hood it's almost embarrassingly simple, and that's the appeal. keywords is a multi-line string; each line gets stripped, and blank lines are thrown away. On every run the node takes a module-level counter, does counter % len(keywords) to pick the current line with wraparound, and then runs a plain template.replace("{keyword}", current_keyword). That's the whole mechanism - no API, no model files, nothing to download.

    Two details make the simplicity work. First, the node overrides IS_CHANGED to return the current counter value, which tells ComfyUI "my output changed, re-run me" on every execution. Without that, ComfyUI's node caching would see identical inputs and skip the node, and you'd get the same image forever. Second, the counter is global to the whole pack, not per node - more on why that matters below.

    The inputs and outputs that matter

    Only two inputs, both plain strings:

    • keywords (STRING, multiline) - one keyword per line. This is the list you're sweeping through.
    • template (STRING) - your prompt with a literal {keyword} placeholder. Note it's the exact text {keyword} - the node does a string replace, not Python f-string interpolation, so {cat} or {keyword:^10} won't work.

    The two outputs:

    • prompt (STRING) - the filled template. Wire this into a CLIP Text Encode node (or your model's text-encoder node) and you're done.
    • index (STRING) - a display string like "1/3", not a number. It's for watching your progress, so don't feed it to something expecting an INT.

    Installing it

    Install from ComfyUI Manager - search for "ComfyUI-Prompt-Iterator" - or do it by hand:

    cd ComfyUI/custom_nodes/
    git clone https://github.com/mingchoi/ComfyUI-Prompt-Iterator.git
    

    Then restart ComfyUI. There's no requirements.txt and no model download: the whole pack is one pure-Python file, which makes it a refreshingly safe custom node to install in an ecosystem where that's not always true.

    Gotchas worth knowing

    • It advances one step per queue run. Hitting Queue once runs the graph once and increments the counter. To sweep all fifteen keywords you queue fifteen times - manually, via the API, or by whatever loop you're using for overnight batches. There's no "generate all" built in.
    • The counter is shared with the sibling Prompt Iterator node. If both are in the same workflow they advance in lockstep, both at index 1, then both at 2. The README sells this as a feature; for two genuinely independent iterators you need a different tool.
    • Editing resets the counter. Change the keywords or the template and it starts back at 0. That's usually exactly what you want, but it means you can't trim the list mid-sweep and keep your place.
    • Restarting ComfyUI resets it too - the counter lives in a Python global, not in your workflow save. Reopening a workflow doesn't lose your place, but restarting the server does.

    Worth knowing before you pick this over the wildcard/bracket trick ({cat|dog|bird} in a single prompt, common in A1111-era workflows): wildcards pick per run and let every combo happen; this gives you ordered, sequential iteration where each subject appears exactly once. For a deliberate sweep of known subjects, the iterator wins. For happy accidents, use wildcards.

    Categoryutils

    Inputs (2)

    NameTypeDefaultDescription
    keywordsSTRINGcat dog bird
    templateSTRINGA photo of a {keyword}

    Outputs (2)

    NameTypeDescription
    promptSTRING
    indexSTRING