Nodes/ComfyUI-LLMs-Toolkit/String Template
ComfyUI Node

String Template

`Hello {name}!` made real in a ComfyUI node

By ComfyUI-Kelin·Created 2 years ago·Updated 4 months ago· 27
String Template
    • string
    template
    variables{}

    The node that finally kills copy-paste prompt building

    You've got a prompt that's 90% boilerplate - "A portrait of {subject}, {style}, soft lighting, {extra}" - and you want to swap the variables without rebuilding the string by hand every run. String Template is that node: a template with {placeholders} plus a JSON object of variables, and out comes the filled-in string. It's the same idea as Python's str.format and about as simple, which is exactly why it's useful in ComfyUI, where text nodes are otherwise static.

    It's the natural partner for the pack's JSON Builder family: build your variables as structured JSON, plug it into the variables input, and let this node render the prompt. That's the combination people actually build workflows around - a JSON Builder holding a character config, a String Template rendering the prompt, and the result feeding a CLIP text encoder. The tooltip on the variables input says it plainly: "Connect a JSON output (e.g., from JSON Builder) here."

    How it works

    Under the hood it's a Python formatter, but with a crucial tweak: the pack uses a custom SafeFormatter that leaves missing keys as-is instead of raising an error. Type {name} in your template but forget to provide name, and you get {name} back in the output - not a crash, not an empty string. That one behavior makes the node far more forgiving in a graph where inputs can be dry or disconnected.

    The variables input expects a JSON object string; it's parsed and fed to the formatter as keyword arguments. If the input is invalid JSON or not an object, the node logs a warning and renders with an empty context - so you see the template, unfilled, rather than a red node. The template input has standard ComfyUI dynamic prompts disabled, so {key} placeholders won't get mixed up with ComfyUI's own {wildcard} syntax.

    Inputs and output

    • template (required, multiline) - the text with {key} placeholders.
    • variables (optional, connectable) - a JSON object string, e.g. from JSON Builder.
    • Output: string - the rendered result. If rendering throws anything unexpected, you get the original template back rather than a failure.

    Installing

    It ships in the HuangYuChuh/ComfyUI-LLMs-Toolkit pack alongside everything else. ComfyUI Manager → search ComfyUI-LLMs-Toolkit → Install → restart, or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/HuangYuChuh/ComfyUI-LLMs-Toolkit
    cd ComfyUI-LLMs-Toolkit
    pip install -r requirements.txt
    

    It's filed under 🚦ComfyUI_LLMs_Toolkit/JSON (the pack treats it as a text/JSON utility). Dependencies are just Pillow and aiohttp.

    Gotchas

    Two things to keep in mind. First, the formatter uses Python's format semantics - that means literal braces in your template need doubling ({{ and }}) if you want them to survive rendering, and things like {0} or {key.attr} are interpreted, not just simple {key} swaps. If your output has braces in odd places, that's the cause. Second, values are interpolated as strings - a number from a JSON Builder will print fine, but don't expect formatting directives to behave like printf. And the missing-key behavior cuts both ways: it's great for robustness, but it also means a typo in a placeholder name produces a silent no-op - you'll see {subject} sitting in your final prompt and wonder why it's not filled. Check your keys against the JSON when output looks half-rendered.

    Category🚦ComfyUI_LLMs_Toolkit/JSON

    Inputs (2)

    NameTypeDefaultDescription
    templateSTRING
    variablesoptSTRING{}Connect a JSON output (e.g., from JSON Builder) here.

    Outputs (1)

    NameTypeDescription
    stringSTRING