Nodes/ComfyUI-prompt-filter-nodes/Apply Multiple Prefixes to Keywords
ComfyUI Node

Apply Multiple Prefixes to Keywords

#character, @artist, in one pass

By GHOSTLXH·Created about a year ago·Updated 6 months ago· 1
Apply Multiple Prefixes to Keywords
    • STRING
    original_texthatsune miku, misaki kurehito, masterpiece, best quality, red dress
    filtered_keywords_listhatsune miku misaki kurehito
    prefixes# @

    If you prompt anime models with real Danbooru tags, you've probably felt the itch this node scratches: you have a big comma-separated prompt, you know which tag is the artist and which are characters, and you want to mark them so you can find, strip, or swap them later. Doing that by hand on every prompt is miserable. This node is the payoff - it takes your prompt, a list of keywords, and a list of prefixes, then stamps the right prefix onto each matching tag. hatsune miku becomes #hatsune miku, misaki kurehito becomes @misaki kurehito, and everything else stays untouched.

    It's the third node in a four-node pack (ComfyUI-prompt-filter-nodes) whose whole point is this marking workflow. The pack is tiny, pure Python, and has zero dependencies or model files - no GPU, no API keys, nothing to download. It just does string surgery on your prompt before it hits the CLIP encoder.

    How it works

    The mechanism is refreshingly dumb, in a good way. The filtered_keywords_list input is parsed one line at a time, and prefixes is parsed one line at a time, then they're zipped together positionally: line 1 of keywords gets line 1 of prefixes, line 2 gets line 2, and so on.

    Two details from the source are worth knowing:

    • A single keyword line can itself be comma-separated - every tag on that line gets the same prefix.
    • Matching is exact. The prompt is split on commas, each tag is stripped of whitespace, and a tag only gets a prefix if it matches a keyword line character-for-character. hatsune miku in the file won't touch a prompt tag that reads hatsune miku in a dress.

    After matching, the whole prompt is rebuilt with the prefixes prepended and handed back as a single string.

    The inputs that matter

    • original_text - your full prompt, the thing you're marking up.
    • filtered_keywords_list - the keywords to find, one per line. This is exactly what the pack's Filter Prompt by TXT File node outputs, which is the intended wiring.
    • prefixes - one per line, matched positionally to the keyword lines. The defaults give you the idea: # for characters, @ for artists.

    The single output (STRING) is the modified prompt. Wire it straight into a CLIP Text Encode node and you're done.

    Where people get burned

    The positional mapping is the trap. If your keyword list has three lines but your prefix list has two, the code takes the min() of the two and silently drops the third keyword line - it prints a warning to the console, but the workflow doesn't fail. Same thing in reverse: extra prefixes are ignored. If you're building the keyword list with another node, count your newlines. This is also why the pack ships Concatenate Strings (Multi): it exists to assemble a prefix list with the right number of lines so the pairing lines up.

    Also worth knowing: exact match means the keyword file and your prompt have to agree on spelling. A trailing period, an extra space, twintails vs twin_tails - no match, no prefix, and no error.

    Installing it

    It's on the Comfy Registry, so ComfyUI Manager handles it: search "ComfyUI-prompt-filter-nodes" (or just "prompt filter"), hit install, restart. Or clone it yourself:

    cd ComfyUI/custom_nodes
    git clone https://github.com/GHOSTLXH/ComfyUI-prompt-filter-nodes
    

    Then restart ComfyUI. There's no requirements.txt - the whole pack only imports Python's standard os module - so there's nothing to pip install. If it's not showing up after a restart, you cloned it into the wrong directory or ComfyUI didn't reload; the folder must be inside custom_nodes directly, not nested.

    Categorytext/filtering

    Inputs (3)

    NameTypeDefaultDescription
    original_textSTRINGhatsune miku, misaki kurehito, masterpiece, best quality, red dress
    filtered_keywords_listSTRINGhatsune miku misaki kurehito
    prefixesSTRING# @

    Outputs (1)

    NameTypeDescription
    STRINGSTRING