Nodes/ComfyUI/Text Length
ComfyUI Node Runs on cloud

Text Length

Count the characters and actually use the number

By Comfy-Org·Created 4 years ago·Updated about an hour ago· 129,820
Text Length
    • length
    string

    Text Length (StringLength) counts the characters in a string and hands you the number as an integer. That's the whole node - and it's easy to write off as trivia until you need a workflow to measure its text. Then it's the thing that lets your graph check its own work.

    The genuinely useful patterns are all about validation and batching. You're feeding a prompt into a model with a hard token/character limit - a quick sanity check that your generated prompt isn't about to get truncated is worth a node. You built a filename out of parts and want to catch it getting absurdly long before the filesystem does. You're batching text and want to route anything under a length threshold down one path (short prompts get embellished, long ones pass through). In each case the trick is the same: this node turns an invisible property - "how long is this?" - into a number, and numbers are what routing, comparison, and loop logic are made of.

    How it works

    The mechanism is Python's len() on the input string - a single input, one output:

    • string - the text to measure. Multiline, so a prompt with embedded newlines counts them: "a\nb" is length 3, not 2, because the line break is a character. Worth remembering, since copied text loves to hide \n and \r\n in there.
    • Output: an INT named length - the character count. Wire it into a compare node, a threshold check, a math node, or a loop counter.

    There's no option for "word count," no "token estimate," no trimming - it's a raw character count, and anything fancier is up to the nodes you connect it to.

    A working sketch: an LLM prompt-writer node upstream, and you want every prompt to land between 200 and 600 characters. StringLength on the output → compare the INT against 200 and 600 → branch to "regenerate" or "use as-is." That's three or four nodes of graph for a rule you'd otherwise police by hand every run.

    Common issues

    The two things that bite: newlines count as characters, which inflates counts on multiline inputs in ways that surprise people measuring against a single-line expectation; and it's characters, not tokens - a 512-character prompt is not a 512-token prompt, so don't use the number to guess token limits without knowing your tokenizer's ratio. Beyond that it's about how you use the number: StringLength alone is inert, so pair it with a comparison or threshold node - the pattern is "length → compare against a target → branch." If you find yourself subtracting or slicing based on the result, the sibling Substring node (StringSubstring) is the partner that does something with the measured text.

    Categorytext

    Inputs (1)

    NameTypeDefaultDescription
    stringSTRING

    Outputs (1)

    NameTypeDescription
    lengthINT