String Extract Prompt
The node that turns messy LLM output into a usable prompt — auto mode just works
- prompt
- method_used
- success
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:
code_block- pull the contents out of``` or single backticks.json- parse a JSON object and return the first key in yourjson_keypriority list that has a non-empty string value.after_label- matchPrompt:,プロンプト:,Description:, etc. and take what follows (the label list is configurable vialabel_names).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. Defaultprompt,description,text,content.fallback_to_raw- for a negative prompt you usually wantFalseso 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
autoisn't magic. If your model emits a genuinely freeform paragraph with no code fence, no label and few commas,autowill fall back - and withfallback_to_raw=Trueyou'll silently get the whole paragraph. Watch thesuccessoutput, not just the prompt.- Reasoning models leak scratch work. Keep
strip_think_blockson; a<think>block that survives is exactly the kind of thing that becomes literal prompt tokens on an LLM-encoded model. - The
most_commasheuristic assumes SD-style tag lists. If your prompt style is natural-language prose, prefer forcingjsonorcode_blockoutput 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.
Inputs (6)
| Name | Type | Default | Description |
|---|---|---|---|
| text | STRING | — | |
| mode | COMBO | auto | auto=構造抽出→前置き/後書き除去を順に試す / code_block=```抽出 / json=JSONフィールド抽出 / after_label=ラベル後テキスト / strip_preamble=前置き・後書き除去 / most_commas=カンマ最多段落 / first_line=最初の行 / raw=そのまま |
| json_keyopt | STRING | prompt,positive_prompt,positive,description,text,content | json モード時に探すキー名。カンマ区切りで優先順に複数指定可。大文字小文字・空白/_/- の違いは無視し、入れ子のJSONも探索する |
| label_namesopt | STRING | Prompt,プロンプト,Description,Output,Result | after_label モード時に認識するラベル名。カンマ区切りで優先順 |
| strip_think_blocksopt | BOOLEAN | true | <think>...</think> 等の思考ブロックを除去する (Qwen3 / DeepSeek-R1 等) |
| fallback_to_rawopt | BOOLEAN | true | auto モードで確信を持てなかったとき: True=前置き除去済みテキストを返す / False=空を返す |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| prompt | STRING | — |
| method_used | STRING | — |
| success | BOOLEAN | — |