NanoBanana - Structured Output (JSON)
Force Gemini to return valid JSON, not a rambling answer
- network
- json_output
LLMs love to narrate. Ask a model to extract "the price, the brand, and the rating" from a product description and half the time you get a paragraph explaining what it found, which you then have to parse with regex in a ComfyUI text node. This node kills that problem: you give it a JSON Schema, and Gemini is constrained to return data matching that shape. No commentary, no markdown fences, just JSON you can feed into any downstream node that expects structured data.
This is the node that makes "the LLM as a data pipeline" thing real in ComfyUI - extract fields from text, classify into a fixed schema, turn freeform input into a clean record. Wire it in front of a router or a JSON-parsing node and you've got programmatic output from a generative model.
How it works
It calls generate_content with response_mime_type: application/json and your schema passed as response_schema. Gemini's JSON-schema-constrained mode is genuinely good - it won't just try to match, it's instructed to produce only valid data in that shape. The node then prefers the SDK's pre-parsed .parsed result (which strips markdown fences and returns real JSON) and serializes it back to a string.
Inputs:
- prompt - describe what data to extract or generate.
- json_schema - the constraint. Defaults to a minimal
{"type": "object", "properties": {"result": {"type": "string"}}}so the node works out of the box. Write your own schema for real work - you can nest objects and arrays, and Gemini honors them. - model - defaults to
gemini-2.5-flash. Schema-following is reliable even on flash, which keeps it cheap. - temperature - defaults to 0.3, deliberately low. Structured output wants determinism; the tooltip's advice is right, don't crank it.
Output: json_output - a STRING containing the JSON. Wire it into whatever consumes JSON.
Installation
Part of the NanoBanana2 pack:
cd ComfyUI/custom_nodes
git clone https://github.com/IxMxAMAR/ComfyUI-NanoBanana2
pip install google-genai
ComfyUI Manager: search NanoBanana2. Needs a Gemini API key from aistudio.google.com.
Gotchas
The schema is the contract - spend time on it. A sloppy schema gets you valid-but-useless JSON (all your fields as strings, or a shape you can't consume). If the node errors with "Invalid JSON schema," it means the schema text itself didn't parse - check for trailing commas and unbalanced braces before suspecting the model. And don't confuse this with the pack's Safety Settings output, which is also a JSON string: that one is a config for other nodes, this one is content from the model. Different jobs, both called JSON.
Inputs (8)
| Name | Type | Default | Description |
|---|---|---|---|
| api_key | STRING | NanoBanana - API key. Leave blank to use GEMINI_API_KEY env var. | |
| model | COMBO | gemini-2.5-flash | NanoBanana - model for structured output. |
| custom_model | STRING | Override with a custom model ID. | |
| prompt | STRING | The prompt describing what data to extract/generate. | |
| json_schema | STRING | { "type": "object", "properties": { "result": {"type": "string"} } } | JSON Schema that constrains the model output format. |
| system_instructionopt | STRING | System instruction for the model. | |
| temperatureopt | FLOAT | 0.300–2 | Lower temperature recommended for structured output. |
| networkopt | NB_NETWORK | Optional. Wire a NanoBanana - Network Route node here to route this request through that proxy (e.g. US egress). |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| json_output | STRING | — |