Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix: validate iterator types through If collectors

JPPhoto71b7d5c5da49
diff --git a/invokeai/app/services/shared/graph_validation.py b/invokeai/app/services/shared/graph_validation.py
index 521b1b53c8..827b69344a 100644
--- a/invokeai/app/services/shared/graph_validation.py
+++ b/invokeai/app/services/shared/graph_validation.py
@@ -1892,7 +1892,16 @@ class Graph(BaseModel):
if output_type_error is not None:
return output_type_error
- return self._validate_iterator_collector_input(input_node, output_field_types)
+ if input_sources is None:
+ return self._validate_iterator_collector_input(input_node, output_field_types)
+
+ for source in input_sources:
+ collector_input_error = self._validate_iterator_collector_input(
+ self.get_node(source.node_id), output_field_types
+ )
+ if collector_input_error is not None:
+ return collector_input_error
+ return None
def _validate_iterator_input_presence(self, inputs: list[EdgeConnection]) -> str | None:
if len(inputs) == 0:
@@ -1930,6 +1939,11 @@ class Graph(BaseModel):
def _get_effective_output_connections(self, node_id: str, field: str) -> list[EdgeConnection] | None:
"""Resolve an If output to its unique branch sources, or None when a branch is unresolved or cyclic."""
+ root_source = (node_id, field)
+ root_node = self.get_node(node_id)
+ if not isinstance(root_node, IfInvocation) or field != "value":
+ return [EdgeConnection(node_id=node_id, field=field)]
+
pending: list[tuple[str, str, bool]] = [(node_id, field, False)]
active: set[tuple[str, str]] = set()
visited: set[tuple[str, str]] = set()
@@ -1946,7 +1960,7 @@ class Graph(BaseModel):
if source in visited:
continue
- node = self.get_node(source_node_id)
+ node = root_node if source == root_source else self.get_node(source_node_id)
if not isinstance(node, IfInvocation) or source_field != "value":
visited.add(source)
sources.append(EdgeConnection(node_id=source_node_id, field=source_field))
diff --git a/tests/test_node_graph.py b/tests/test_node_graph.py
index e4793eed8e..2366f4b04d 100644
--- a/tests/test_node_graph.py
+++ b/tests/test_node_graph.py
@@ -1188,6 +1188,49 @@ def test_graph_rejects_mixed_if_branch_types_for_collector_items():
graph.add_edge(create_edge(if_node.id, "value", collector.id, "item"))
+def test_graph_rejects_iterator_consumers_incompatible_with_if_collector_branches():
+ graph = Graph()
+ string_value = StringInvocation(id="string", value="text")
+ image_value = ImageToImageTestInvocation(id="image")
+ string_collector = CollectInvocation(id="string_collector")
+ image_collector = CollectInvocation(id="image_collector")
+ if_node = IfInvocation(id="if")
+ iterator = IterateInvocation(id="iterator")
+ string_sink = StringInvocation(id="string_sink")
+ for node in (string_value, image_value, string_collector, image_collector, if_node, iterator, string_sink):
+ graph.add_node(node)
+
+ graph.add_edge(create_edge(string_value.id, "value", string_collector.id, "item"))
+ graph.add_edge(create_edge(image_value.id, "image", image_collector.id, "item"))
+ graph.add_edge(create_edge(string_collector.id, "collection", if_node.id, "true_input"))
+ graph.add_edge(create_edge(image_collector.id, "collection", if_node.id, "false_input"))
+ graph.add_edge(create_edge(if_node.id, "value", iterator.id, "collection"))
+
+ with pytest.raises(InvalidEdgeError, match="Iterator output type does not match iterator input type"):
+ graph.add_edge(create_edge(iterator.id, "item", string_sink.id, "value"))
+
+
+def test_graph_skips_if_traversal_state_for_non_if_sources(monkeypatch: pytest.MonkeyPatch):
+ from invokeai.app.services.shared import graph_validation
+
+ string_value = StringInvocation(id="string", value="text")
+ graph = Graph(nodes={string_value.id: string_value})
+ set_calls = 0
+ original_set = set
+
+ def count_set_calls(*args: Any, **kwargs: Any) -> set[Any]:
+ nonlocal set_calls
+ set_calls += 1
+ return original_set(*args, **kwargs)
+
+ monkeypatch.setattr(graph_validation, "set", count_set_calls, raising=False)
+
+ assert graph._get_effective_output_connections(string_value.id, "value") == [
+ EdgeConnection(node_id=string_value.id, field="value")
+ ]
+ assert set_calls == 0
+
+
def test_graph_validation_bounds_shared_if_branch_validation_work(monkeypatch: pytest.MonkeyPatch):
from invokeai.app.services.shared import graph as graph_facade