Nodes/D2 Nodes ComfyUI/D2 Prompt Sanitizer
ComfyUI Node

D2 Prompt Sanitizer

Turn messy prompts (long_hair, ,, , double spaces) into clean tag soup

By da2el-ai·Created 2 years ago·Updated 5 days ago· 66
D2 Prompt Sanitizer
    • prompt
    prompt
    underscore_to_spacetrue
    space_after_commatrue
    remove_extra_commatrue
    protect_loratrue
    protect_scoretrue
    newline_modekeep
    remove_duplicate_tagsfalse
    strip_trailing_commafalse

    You know the feeling: you grab a prompt from a Danbooru tag dump, an old A1111 image, or a shared workflow, and it arrives with long_hair, , ,, cascades, commas glued to the next tag, and maybe a LoRA <lora:foo:1> in the middle of it all. D2 Prompt Sanitizer is the tidy-up pass. You drop it between your text source and the prompt encoder, and it normalizes the string without you hand-editing forty tags.

    It's a text-formatting node, not a generation node - feed it any string, get a cleaned string out. In the D2 ecosystem it slots right before D2 Prompt or D2 KSampler, but it works fine ahead of a plain CLIPTextEncode too.

    What it actually fixes

    Five toggles, all on by default, each doing one thing:

    • underscore_to_space - long_hairlong hair. This is the big one for tag-based prompts.
    • space_after_comma - normalizes whitespace around commas to a single ", " so a , b becomes a, b.
    • remove_extra_comma - collapses ,, / , , runs into one comma and strips commas at the start of a line.
    • protect_lora - leaves <lora:my_lora:1> untouched. Without this, the underscore conversion would mangle LoRA syntax.
    • protect_score - leaves Pony quality tags like score_9 / score_8_up intact. Same reason: those underscores are part of the tag name.

    The protection toggles are the part worth understanding. The sanitizer isn't just blindly replacing _ with spaces; it stashes <...> and score_N matches, does its comma/space work, then puts them back. So turning protect_lora off genuinely would corrupt your LoRA call - leave it on unless you know why you don't want it.

    The output

    One output, prompt - the cleaned string. That's it. Output node it is not; you keep wiring prompt into your encoder or D2 Prompt.

    The processing order matters if you're chasing a weird result: underscore conversion happens before comma cleanup, and protected text is restored last. So score_9, won't end up as score 9 , - the score tag gets stashed before any of the comma surgery.

    Installing

    Part of the D2-nodes-ComfyUI pack - install the pack, not the node.

    • ComfyUI Manager → search "D2-nodes-ComfyUI" → Install → restart.
    • Or cd ComfyUI/custom_nodes && git clone https://github.com/da2el-ai/D2-nodes-ComfyUI, then restart.

    Requirements are just piexif and charset-normalizer, no model files. Watch the pack's one big gotcha: v32.0.0+ requires the newer ComfyUI (V3 schema) - on an older install use a pre-32.0.0 release.

    Notes and gotchas

    This node won't add commas between tags that never had them - it fixes existing separators, it doesn't invent structure. If your input is 1girl long hair with spaces as separators, the result is 1girl long hair, not 1girl, long hair. Also: don't expect it to dedupe tags; that's a different toggle that lives in a newer version of the pack than the one you'll see listed here. If your prompts keep coming in with duplicate tags, pair the sanitizer with a dedupe pass in your prompt pipeline.

    For a string utility node it's genuinely the one I'd reach for - it's deterministic, off-by-default-friendly (every transform can be disabled), and it costs nothing to leave in the graph.

    CategoryD2/Text

    Inputs (9)

    NameTypeDefaultDescription
    promptSTRING
    underscore_to_spaceBOOLEANtrue
    space_after_commaBOOLEANtrue
    remove_extra_commaBOOLEANtrue
    protect_loraBOOLEANtrue
    protect_scoreBOOLEANtrue
    newline_modeCOMBOkeep5 options: keep, add_comma, to_comma, to_space, remove
    remove_duplicate_tagsBOOLEANfalse
    strip_trailing_commaBOOLEANfalse

    Outputs (1)

    NameTypeDescription
    promptSTRING