Nodes/ComfyUI API Toolkit/Gemini Structured Output (JSON)
ComfyUI Node

Gemini Structured Output (JSON)

Gemini forced into your schema, not hoping for valid output

By IxMxAMAR·Created 5 months ago·Updated 2 months ago· 1
Gemini Structured Output (JSON)
    • json_output
    api_key
    modelgemini-2.5-flash
    custom_model
    prompt
    json_schema{ "type": "object", "properties": { "result": {"type": "string"} } }
    system_instruction
    temperature0.30

    Ask any LLM to "return JSON" and roughly 20% of the time it'll add a conversational preamble, wrap the answer in markdown fences, or emit one trailing comma too many. For a human reading a chat, that's cosmetic. For a ComfyUI workflow that's about to parse that output and feed fields into other nodes, it's a hard crash. This node exists to make that failure mode impossible: you give it a JSON Schema, and the API is constrained to respond with data matching it. Not prompted - constrained.

    This is the node for turning Gemini into a data-munging stage in your pipeline. Extract structured facts from a caption, classify an image description into fixed fields, generate a consistent config object for downstream nodes, or produce a batch of parameters that a loop can iterate. The KB's llm-in-comfyui.md notes that the LLM's real job in image pipelines is "short structured rewriting" - this node is that job, done properly.

    How it works

    Gemini's API supports schema-constrained generation: pass a response_mime_type="application/json" plus a response_schema, and the model's decoding is restricted to tokens that produce valid JSON matching your schema. The node parses your json_schema text, hands both to generate_content, and returns the text of the response. Because the constraint is enforced at generation time, you get schema-valid JSON or a failed call - not "mostly valid, good luck."

    The default schema is a minimal one ({"type": "object", "properties": {"result": {"type": "string"}}}) so the node works out of the box, but that's just a placeholder - the schema is the whole point of the node.

    Inputs and outputs that matter

    • prompt - what to extract or generate. "From this text, return the subject, setting, and mood." The model fits its answer into your schema.
    • json_schema - your JSON Schema, pasted as text. This is the input you'll spend your time on. Define the fields, their types, and (helpfully) an enum or description for each and the model behaves accordingly.
    • model - defaults to gemini-2.5-flash, a solid structured-output workhorse; 35 choices.
    • temperature - defaults to 0.3, deliberately low. The tooltip says it and it's right: for structured output you want deterministic, and low temperature is your friend.
    • system_instruction - optional context for how to interpret the prompt.

    One output: json_output (STRING) - valid JSON matching your schema, ready for a JSON-parse node.

    How to install it

    Pack-level install:

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

    or "API Toolkit" in ComfyUI Manager. Needs google-genai>=0.8.0 and a Gemini API key.

    Common issues

    The classic trip is an invalid schema - the node raises "Invalid JSON schema" the moment your pasted JSON doesn't parse, so validate your schema in a JSON tool before pasting it. A more subtle one: schemas that are too loose (all type: string, no descriptions) give the model room to improvise and you get data that's valid JSON but semantically wrong. Tighten it with descriptions and enums and the quality jumps.

    And the usual cost note: every call is paid and the pack's API nodes re-run each queue, so a pipeline that invokes this a dozen times is a dozen paid calls per render. Use it where the structure earns the cost - not for one-off chat.

    CategoryAPI Toolkit/Gemini/Text

    Inputs (7)

    NameTypeDefaultDescription
    api_keySTRINGGemini API key. Leave blank to use GEMINI_API_KEY env var.
    modelCOMBOgemini-2.5-flashGemini model for structured output.
    custom_modelSTRINGOverride with a custom model ID.
    promptSTRINGThe prompt describing what data to extract/generate.
    json_schemaSTRING{ "type": "object", "properties": { "result": {"type": "string"} } }JSON Schema that constrains the model output format.
    system_instructionoptSTRINGSystem instruction for the model.
    temperatureoptFLOAT0.300–2Lower temperature recommended for structured output.

    Outputs (1)

    NameTypeDescription
    json_outputSTRING