Skip to content
190 changes: 173 additions & 17 deletions comfy_api/latest/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,125 @@ def _expand_schema_for_dynamic(out_dict: dict[str, Any], live_inputs: dict[str,
out_dict[input_type][finalized_id] = value
out_dict["dynamic_paths"][finalized_id] = finalize_prefix(curr_prefix, curr_prefix[-1])

class DynamicInputError(ValueError):
def __init__(self, input_name: str, message: str):
super().__init__(message)
self.input_name = input_name


@comfytype(io_type="COMFY_DYNAMICGROUP_V3")
class DynamicGroup(ComfyTypeI):
"""Repeat a widget template and pass its values to execute as a list of row dicts.

Template fields must be widget inputs without force_input or nested dynamic inputs.
Submit fields as '<group>.<index>.<field>', using indices below max without leading zeros.
The '<group>.' prefix is reserved for group fields, not separate sibling inputs.
min/max are integers that count submitted rows (defaults: 0/20), even when optional=True.
max also bounds the reconstructed list length and cannot exceed 20.
Each submitted row follows the template's required/optional field declarations.

Missing positions are dicts whose fields are None. Missing optional fields are
also None; widget defaults are not injected. With INPUT_IS_LIST, missing fields
are [None], matching the lists supplied for submitted fields.

Empty groups are [] in execute and check_lazy_status. Nonempty lazy groups
contain (value, original_key) tuples at each field.
PriceBadge widget dependencies use the same indexed field names as prompt keys.
"""

Type = list[dict[str, Any]]
_MaxRows = 20

class Input(DynamicInput):
def __init__(self, id: str, template: list[WidgetInput], min: int=0, max: int=20,
display_name: str=None, optional: bool=False, tooltip: str=None,
lazy: bool=None, extra_dict=None, group_name: str="Group"):
super().__init__(id, display_name, optional, tooltip, lazy, extra_dict)
if not template:
raise ValueError("DynamicGroup template must have at least one field.")
for t in template:
if isinstance(t, DynamicInput):
raise ValueError("Nesting dynamic inputs inside DynamicGroup is not supported.")
if not isinstance(t, WidgetInput):
raise TypeError("DynamicGroup template fields must be WidgetInputs.")
if t.force_input:
raise ValueError(f"DynamicGroup template field '{t.id}' must not use force_input.")
if not t.id or "." in t.id:
raise ValueError(f"DynamicGroup template field id must be nonempty and must not contain '.'. Got: '{t.id}'")
field_ids = [t.id for t in template]
if len(field_ids) != len(set(field_ids)):
raise ValueError("DynamicGroup template field ids must be unique within a row.")
if not id or "." in id:
raise ValueError(f"DynamicGroup id must be nonempty and must not contain '.'. Got: '{id}'")
if isinstance(min, bool) or not isinstance(min, int) or isinstance(max, bool) or not isinstance(max, int):
raise TypeError("DynamicGroup min and max must be integers.")
if not 1 <= max <= DynamicGroup._MaxRows:
raise ValueError(f"DynamicGroup max must be between 1 and {DynamicGroup._MaxRows}.")
if not 0 <= min <= max:
raise ValueError("DynamicGroup min must be between 0 and max.")
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
self.template = template
self.min = min
self.max = max
self.group_name = group_name

def get_all(self) -> list[Input]:
return [self] + list(self.template)

def as_dict(self):
return super().as_dict() | prune_dict({
"template": create_input_dict_v1(self.template),
"min": self.min,
"max": self.max,
"group_name": self.group_name,
})

def validate(self):
for t in self.template:
t.validate()

@staticmethod
def _expand_schema_for_dynamic(out_dict: dict[str, Any], live_inputs: dict[str, Any], value: tuple[str, dict[str, Any]], input_type: str, curr_prefix: list[str] | None):
info = value[1]
min_rows = info.get("min", 0)
max_rows = info.get("max", DynamicGroup._MaxRows)
template = info.get("template", {})
field_specs = {
field_id: (field_value, category)
for category in ("required", "optional")
for field_id, field_value in template.get(category, {}).items()
}
finalized_prefix = finalize_prefix(curr_prefix)
prefix = finalized_prefix + "."
present_rows = set()
for key in live_inputs:
if not key.startswith(prefix):
Comment thread
jaeone94 marked this conversation as resolved.
Resolved
continue
index, separator, field_id = key[len(prefix):].partition(".")
if (not separator or not index.isascii() or not index.isdecimal()
or (len(index) > 1 and index.startswith("0")) or field_id not in field_specs):
raise DynamicInputError(key, f"Invalid DynamicGroup input key '{key}'; expected '{finalized_prefix}.<index>.<template field>'.")
row = int(index)
if row >= max_rows:
raise DynamicInputError(key, f"DynamicGroup input '{key}' exceeds the index limit of {max_rows - 1} (max={max_rows}).")
present_rows.add(row)

if not min_rows <= len(present_rows) <= max_rows:
raise DynamicInputError(finalized_prefix, f"DynamicGroup input '{finalized_prefix}' received {len(present_rows)} rows; expected between {min_rows} and {max_rows}.")

for row in range(max(present_rows, default=-1) + 1):
Comment thread
jaeone94 marked this conversation as resolved.
Resolved
for field_id, (field_value, category) in field_specs.items():
slot_id = f"{finalized_prefix}.{row}.{field_id}"
if row in present_rows:
out_dict[category][slot_id] = field_value
# Preserve gaps without adding validation inputs for them.
out_dict["dynamic_paths"][slot_id] = slot_id

out_dict["list_paths"].add(finalized_prefix)
if not present_rows:
out_dict["dynamic_paths"][finalized_prefix] = finalized_prefix
out_dict["dynamic_paths_default_value"][finalized_prefix] = DynamicPathsDefaultValue.EMPTY_LIST


@comfytype(io_type="IMAGECOMPARE")
class ImageCompare(ComfyTypeI):
Type = dict
Expand Down Expand Up @@ -1529,6 +1648,8 @@ def setup_dynamic_input_funcs():
register_dynamic_input_func(DynamicCombo.io_type, DynamicCombo._expand_schema_for_dynamic)
# DynamicSlot.Input
register_dynamic_input_func(DynamicSlot.io_type, DynamicSlot._expand_schema_for_dynamic)
# DynamicGroup.Input
register_dynamic_input_func(DynamicGroup.io_type, DynamicGroup._expand_schema_for_dynamic)

if len(DYNAMIC_INPUT_LOOKUP) == 0:
setup_dynamic_input_funcs()
Expand All @@ -1540,6 +1661,8 @@ class V3Data(TypedDict):
'Dictionary where the keys are the input ids and the values dictate how to turn the inputs into a nested dictionary.'
dynamic_paths_default_value: dict[str, Any]
'Dictionary where the keys are the input ids and the values are a string from DynamicPathsDefaultValue for the inputs if value is None.'
list_paths: set[str]
'Paths whose index-keyed row dictionaries are reconstructed as lists.'
create_dynamic_tuple: bool
'When True, the value of the dynamic input will be in the format (value, path_key).'

Expand Down Expand Up @@ -1651,16 +1774,19 @@ def validate(self) -> None:
raise ValueError("PriceBadgeDepends.input_groups must be a list[str].")

def as_dict(self, schema_inputs: list["Input"]) -> dict[str, Any]:
# Build lookup: widget_id -> io_type
input_types: dict[str, str] = {}
for inp in schema_inputs:
all_inputs = inp.get_all()
input_types[inp.id] = inp.get_io_type() # First input is always the parent itself
for nested_inp in all_inputs[1:]:
# For DynamicCombo/DynamicSlot, nested inputs are prefixed with parent ID
# to match frontend naming convention (e.g., "should_texture.enable_pbr")
prefixed_id = f"{inp.id}.{nested_inp.id}"
input_types[prefixed_id] = nested_inp.get_io_type()

def collect_inputs(inputs: list[Input], prefix: str = "") -> None:
for inp in inputs:
name = prefix + inp.id
input_types[name] = inp.get_io_type()
if isinstance(inp, DynamicGroup.Input):
for row in range(inp.max):
collect_inputs(inp.template, f"{name}.{row}.")
else:
collect_inputs(inp.get_all()[1:], name + ".")

collect_inputs(schema_inputs)

# Enrich widgets with type information, raising error for unknown widgets
widgets_data: list[dict[str, str]] = []
Expand Down Expand Up @@ -1888,6 +2014,7 @@ def get_finalized_class_inputs(d: dict[str, Any], live_inputs: dict[str, Any], i
"optional": {},
"dynamic_paths": {},
"dynamic_paths_default_value": {},
"list_paths": set(),
}
d = d.copy()
# ignore hidden for parsing
Expand All @@ -1899,10 +2026,12 @@ def get_finalized_class_inputs(d: dict[str, Any], live_inputs: dict[str, Any], i
dynamic_paths = out_dict.pop("dynamic_paths", None)
if dynamic_paths is not None and len(dynamic_paths) > 0:
v3_data["dynamic_paths"] = dynamic_paths
# this list is used for autogrow, in the case all inputs are optional and no values are passed
dynamic_paths_default_value = out_dict.pop("dynamic_paths_default_value", None)
if dynamic_paths_default_value is not None and len(dynamic_paths_default_value) > 0:
v3_data["dynamic_paths_default_value"] = dynamic_paths_default_value
list_paths = out_dict.pop("list_paths")
Comment thread
jaeone94 marked this conversation as resolved.
Resolved
if list_paths:
v3_data["list_paths"] = list_paths
return out_dict, hidden, v3_data

def parse_class_inputs(out_dict: dict[str, Any], live_inputs: dict[str, Any], curr_dict: dict[str, Any], curr_prefix: list[str] | None=None) -> None:
Expand All @@ -1921,11 +2050,23 @@ def parse_class_inputs(out_dict: dict[str, Any], live_inputs: dict[str, Any], cu
if curr_prefix:
out_dict["dynamic_paths"][finalized_id] = finalized_id

def _dynamic_group_prefixes(inputs: list[Input]) -> Iterable[str]:
for inp in inputs:
if isinstance(inp, DynamicGroup.Input):
yield inp.id + "."
elif isinstance(inp, DynamicInput):
for prefix in _dynamic_group_prefixes(inp.get_all()[1:]):
yield f"{inp.id}.{prefix}"


def create_input_dict_v1(inputs: list[Input]) -> dict:
input = {
"required": {}
}
group_prefixes = tuple(_dynamic_group_prefixes(inputs))
for i in inputs:
if group_prefixes and i.id.startswith(group_prefixes):
raise ValueError(f"Input '{i.id}' conflicts with a DynamicGroup field prefix.")
add_to_dict_v1(i, input)
return input

Expand All @@ -1938,10 +2079,12 @@ def add_to_dict_v1(i: Input, d: dict):

class DynamicPathsDefaultValue:
EMPTY_DICT = "empty_dict"
EMPTY_LIST = "empty_list"

def build_nested_inputs(values: dict[str, Any], v3_data: V3Data):
def build_nested_inputs(values: dict[str, Any], v3_data: V3Data, *, input_is_list: bool = False):
paths = v3_data.get("dynamic_paths", None)
default_value_dict = v3_data.get("dynamic_paths_default_value", {})
list_paths = v3_data.get("list_paths", set())
if paths is None:
return values
values = values.copy()
Expand All @@ -1958,19 +2101,31 @@ def build_nested_inputs(values: dict[str, Any], v3_data: V3Data):
is_last = (i == len(parts) - 1)

if is_last:
missing = key not in values
value = values.pop(key, None)
if value is None:
# see if a default value was provided for this key
default_option = default_value_dict.get(key, None)
if default_option == DynamicPathsDefaultValue.EMPTY_DICT:
value = {}
if create_tuple:
default_option = default_value_dict.get(key, None)
if default_option == DynamicPathsDefaultValue.EMPTY_LIST:
# No row keys were submitted, regardless of the root input value.
value = []
elif value is None and default_option == DynamicPathsDefaultValue.EMPTY_DICT:
value = {}
elif missing and input_is_list and ".".join(parts[:-2]) in list_paths:
value = [None]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't necessarily the wrong behavior, but it is a change in behavior. The extra [None] wrapping now reaches other dynamic input types** (_io.py:2111-2112, build_nested_inputs). The new list-mode padding applies to every key in dynamic_paths, not just DynamicGroup fields.

  • On a V3 node with INPUT_IS_LIST, an omitted optional input inside a DynamicCombo or DynamicSlot now arrives as [None] instead of None. One reviewer reproduced this on the branch.
  • The same happens in validate_inputs.
  • No node in the repo hits this, but custom nodes that check x is None would silently change behaviour.

As long as this was intended this is fine. We might want to document it for custom node authors though.

if create_tuple and default_option != DynamicPathsDefaultValue.EMPTY_LIST:
value = (value, key)
current[p] = value
else:
current = current.setdefault(p, {})

values.update(result)
for list_path in list_paths:
parts = list_path.split(".")
container = values
for part in parts[:-1]:
container = container[part]
rows = container[parts[-1]]
if isinstance(rows, dict):
Comment thread
jaeone94 marked this conversation as resolved.
Resolved
container[parts[-1]] = [rows[key] for key in sorted(rows, key=int)]
return values


Expand Down Expand Up @@ -2538,6 +2693,7 @@ def as_dict(self):
"MatchType",
"DynamicCombo",
"Autogrow",
"DynamicGroup",
# Other classes
"HiddenHolder",
"Hidden",
Expand Down
14 changes: 12 additions & 2 deletions execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ async def process_inputs(inputs, index=None, input_is_list=False):
f = make_locked_method_func(type_obj, func, class_clone)
# in case of dynamic inputs, restructure inputs to expected nested dict
if v3_data is not None:
inputs = _io.build_nested_inputs(inputs, v3_data)
inputs = _io.build_nested_inputs(inputs, v3_data, input_is_list=input_is_list)
# V1
else:
f = getattr(obj, func)
Expand Down Expand Up @@ -880,7 +880,17 @@ async def validate_inputs(prompt_id, prompt, item, validated, visiting=None):
if issubclass(obj_class, _ComfyNodeInternal):
obj_class: _io._ComfyNodeBaseInternal
class_inputs = obj_class.INPUT_TYPES()
class_inputs, _, v3_data = _io.get_finalized_class_inputs(class_inputs, inputs)
try:
class_inputs, _, v3_data = _io.get_finalized_class_inputs(class_inputs, inputs)
except _io.DynamicInputError as ex:
errors.append({
"type": "invalid_dynamic_input",
"message": "Invalid dynamic input",
"details": str(ex),
"extra_info": {"input_name": ex.input_name},
})
validated[unique_id] = (False, errors, unique_id)
return validated[unique_id]
validate_function_name = "validate_inputs"
validate_function = first_real_override(obj_class, validate_function_name)
else:
Expand Down
Loading
Loading