Nodes/ComfyUI-Info-Prompt-Toolkit/Flatten Prompt for Caption
ComfyUI Node

Flatten Prompt for Caption

Strip the weights, keep the words

By kinorax·Created 5 months ago·Updated about a month ago· 2
Flatten Prompt for Caption
    • string
    string

    Flatten Prompt for Caption takes a prompt full of (emphasis:1.3), (parentheses), and escaped \(weird\) characters, and turns it into clean plain text you'd actually want to save in a caption file. One string in, one string out, no settings - it's the node you reach for when the prompt you generated with is not the prompt you want to train on.

    What it actually does

    Your working prompt is covered in syntax: (masterpiece:1.2), (detailed face:0.9), a \(cat\) in a park - where the parentheses and colons are instructions to the sampler, not content. Caption files are the opposite: they're training or tagging text where (cat:0.9) is just noise that teaches the model the wrong associations. This node removes the emphasis syntax and weight values, then unescapes the escaped parentheses and backslashes - so the literal \(cat\) becomes (cat) as content, and the weighted (masterpiece:1.2) becomes masterpiece.

    Under the hood it parses the string properly rather than doing a dumb regex strip: it tracks parenthesis depth so a real (cat) in the text survives while weighted emphasis gets removed, and it handles backslash escapes so you don't lose a literal character the sampler was treating as punctuation. That's the difference between "clean" and "destroyed."

    Where it fits

    The pack's caption pipeline is: tagger output or handwritten captions plus prompt text, merged and cleaned, then written as same-name .txt files alongside images (via Image Saver with write_caption, or Caption File Saver). Flatten Prompt for Caption sits at the front of that chain - take the positive prompt that just produced a great image, flatten it, and you have a caption that describes the image without the sampler's annotations. The pack also has sibling caption tools (Merge Caption Tokens, Remove Caption Tokens) to tidy the result further, but flattening the prompt is the step people most often forget.

    The one input

    Just string - the prompt text. Output is string, the flattened version. No threshold, no mode toggle, no surprises.

    The honest caveats

    It doesn't know what a "good" caption is - it only removes sampler syntax. Your (masterpiece:1.2), (best quality) flattens to masterpiece, best quality, which is exactly what you want if you're captioning Illustrious-style training data, and exactly what you don't want if you're captioning for a model that shouldn't have quality tags at all. Also, if your prompt uses unusual syntax like numbered steps or Dynamic-Prompts alternatives, the flattening is conservative - it's built for standard emphasis syntax, and fancier constructs will pass through. That's usually the right failure mode.

    Installing it

    Ships in the ComfyUI-Info-Prompt-Toolkit pack. Via ComfyUI Manager - search "Info Prompt Toolkit" - or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/kinorax/comfyui-info-prompt-toolkit.git
    cd comfyui-info-prompt-toolkit
    pip install -r requirements.txt
    

    Restart ComfyUI. No model downloads. Small single-author pack (GPL-3.0), modest footprint - but the niche it fills, prompt-to-caption without the sampler's syntax, is a real gap in the standard install.

    CategoryInfo-Prompt-Toolkit/Prompt

    Inputs (1)

    NameTypeDefaultDescription
    stringoptSTRINGFlatten prompt text for caption files by removing emphasis syntax and unescaping backslashes

    Outputs (1)

    NameTypeDescription
    stringSTRING