Nodes/ComfyUI-MDSNodes/String Trim Fallback
ComfyUI Node

String Trim Fallback

First non-blank string wins, and whitespace doesn't count

By MarwanDSAI·Created 3 months ago·Updated about 15 hours ago· 3
String Trim Fallback
    • string
    ◄string_a—►
    ◄fallback_b—►

    Sooner or later every workflow grows an optional branch. There's a trigger word for the LoRA - but only when the LoRA is loaded. There's a caption from a vision model - but only when you're actually captioning. There's a style suffix that some runs want and some don't. And the moment that optional branch produces an empty string, it doesn't politely get skipped. It overwrites your good default with nothing, and the run completes happily with a generic image.

    String Trim Fallback is the two-line fix for that, packaged as a node: take A if it has any actual content, otherwise take B.

    How it works

    Strip the whitespace off both inputs. If the stripped primary string isn't empty, output it. Otherwise output the stripped fallback. Both inputs are optional, so an unconnected socket arrives as nothing rather than throwing an error, and the node reads that as an empty string.

    The whitespace part is the reason to prefer this over a naive null check. A text widget you tabbed through, a prompt file that ends in a newline, an LLM or caption node that "returned something" - all of those produce a string that is technically non-empty and semantically blank. " \n" is not a prompt. Here it counts as empty and the fallback takes over.

    What it does not do: text boxes. Both string_a and fallback_b are forced inputs, so this node arrives on your canvas as a completely blank rectangle with two sockets and no fields to type into. Everybody assumes it's broken. It isn't - you're supposed to wire both sides, typically from two PrimitiveString nodes, the pack's own Text (Multiline) Dual, or a CLIP Text Encode with its widget converted to an input. Leave string_a unwired and the node always outputs the fallback, which is a legitimate way to use it as a default-value holder.

    The one output is a STRING, and its name is lowercase - string, not STRING like most of the pack's other nodes. Cosmetic, but it's what you'll see on the slot.

    Where it belongs in the graph

    This is the fallback shape, not the A/B shape. There's no selector, nothing to flip, no boolean to remember - if there's content, it goes through; if there isn't, the default does. That's the difference between this and the pack's String Switch, where you choose explicitly and an empty choice is honoured as empty.

    Its natural neighbour is rgthree's Any Switch, which does first-non-null for any type. Reach for Trim Fallback when (a) you're dealing in strings specifically, and (b) you care that whitespace-only text is not content. Any Switch checks for null/empty; this strips first, which is the stricter and usually more correct reading of "the user left it blank".

    Concrete uses that come up constantly: an optional LoRA trigger word that shouldn't leave a stray comma when the LoRA isn't loaded; a style suffix that defaults to your house look; a caption that falls back to a hand-written description when the vision model returns nothing; a prompt read from a file that falls back to a stored default if the file is empty.

    The trap nobody warns you about

    It trims the winner on the way out. If you build prompts by concatenating pieces, those trailing spaces you were relying on as separators are gone. Chain "1girl, " + "blue eyes" through this and you get 1girl,blue eyes - which models will often still read correctly, and sometimes won't. If your graph glues tags with literal spaces, put the space in the next node's prefix, or use a separator argument on a merge node instead of a raw space.

    Installing it

    ComfyUI Manager: search ComfyUI-MDSNodes (publisher marwandsai), install. Or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/MarwanDSAI/comfyui-mdsnodes
    

    Restart ComfyUI and hard-refresh the tab. Nothing else to do - the pack has no dependencies at all (its requirements.txt is one blank line) and downloads no models. MIT, version 1.8.0, node lives under MDSNodes → text.

    Fair warning on support: this pack is one author's internal toolbox for his own model-testing workflow, with no footprint in the ComfyUI subreddits whatsoever. The node is six lines of Python, so when it surprises you, you can read the answer instead of searching for it.

    Common issues

    The node shows up with no text boxes. By design - both inputs are forced sockets. Wire them.

    Output is always the fallback. Either string_a is unconnected, or whatever feeds it is producing whitespace only. Check by temporarily wiring the upstream straight into a note or a preview.

    Words are running together downstream. That's the trim. Your separators were in the whitespace.

    You want a manual override you can toggle. Wrong node - use String Switch, which gives you a checkbox, or wire Is Empty into it if you want the decision made automatically.

    CategoryMDSNodes/text

    Inputs (2)

    NameTypeDefaultDescription
    string_aoptSTRING—
    fallback_boptSTRING—

    Outputs (1)

    NameTypeDescription
    stringSTRING—