Nodes/ComfyUI-Pi-LLM/Pi Text Extractor
ComfyUI Node

Pi Text Extractor

The polite scrubber for chat-LLM output

By felixowens·Created 2 months ago·Updated 2 months ago· 0
Pi Text Extractor
    • text
    • found
    text
    extraction_modeauto
    xml_tagprompt
    fence_language
    start_delimiter
    end_delimiter
    occurrencefirst
    strip_whitespacetrue
    fail_if_missingfalse

    The node that un-wraps your LLM's present

    Chat models almost never emit what you asked for and nothing else. Ask one to "return only the prompt" and you get "Here is your enhanced prompt:" followed by a ```text code fence, or the prompt wrapped in <prompt>...</prompt> XML tags, or some other wrapper the model invented for politeness. Feed that straight into a CLIP Text Encode and the scaffolding becomes literal prompt tokens - the exact dirty-output failure mode that sinks naive LLM prompt-enhancer workflows. Pi Text Extractor is the regex cleaner that strips it.

    It's the companion to Pi LLM Text, but it doesn't care where the text came from. Any STRING in, cleaned STRING out. It ships in the same pack, and - useful detail - it's pure Python, so you don't even need the Pi CLI installed to use just this node.

    How it works

    Give it the LLM's raw output in text and pick an extraction_mode:

    • auto (default) tries an XML tag first, then a code fence, then custom delimiters - first match wins
    • xml_tag pulls from <prompt>...</prompt> (the tag name lives in the xml_tag field, default prompt)
    • code_fence pulls from triple-backtick blocks; leave fence_language empty to match any language, or set e.g. text to only match ```text fences
    • between_delimiters uses your own start_delimiter / end_delimiter markers

    Under the hood it's a small set of regexes with occurrence picking the first or last match, and strip_whitespace (default on) trimming the result. The smart default is fail_if_missing = false: if nothing matches, you get the original text back rather than a broken graph - a stale prompt format won't kill your queue, it just quietly passes through. Flip it on if you'd rather get a loud error than silently bad text.

    Two outputs come out: text (the cleaned string) and found (a BOOLEAN saying whether a wrapper was actually found). Wire found into a node that can branch on it if you want to catch the silent pass-through.

    The pair that works

    With Pi LLM Text, tell the model which wrapper to use:

    Return only the final prompt inside <prompt>...</prompt> tags.
    

    or:

    Return only the final prompt in a ```text code fence.
    

    Set xml_tag to prompt (already the default), connect the LLM node's text output here, and pipe this node's text into your CLIP Text Encode. If the LLM ever forgets the wrapper, fail_if_missing off means you still get something rather than a crash mid-batch.

    Install

    Same pack as the others - ComfyUI Manager (search "ComfyUI-Pi-LLM") or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/felixowens/ComfyUI-Pi-LLM.git
    

    Restart ComfyUI and it shows up under the Pi category. No model downloads, no Pi install, nothing heavier than ComfyUI's bundled Python. About the only thing that'll trip you up: if your custom delimiters look like regex metacharacters (say [[PROMPT]]), don't worry - the node escapes them for you. And if you ever get back your raw, unfiltered text when you expected a clean extract, check the found output first. That's the tell that the wrapper didn't match, not a bug.

    CategoryPi

    Inputs (9)

    NameTypeDefaultDescription
    textSTRING
    extraction_modeCOMBOauto4 options: auto, xml_tag, code_fence, between_delimiters
    xml_tagSTRINGprompt
    fence_languageSTRING
    start_delimiterSTRING
    end_delimiterSTRING
    occurrenceCOMBOfirst2 options: first, last
    strip_whitespaceBOOLEANtrue
    fail_if_missingBOOLEANfalse

    Outputs (2)

    NameTypeDescription
    textSTRING
    foundBOOLEAN