ComfyUI - Categorized Prompt Formatter Node
A custom node for ComfyUI that allows you to filter and restructure text prompts based on tag categories defined in a YAML file.
This node takes a standard comma-separated prompt, categorizes each tag according to your definitions, and then rebuilds the prompt based on a template you provide. It's useful for enforcing prompt structure, extracting specific concepts, or reordering elements dynamically.
Features
- YAML-based Categorization: Define which tags belong to which categories in a simple
.yamlfile. - Multi-Category Tags: A single tag (e.g., "blue eyes") can belong to multiple categories (e.g.,
eyes,person_details). - Template-Driven Output: Structure your output prompt using placeholders like
<|category_name|>. - Flexible Delimiters: Configure input and output delimiters.
- Weight/Emphasis Handling: Attempts to preserve common syntax like
(tag:1.2)and[tag]while using the base tag for categorization. - Underscore/Space Matching: Optionally treat tags like
red_eyesandred eyesas equivalent during matching (while preserving original format in output). - Case Sensitivity Option: Choose whether matching should be case-sensitive or not.
- Unmatched Tag Handling: Decide whether to discard tags not used in the template, append them to the end, or output them separately.
Installation
-
Clone this repository into your
ComfyUI/custom_nodes/directory:cd ComfyUI/custom_nodes/ git clone https://github.com/xLegende/ComfyUI-Prompt-Formatter.git -
Install dependencies:
- Navigate to the node's directory:
cd ComfyUI-Prompt-Formatter - Run the install script using your ComfyUI Python environment:
# Example using a virtual environment: # path/to/your/ComfyUI/python_env/bin/python install.py # Or if your system python is the one used by ComfyUI: python install.py - This will install the
PyYAMLlibrary if it's not already present.
- Navigate to the node's directory:
-
Restart ComfyUI.
The node "📝 Categorized Prompt Formatter" should now appear under the "text/filtering" category when you right-click or double-click on the ComfyUI canvas.
Usage
- Prepare your YAML file:
- Create a
.yamlfile (e.g.,my_categories.yaml) or modify the includedprompt_categories.yaml. - The format is:
category_name_1: - tag1 - tag2 - tag_shared category_name_2: - tag3 - tag_shared # This tag now belongs to both categories - "(tag with weight:1.1)" # Define tags exactly as they might appear # ... etc - Place this file somewhere ComfyUI can find it (e.g., in the
ComfyUI/input/directory, or provide an absolute path, or place it in thecustom_nodes/ComfyUI-Prompt-Formatter/folder).
- Create a
- Add the Node: Add the "📝 Categorized Prompt Formatter" node to your workflow.
- Connect Input: Connect your source prompt string to the
promptinput. - Configure Widgets:
category_definition_file: Enter the name (e.g.,my_categories.yaml) or full path to your YAML file. The node will search relative to itself, inComfyUI/input/, and absolute paths.output_template: Define your desired output structure using<|category_name|>placeholders. Example:<|quality|>, <|character_num|>, <|eyes|>, <|clothing|>, <|setting|>- Adjust delimiters, case sensitivity, weight handling, and unmatched tag handling as needed.
match_underscores_spaces(Boolean, default: True): If enabled, the node will try to match input tags against YAML definitions by checking variants with underscores swapped for spaces (e.g., inputred_eyescan match YAMLred eyes, and vice-versa). The original format of the input tag is kept for the output string.
- Connect Output: Connect the
formatted_promptoutput to the next node in your workflow (e.g., a CLIP Text Encode node). Use the optionalrejected_promptoutput for debugging or alternative workflows.
Example
-
Input Prompt:
"masterpiece, 1girl, (red eyes:1.1), long hair, wearing a blue dress, outdoors, detailed background" -
YAML (
prompt_categories.yamlfrom this repo): (Contains categories likequality,character_num,eyes,hair_style,person_details,clothing,setting) -
Output Template:
<|quality|>, <|character_num|>, <|eyes|>, <|clothing|>, <|setting|> -
Unmatched Handling:
output_separately -
Result (
formatted_prompt):"masterpiece, 1girl, (red eyes:1.1), wearing a blue dress, outdoors" -
Result (
rejected_prompt):"long hair, detailed background"(Because<|hair_style|>wasn't in the template, and "detailed background" might not be defined or its category wasn't used)
Notes
- The YAML file path finding tries to be robust, checking relative to the node, the ComfyUI
inputdirectory, and absolute paths. If it can't find the file, it will print a warning and proceed without categorization. - Weight handling uses regular expressions to parse common patterns. Very complex nested structures might not parse correctly. The base tag extracted is used for lookup.
- If a tag belongs to multiple categories, and the template requests those multiple categories, the tag will appear multiple times in the output string by default.
