Nodes/ComfyUI-AI-Assistant/Auto Prompt Sorting
ComfyUI Node

Auto Prompt Sorting

Your Pasted Prompt Is Full of Duplicates — This Node Fixes That in One Pass

By JackEllie·Created 2 years ago·Updated 2 years ago· 24
Auto Prompt Sorting
    • string
    string
    remove_duplicate_tagstrue

    If you run Danbooru-trained models like Illustrious or NoobAI, you know the drill: you paste a prompt from Civitai or a saved A1111 file and it's a wall of comma-separated tags with "masterpiece" in there twice, triple spaces where a line break used to be, and a trailing comma the model dutifully reads as noise. Auto Prompt Sorting is the first thing you should drop in front of your positive prompt text encoder. It's not magic - it's a tidy-up pass, and that's exactly what a messy pasted prompt needs.

    Don't over-read the "Sorting" in the name. It does not alphabetize your tags. What it actually does is normalize the punctuation and optionally strip exact duplicates. Both are worth having, because on tag-based models token budget is real and duplicate tags don't help you - the model already saw the concept, a second copy just eats tokens and slightly dilutes the signal.

    How it works

    Under the hood it runs a small regex cleanup loop, then an optional dedupe:

    • Collapses every run of commas, spaces, tabs and newlines down to a single comma, and fixes double spaces.
    • Trims stray leading/trailing commas and whitespace.
    • If remove_duplicate_tags is on, it walks the tag list left to right and keeps the first occurrence of each tag. So a tag repeated near the front wins - which is what you want, since tag position roughly encodes emphasis.

    Everything is case-sensitive and exact-match. blue_hair and Blue_Hair are different tags to it, and 1girl won't be deduped against 1 girl. If you've got case-inconsistent prompts, run them through it anyway - it still fixes the whitespace, and the leftover near-duplicates are usually intentional variants.

    One thing it deliberately leaves alone: BREAK tokens. The source has the BREAK-normalization code commented out, so any BREAKs you've used for section weighting pass through untouched. Good news if you lean on BREAK to structure long prompts; just know the node isn't touching them.

    The inputs that matter

    • string (multiline) - your raw prompt. You can type it in or wire it from another text node.
    • remove_duplicate_tags (boolean, default on) - flip this off if your prompt is short enough that you want even repeats kept verbatim. Realistically you leave it on.

    It has one output, also confusingly named string. That's your cleaned prompt, so wire it into the text input of a CLIP Text Encode node and you're done.

    Installing it

    This ships in the JackEllie/ComfyUI_AI_Assistant pack - a small fork of tori29umai0123's AI-Assistant that adds two preprocessors and a handful of prompt tools. In ComfyUI Manager, search "ComfyUI-AI-Assistant" and install. Manual install works too:

    cd ComfyUI/custom_nodes
    git clone https://github.com/JackEllie/ComfyUI_AI_Assistant
    

    Then restart ComfyUI. No models to download - this node is pure text processing, nothing touches your GPU. The pack's requirements.txt only lists opencv-python, though the module does import skimage at the top, so if the whole pack fails to load with a ModuleNotFoundError for skimage, that's the culprit: pip install scikit-image.

    Gotchas

    The only real failure mode is feeding it non-text - an empty string just returns an empty string, and a list of tags gets joined into one string first, so it handles most things gracefully. About the only thing that will surprise you is the case-sensitivity mentioned above. If you want smarter cleanup that understands near-duplicates or removes whole tag families by keyword, the same pack's Clean Prompt Tags and Prompt Blacklist nodes cover those cases - this one is just the fast, dumb whitespace-and-duplicate pass you run first.

    Categoryadvanced/AI_Assistant/text

    Inputs (2)

    NameTypeDefaultDescription
    stringSTRING
    remove_duplicate_tagsBOOLEANtrue

    Outputs (1)

    NameTypeDescription
    stringSTRING