ComfyUI Node

setdefault

Give me the value, or put this default in for me — setdefault is both at once

By StableLlama·Created about a year ago·Updated 4 days ago· 48
setdefault
  • input_dict
  • default_value
  • DICT
  • value
key

Here's a workflow situation you'll hit constantly: you want a value out of a dict, but the key might not exist - and when it doesn't, you'd rather fall back to a default than crash. You could reach for DictGet, check the result, branch on whether it came back empty, and handle the fallback yourself. Or you could use setdefault (Basic data handling: DictSetDefault), which does the whole dance in one node.

It takes three required inputs:

  • input_dict - the DICT you're reading from
  • key - the key you want, as a STRING
  • default_value - the value to use (and store) if the key is missing (ANY)

and returns two outputs:

  • DICT - the dict, with default_value inserted under key if the key wasn't there
  • value - the value for that key, whether it was pre-existing or just inserted

This mirrors Python's dict.setdefault(key, default) exactly. If the key exists, you get the existing value back and the dict is not touched - the default is ignored, not used to overwrite. If the key is missing, the default is inserted and returned. "Use it if absent, leave it alone if present" - that's the entire contract, and it's why the node is named after the Python method rather than something friendlier.

Why this is more useful than it looks

The killer pattern is "load settings with sensible defaults." Build a base dict of defaults with DictCreate, run it through one DictSetDefault per setting you care about, and out the other side you have a dict where every key is guaranteed to exist - without clobbering anything the user already set. That's the difference between set (always overwrites) and setdefault (only fills gaps), and it makes this the node you reach for when you're merging config or initializing state.

Because you get the dict and the resolved value back, it's also a clean "get-or-default" that keeps your graph short: no branch on a missing result, no empty-string sentinel to parse.

Installing it

This node ships in the Basic data handling pack by StableLlama. In ComfyUI Manager, search "Basic data handling", install, restart. Manual route:

cd ComfyUI/custom_nodes
git clone https://github.com/StableLlama/ComfyUI-basic_data_handling

then restart. The pack is dependency-free - empty dependencies list, no requirements.txt, no model files. Pure Python.

Troubleshooting

The usual suspect applies here too: DICT is this pack's custom type, so input and output sockets only talk to other DICT-speaking nodes. The subtler trap is expecting the default to override an existing value - it won't, by design. If you want overwrite behavior, use set instead. And remember the key match is exact, so if you setdefault "cfg" but something else writes "CFG", you'll end up with both keys and your default quietly unused.

CategoryBasic/DICT

Inputs (3)

NameTypeDefaultDescription
input_dictDICT
keySTRING
default_value*

Outputs (2)

NameTypeDescription
DICTDICT
value*