Nodes/Prompt Palette-F/Get First Word
ComfyUI Node

Get First Word

Grab the first word of a prompt — and turn it into a clean filename

By id-fa·Created about a year ago·Updated 7 days ago· 2
Get First Word
    • text
    stop_word,
    use_regexfalse
    trimtrue
    remove_invalid_filename_charsfalse
    add_trailing_slashfalse
    text

    Get First Word is a tiny string-slicing utility, and it's the kind of node you don't think you need until a batch of 200 images lands with filenames like masterpiece, best quality, 1girl, ...png. It returns everything before the first occurrence of a stop word - by default a comma - so a full prompt becomes just its first tag. It's a boring little tool that quietly solves the most annoying part of organized output.

    How it works

    The mechanism is dead simple: find the first occurrence of stop_word in the text, return everything before it. "1girl, sunset, high quality" with the default stop word (,) gives you 1girl. An empty stop_word returns the whole text untouched.

    Where it stops being boring is the filename tooling bolted on. The settings read like a checklist for "make this safe to put in a path":

    • stop_word - the delimiter (default ,). In literal mode you can type \n, \r, or \t and they're expanded, so you can cut at a newline from a single-line widget.
    • use_regex - treat the stop word as a regular expression (default off). Invalid patterns don't crash the node; it just returns the whole text. Note that escape expansion is skipped when this is on.
    • trim - strip leading/trailing whitespace and newlines (default on).
    • remove_invalid_filename_chars - strip the Windows-forbidden characters <>:"/\|?* plus control chars and trailing dots/spaces. Flip this on and the result is genuinely filename-safe.
    • add_trailing_slash - append / so you can use the result as a folder path. Skipped if the result is empty.

    text is a wire-only input - there's no typing a value in, you feed it from upstream.

    What you'd actually do with it

    The natural setup: take your built prompt, extract the first tag, feed it into a Save Image filename_prefix. Combined with the pack's PromptPalette-F node, you can keep the palette's first line as the "subject tag" and have every output file named after whatever subject you toggled on. The remove_invalid_filename_chars toggle is the one that saves your bacon if you're automating a lot - a single : or / in a tag will otherwise make Windows throw your whole batch.

    The add_trailing_slash option points at a specific workflow: use the first tag as a per-prompt subfolder, so outputs organize themselves as you test variations.

    Installing it

    Same pack as everything else here:

    cd ComfyUI/custom_nodes
    git clone https://github.com/id-fa/ComfyUI-PromptPalette-F prompt-palette-f
    cd prompt-palette-f
    pip install -r requirements.txt
    

    Restart ComfyUI, and it shows up under Prompt Palette-F. Via ComfyUI Manager, search "Prompt Palette-F." No models, no extra dependencies - this node is a couple dozen lines of Python.

    The honest caveat

    There's not much to trip over, but note the defaults: trim is on (good) while remove_invalid_filename_chars and add_trailing_slash are off. So if you flip it on without checking, you'll get a filename with spaces and trailing junk. Turn on what you actually need. And remember it cuts at the first occurrence - if your stop word appears inside a (word:1.2) weight or an early tag, you'll cut earlier than you expect. For the common "first tag is the subject" case, it's exactly right.

    CategoryPrompt Palette-F

    Inputs (6)

    NameTypeDefaultDescription
    stop_wordSTRING,
    use_regexBOOLEANfalse
    trimBOOLEANtrue
    remove_invalid_filename_charsBOOLEANfalse
    add_trailing_slashBOOLEANfalse
    textoptSTRING

    Outputs (1)

    NameTypeDescription
    textSTRING