Nodes/ComfyUI-String-Function/String Extract Prompt
ComfyUI Node

String Extract Prompt

The node that turns messy LLM output into a usable prompt — auto mode just works

By TakkunRed·Created 5 months ago·Updated 15 days ago· 1
String Extract Prompt
    • prompt
    • method_used
    • success
    ◄text►
    ◄modeauto►
    ◄json_keyprompt,positive_prompt,positive,description,text,content►
    ◄label_namesPrompt,プロンプト,Description,Output,Result►
    ◄strip_think_blockstrue►
    ◄fallback_to_rawtrue►

    String Extract Prompt is the flagship of the TakkunRed ComfyUI-String-Function pack, and the reason most people install it. The job it solves is the one that makes LLM-assisted prompting either a joy or a mess: a chat model does not hand you a clean prompt. It wraps your request in "Here is the prompt:" preamble, dumps the answer inside a markdown code block, wraps it in JSON, or - with a reasoning model - hides half of it in a <think>...</think> block. Every one of those extras is going to ride straight into your CLIPTextEncode as literal prompt tokens if nothing strips it. That's the exact "dirty, non-structured output" failure mode that's the known Achilles' heel of local prompt enhancement.

    This node takes the LLM's output text and returns just the prompt string, so you can wire it directly into a KSampler's conditioning chain. Leave it on auto and it sorts out the format for you - no prompt template changes needed when you swap models.

    How it works

    Before anything else, it cleans the text: <think>...</think> blocks get removed (strip_think_blocks, on by default - this is the Qwen reasoning-model case), gpt-oss-20b's <|channel|>final<|message|> format gets unwrapped, and leftover special tokens are scrubbed.

    Then, in auto mode, it tries extraction strategies in order until one succeeds:

    1. code_block - pull the contents out of ``` or single backticks.
    2. json - parse a JSON object and return the first key in your json_key priority list that has a non-empty string value.
    3. after_label - match Prompt:, プロンプト:, Description:, etc. and take what follows (the label list is configurable via label_names).
    4. most_commas - return the paragraph with the most commas, on the theory that a comma-separated tag list is what an SD prompt looks like.

    If all four fail, fallback_to_raw decides: True returns the cleaned text as-is, False returns an empty string. Three other modes (strip_preamble, first_line, raw) exist but aren't used by auto, because they always return something and would mask a real extraction failure.

    Inputs and outputs

    text (required) is the raw LLM output; mode defaults to auto. The two you'll actually tune:

    • json_key - comma-separated keys to look for in JSON mode, in priority order. Default prompt,description,text,content.
    • fallback_to_raw - for a negative prompt you usually want False so a model that didn't produce one yields empty, not a chunk of prose.

    Outputs: prompt (STRING) - wire this into CLIPTextEncode; method_used (STRING) - which strategy fired, genuinely handy when the output looks wrong; success (BOOLEAN) - False means it fell back or extracted nothing.

    The two-node setup for positive + negative

    The README's reference workflow runs two of these. The positive uses mode=auto (or json with json_key=prompt, fallback_to_raw=True); the negative uses mode=json, json_key=negative_prompt, fallback_to_raw=False. If you're on LM Studio, the README also includes a JSON Schema for Structured Output that locks the model into emitting {"prompt": ..., "negative_prompt": ...}, which makes both extractions deterministic. That's the stable setup: structured output on the model side, this node on the parse side, and a Prompt Preview in between so you can see what survived.

    Installing it

    Pure stdlib, no dependencies, no model downloads:

    cd ComfyUI/custom_nodes
    git clone https://github.com/TakkunRed/ComfyUI-String-Function
    

    Or search "ComfyUI-String-Function" in ComfyUI Manager, restart, and it's under String Function.

    Where people get burned

    • auto isn't magic. If your model emits a genuinely freeform paragraph with no code fence, no label and few commas, auto will fall back - and with fallback_to_raw=True you'll silently get the whole paragraph. Watch the success output, not just the prompt.
    • Reasoning models leak scratch work. Keep strip_think_blocks on; a <think> block that survives is exactly the kind of thing that becomes literal prompt tokens on an LLM-encoded model.
    • The most_commas heuristic assumes SD-style tag lists. If your prompt style is natural-language prose, prefer forcing json or code_block output on the model side.
    • It only extracts - it doesn't generate. This node works with whatever text-generation node you already have, and it's the parse half of the LLM-in-the-graph pattern the pack is built around.
    CategoryString Function

    Inputs (6)

    NameTypeDefaultDescription
    textSTRING—
    modeCOMBOautoauto=構造抽出→前置き/後書き除去を順に試す / code_block=```抽出 / json=JSONフィールド抽出 / after_label=ラベル後テキスト / strip_preamble=前置き・後書き除去 / most_commas=カンマ最多段落 / first_line=最初の行 / raw=そのまま
    json_keyoptSTRINGprompt,positive_prompt,positive,description,text,contentjson モード時に探すキー名。カンマ区切りで優先順に複数指定可。大文字小文字・空白/_/- の違いは無視し、入れ子のJSONも探索する
    label_namesoptSTRINGPrompt,プロンプト,Description,Output,Resultafter_label モード時に認識するラベル名。カンマ区切りで優先順
    strip_think_blocksoptBOOLEANtrue<think>...</think> 等の思考ブロックを除去する (Qwen3 / DeepSeek-R1 等)
    fallback_to_rawoptBOOLEANtrueauto モードで確信を持てなかったとき: True=前置き除去済みテキストを返す / False=空を返す

    Outputs (3)

    NameTypeDescription
    promptSTRING—
    method_usedSTRING—
    successBOOLEAN—