Nodes/ComfyUI-JSON-Prompt/LLM Output Formatting
ComfyUI Node

LLM Output Formatting

The janitor for LLM-generated JSON — fences stripped, quotes tamed, junk out

By fr0nky0ng·Created 10 months ago·Updated 9 months ago· 5
LLM Output Formatting
    • cleaned_json_string
    llm_output
    include_quality_promptfalse
    include_negative_promptfalse
    use_single_quotesfalse
    no_quotation_marksfalse

    Every LLM has opinions about formatting, and none of them agree with your text encoder. Give a local model a "return JSON" instruction and you'll get the JSON - wrapped in ```json fences, padded with a friendly paragraph of prose, occasionally with a comma where a bracket should be. This node is the mop. It takes whatever your LLM actually emitted, strips the markdown, parses the JSON, re-serializes it cleanly, and hands you a string you can feed straight into a prompt input.

    It's the second half of the ComfyUI-JSON-Prompt pack, designed to sit right after an Ollama or Gemini generate node. The first node (JSON Prompt Generator) builds the structured request; this one cleans up the reply. Between the two of them you get a local LLM writing structured JSON prompts for models with LLM-based text encoders - Anima, Chroma, Flux 2 Klein and friends - which is the workflow that actually works on those models.

    How it works

    Mechanically it's three steps, all in plain Python with no dependencies. First, if the input starts with { and ends with }, it's used as-is; otherwise a regex hunts for the first json ... fence and grabs what's inside. That's parsed with json.loads and re-dumped indented. Then two optional enrichment flags and two quote-massaging flags get applied. Finally the string comes out the single output, cleaned_json_string - a STRING you wire into your model's prompt input (or a ShowText node to eyeball it first).

    The inputs that matter

    • llm_output - the required input, marked forceInput, so it only takes a connection from another node. There's no text box; you must wire it from your LLM's generate node.
    • include_quality_prompt / include_negative_prompt - two toggles, both off by default. Enable them and the node injects extra fields into the JSON: a quality_targets array and a negative_prompt array.

    And here's the quirk worth knowing: both arrays are hardcoded to the same four strings - "accurate limb lengths and joint angles", "correct finger count and articulation", and so on. Turn both on and you get identical content under two different keys. Worse, those are positive phrasings ("accurate limb lengths") sitting in a negative_prompt field, which is backwards from how negatives usually work. On a real CFG model that's actively unhelpful; on an LLM-encoded model at low CFG the negative box barely matters anyway. My take: leave both off unless you've read the JSON your LLM emits and decided you want them.

    • use_single_quotes - replaces every double quote with a single quote. The author's own tooltip calls this "non-standard JSON," which is the polite way of saying it breaks parsing. Only for prompt templates that demand single quotes.
    • no_quotation_marks - strips all quotes around keys and values, leaving key: value text. The tooltip again flags it as non-JSON; some prompt templates want exactly this. The two flags are mutually-aware in the code: if you set both, single-quotes gets skipped and full stripping wins.

    Install

    Same pack as the generator - ComfyUI Manager, search "JSON Prompt", or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/fr0nky0ng/ComfyUI-JSON-Prompt
    

    Restart, and LLM Output Formatting appears under Utils/Text. No pip installs - requirements.txt is empty and the node only uses the standard library. No model downloads, no API key.

    Common issues

    • json.loads throws on your LLM's output. This is the big one. If the reply isn't valid JSON after fence-stripping, the node raises. The fence regex only handles the json ... shape, so an LLM that wraps in plain triple-backticks without the json label, or that rambles prose around the JSON, will break it. Your fix lives upstream: make the system prompt demand "JSON only, wrapped in ```json", or pick a smaller local model that follows instructions.
    • It fires even when the output looks wrong. The include_negative_prompt content being positive phrasing is by design of the pack, not a bug you can toggle off per-item. Check what it actually injects before shipping a workflow on it.
    • Single quotes broke something downstream. Yes - that's what the tooltip is warning you about. It's for niche prompt templates, not general use.

    Zero impressions, no community noise, and honestly this node is glue - but it's honest glue that does one fiddly thing (taming LLM JSON) well, and it makes the pack's generator node actually usable. If you're already running the first node, you want this one too.

    CategoryUtils/Text

    Inputs (5)

    NameTypeDefaultDescription
    llm_outputSTRING
    include_quality_promptBOOLEANfalse
    include_negative_promptBOOLEANfalse
    use_single_quotesBOOLEANfalseIf enabled, all double quotes in the final JSON will be replaced with single quotes (non-standard JSON)
    no_quotation_marksBOOLEANfalseIf enabled, all quotation marks (both double and single) around keys and values will be removed (produces non-JSON text, useful for some prompt templates)

    Outputs (1)

    NameTypeDescription
    cleaned_json_stringSTRING