ComfyUI-String-Function Introduction
ComfyUI-String-Function is a collection of custom ComfyUI nodes for working with text. It can read prompts from files, move through a list of ideas one at a time, prepare prompts generated by a language model, and make small changes to text before you use it in an image workflow.
This is useful when you want to:
- Keep prompts in text files instead of pasting them into ComfyUI repeatedly.
- Generate images from a different line or file on each workflow run.
- Turn a language model’s conversational response into a usable image prompt.
- Separate a positive prompt from a negative prompt.
- Clean up, split, search, or replace parts of a prompt without editing it by hand.
The extension processes text; it does not generate images or include an image-generation model. You can connect its text outputs to other nodes in your ComfyUI workflow.
How ComfyUI-String-Function Works
Think of each node as a small text tool. One node can read a file, another can pick out the part you need, and another can show you the result. You connect them in the order that suits your workflow.
For example, you could save one prompt per line in a text file and use File Read Line to supply a line on each run. When it reaches the final line, it starts again at the beginning. Or you could ask a language model to write a prompt, then use String Extract Prompt to remove introductory wording and keep the prompt itself.
Several nodes also provide information about their result. File Read reports whether reading succeeded; String Extract Prompt reports which extraction method it used; and String Find reports a character position. These extra outputs can help you check what happened when a workflow produces an unexpected result.
Choosing a language-model prompt workflow
You have two approaches described in this README:
- LM Studio Prompt Generator sends your theme to LM Studio and returns a positive prompt, a negative prompt, and the original response from one node. Its structured-output setting is designed to keep the response in a predictable format.
- String Extract Prompt works with text produced elsewhere, including a separate language-model node. It is useful when you already have an LLM workflow or need to handle varied response formats.
You can use Prompt Preview with either approach to inspect the text before it reaches later nodes.
ComfyUI-String-Function Features
Read prompts from files
| Node | What it does | When to use it |
|---|---|---|
| File Read | Reads the entire contents of one text file. | You keep a complete prompt or set of instructions in a file. |
| File Read Line | Returns one line per run, then loops back to the first line. | You have one prompt or idea per line. |
| Folder File Read | Returns the contents of one file per run, then loops through the folder again. | You keep separate prompt files for different concepts. |
File Read takes a file_path and returns text, line_count, and success. If the file does not exist, success is False. Its strip_newline_end setting, on by default, removes a newline at the very end of the file. The node also removes surrounding double quotes from a pasted path, and its Browse file button opens a file-selection dialog.
All three file-reading nodes offer an encoding setting. The default, auto, tries utf-8-sig, then utf-8, then cp932. If a file does not read correctly, you can choose one explicitly: utf-8, utf-8-sig for files with a UTF-8 byte-order mark, or cp932 for some Japanese Windows text files.
File Read Line returns line_text, a one-based line_index, line_count, and is_last. Choose sequential to follow the file’s order or shuffle to use each line once in a shuffled order before reshuffling. Set skip_empty_lines to True when blank lines should not count as entries. Set reset to True on a run when you want to restart from the beginning. Each node keeps its own position in memory, so restarting ComfyUI resets it.
Folder File Read offers the same sequential, shuffle, and reset controls for files. Its extension_filter defaults to .txt; enter a comma-separated list such as .txt,.md to include more types, or leave it blank to include all files. In sequential mode, sort_order can follow filenames from A to Z or Z to A, or modification times from oldest to newest or newest to oldest. The outputs include the file’s text, name, full path, one-based position, total number of matching files, and is_last. A Browse folder button opens a folder-selection dialog. Its position also resets when ComfyUI restarts.
Generate and extract image prompts
LM Studio Prompt Generator combines the theme you provide, an LM Studio text-generation request, and extraction of prompt and negative_prompt into one node. It also returns raw_text, method_used, and success.
Key settings include:
| Setting | What it changes |
|---|---|
theme | The image idea you send to the language model. |
system_prompt | Instructions for writing the image prompt. You do not need to specify the output format; the node supplies that instruction. |
model_key | The LM Studio model key. Leave it empty to use a loaded model detected by the node. |
structured_output | On by default. Requests JSON Schema output; when off, the node requests a PROMPT: / NEGATIVE: format instead. |
generate_negative | On by default. Controls whether a negative prompt is generated. |
seed | -1 uses a random seed on each run. |
max_tokens and temperature | Control the response length limit and generation behavior; the defaults are 4096 and 0.7. |
strip_thinking | On by default. Removes thinking content from raw_text. |
timeout_seconds | How long to wait for a response; the default is 300. |
debug | Prints request and response information to the console when enabled. |
The node also has auto_unload and unload_delay controls for unloading a model after generation. A delay above zero is a time-to-live in seconds; immediate unloading at a delay of zero requires the lmstudio SDK. If LM Studio cannot be reached or times out, the node stops the workflow with an error rather than passing along an empty prompt.
String Extract Prompt takes LLM output and returns prompt, method_used, and success. Its default auto mode tries several recognizable formats in order: a value in JSON, text in a suitable code block, text after a label such as Prompt:, and finally approaches that remove introductory wording or identify a comma-heavy prompt paragraph.
You can choose a specific extraction mode when you know the response format:
| Mode | What it looks for |
|---|---|
json | A value under one of the names in json_key, including in nested or partially cut-off JSON. |
code_block | Prompt text inside a code block; language-labelled blocks such as JSON or YAML are excluded. |
after_label | Text following a label listed in label_names. |
strip_preamble | Prompt text after removing introductory or closing wording, quotation marks, and a separate negative-prompt paragraph. |
most_commas | The paragraph with the most commas, useful for tag-style prompts. |
first_line | The first nonempty line after introductory wording is removed. |
raw | The cleaned input without further extraction. |
The default json_key list is prompt,positive_prompt,positive,description,text,content, in priority order. Key matching ignores capitalization and differences involving spaces, underscores, and hyphens. strip_think_blocks is on by default to remove supported thinking sections before extraction. If auto cannot confidently identify a prompt, fallback_to_raw=True returns cleaned text with success=False; setting it to False returns an empty result instead.
To extract both fields from an LLM response, use two String Extract Prompt nodes: one with json_key=prompt, and another with mode=json, json_key=negative_prompt, and fallback_to_raw=False. The second node then returns an empty string if the response has no negative prompt, rather than treating the whole response as one. See the example workflow:
Example String Extract Prompt workflow
If you configure Structured Output directly in LM Studio for this two-node workflow, the author provides this schema:
{
"type": "object",
"properties": {
"prompt": {
"type": "string",
"description": "Main prompt (comma-separated tags)"
},
"negative_prompt": {
"type": "string",
"description": "Negative prompt"
}
},
"required": ["prompt", "negative_prompt"],
"additionalProperties": false
}
Prompt Preview displays prompt, negative_prompt, and raw_text together on the node. All three inputs are optional, and the node passes each supplied value through unchanged. Place it between extraction and your later text-encoding nodes when you want to inspect what the model actually returned.
Edit and inspect text
These smaller nodes let you adjust prompts without changing the original file or LLM response:
| Node | Purpose and example |
|---|---|
| String Find | Finds the position of text. Positions start at 1; 0 means no match. Use start_pos and occurrence to search later in the text or find a later occurrence. |
| String Split | Splits text into before and after at a delimiter. Choose which occurrence to split at, or set use_last=True to split at the final one. |
| String Left / Mid / Right | Takes a specified number of characters from the beginning, from a chosen starting position, or from the end of text. |
| String Default | Supplies default_value when text is empty. Set trim_before_check=True if text containing only surrounding whitespace should be treated as empty. It also returns was_empty. |
| String Replace | Replaces old text with new text. count=-1 replaces every occurrence; a smaller count limits replacements. Set case_sensitive=False to ignore capitalization. It reports replace_count. |
| String Trim | Removes whitespace, or characters you specify in chars, from both ends (both), only the beginning (left), or only the end (right). It does not remove matching content from the middle. |
| String Case | Changes capitalization using upper, lower, title, capitalize, or swapcase. For example, title turns hello world into Hello World. |
| String Regex Match | Finds text using a pattern. Use it when an ordinary search is not flexible enough, such as extracting numbers with \d+. |
String Regex Match has four search modes: search finds a match anywhere, match checks from the start, fullmatch requires the whole text to match, and findall collects all matches. group=0 returns the entire match; another group number returns a parenthesized part of the pattern. You can also ignore case, make ^ and $ apply to individual lines with multiline, and choose the separator used to join findall results with join_str. Its outputs are result, matched, and match_count.
For example, the pattern (\d{4})-(\d{2})-(\d{2}) with mode=search and group=2 extracts the month from a date written as YYYY-MM-DD.
ComfyUI-String-Function Models
ComfyUI-String-Function does not provide its own image or language models. LM Studio Prompt Generator uses a model available through LM Studio; you can specify its model_key or leave the field empty to use a detected loaded model.
String Extract Prompt is designed to handle text from several model families and response styles. These are compatibility examples, not models bundled with the extension:
| Model or response style | What the extraction node handles |
|---|---|
| Qwen3 and DeepSeek-R1 | Supported <think> sections, including certain incomplete tags. |
| gpt-oss | Its channel-formatted response, keeping the final response rather than analysis content. |
| Llama 3, Qwen ChatML, Gemma, and Mistral | Supported assistant-role markers and other conversation-format tokens. |
| Conversational responses generally | Introductory wording, Markdown prompt labels, closing remarks, and accompanying negative prompts. |
A descriptive sentence such as “The image shows a cat...” may itself be the intended prompt. The extraction node does not remove it merely because it reads like prose.
Troubleshooting ComfyUI-String-Function
A file cannot be read
- Check that
file_pathpoints to an existing file, or use the node’s Browse file button to select it. - Inspect File Read’s
successoutput.Falseindicates that reading did not succeed, including when the file does not exist. - If the path was pasted with double quotes around it, you can leave them in place; File Read removes surrounding double quotes automatically.
- If the file’s text does not read correctly, try an explicit
encodinginstead ofauto.
A line or folder workflow keeps returning an unexpected item
- Check whether
modeissequentialorshuffle. Shuffle uses every item before reshuffling, but does not follow file order. - For File Read Line, check
skip_empty_linesif blank lines appear or the count differs from what you expected. - For Folder File Read, check
extension_filterand, in sequential mode,sort_order. - Set
reset=Truefor a run to start over. Remember that restarting ComfyUI also resets the positions kept in memory.
Prompt extraction returns too much text or the wrong field
- Inspect the complete LLM response with Prompt Preview.
- If the response is JSON, use
mode=jsonand setjson_keyto the exact field you want, such aspromptornegative_prompt. - If the response uses a visible label, try
mode=after_labeland add that label tolabel_names. - Check
method_usedandsuccess. A raw fallback withsuccess=Falsemeans the node could not confidently identify the prompt. - For a negative-prompt extraction node, set
fallback_to_raw=Falseso a missing field does not become an unintended negative prompt.
LM Studio Prompt Generator stops the workflow
A connection failure or timeout is reported as an error. Check that LM Studio is available at the address configured for the node; the default address is http://localhost:1234. For an LM Studio server on another computer, its server settings must allow local-network access, and the relevant firewall port must be open. The author notes that the .env settings are read on each execution, so changing the configured address does not require restarting ComfyUI.
Frequently asked questions
Do I need an LLM to use this extension?
No. The file-reading and general string nodes work with text independently of the LM Studio prompt workflow.
Will File Read Line eventually repeat a prompt?
Yes. Sequential mode returns to the first line after the last. Shuffle mode reshuffles after all eligible lines have been used.
Can I keep positive and negative prompts separate?
Yes. LM Studio Prompt Generator supplies separate outputs. With another LLM node, use separate String Extract Prompt nodes configured for the two fields.
Does Prompt Preview change my prompt?
No. It displays the supplied values and passes them through unchanged.
Learn More about ComfyUI-String-Function
- Explore the author’s repository for the extension’s source and accompanying files.
- Study the example prompt-extraction workflow alongside its workflow image to see how an LLM response, positive prompt, negative prompt, and preview node can be connected.
The extension is provided under the MIT License.
