Nodes/ComfyUI illumorae/Text Truncate Protect
ComfyUI Node

Text Truncate Protect

Cut text down to size without slicing a word or a bracket in half

By CorvaeOboro·Created 8 months ago·Updated 6 days ago· 1
Text Truncate Protect
    • truncated_text
    • statistics
    • final_length
    • truncated_chars
    text
    character_limit500
    threshold50
    protect_wordsfalse
    protect_paragraphsfalse
    protect_enclosuresfalse
    enclosure_typesall

    A blunt character-limit truncation is fine for most text and quietly destructive for prompts: it cuts mid-word, and worse, it can slice straight through an open parenthesis or a <lora:...> tag, leaving you with a prompt that's both shorter and syntactically broken. Text Truncate Protect, from the illumorae pack (CorvaeOboro's text tools), truncates to a character limit with guardrails - it can hold word boundaries, paragraph boundaries, and enclosure boundaries, so the text that comes out is still valid prompt-shaped text.

    Where does that matter in practice? Two places. Keeping a prompt under a hard character budget (a filename limit, or the budget of a model with a small context - pair it with Text Token Count from the same pack to know the real number). And sanitizing auto-assembled prompts: if a script concatenates tags and blows past a limit, you want the trim to respect the structure, not destroy it.

    How it works

    The node truncates to character_limit (default 500) with a threshold (default 50) that sets how far it's allowed to search for a safe boundary near the limit. Three independent protections:

    • protect_words - truncate at a space/newline, preserving whole words (hyphenated words included).
    • protect_paragraphs - truncate only at newline boundaries.
    • protect_enclosures - never cut inside an enclosure pair; enclosure_types (all, parentheses, braces, brackets, angle_brackets) picks which pairs count. This is the one that keeps (cat:1.3) and <lora:...> intact.

    If no safe point exists inside the threshold window, it picks the closest legal one it can find. The stats come out the other end so you don't have to guess.

    Inputs and outputs

    • text - multiline input.
    • character_limit, threshold - the budget and the search slack.
    • The three protection toggles plus enclosure_types.

    Outputs: truncated_text (STRING), statistics (STRING describing what happened), final_length (INT), and truncated_chars (INT). The two integers are the genuinely useful part - wire truncated_chars somewhere or just read it to know how much you lost.

    Installing it

    ComfyUI Manager → search "illumorae" → install, or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/CorvaeOboro/ComfyUI_illumorae
    

    Restart. No models, no downloads.

    Troubleshooting

    If output is longer than your character_limit, that's the protection doing its job - it walked to the nearest safe boundary beyond the cutoff, which can overshoot by up to the threshold. Lower the threshold if you want a tighter fit. And a heads-up: enclosure protection only helps if your enclosures are actually balanced; an unclosed paren still gets treated as an opening. Enable protect_enclosures and check the statistics output the first few times - it tells you exactly where it cut and why.

    Categoryillumorae

    Inputs (7)

    NameTypeDefaultDescription
    textSTRING
    character_limitINT5001–100000
    thresholdINT500–1000
    protect_wordsoptBOOLEANfalse
    protect_paragraphsoptBOOLEANfalse
    protect_enclosuresoptBOOLEANfalse
    enclosure_typesoptCOMBOall5 options: all, parentheses, braces, brackets, angle_brackets

    Outputs (4)

    NameTypeDescription
    truncated_textSTRING
    statisticsSTRING
    final_lengthINT
    truncated_charsINT