Skip to content

Commit 839a791

Browse files
fix(api): loosen graph parsing in extract_metadata_from_image
There's a pydantic thing that causes the graphs to fail validation erroneously. Details in the comments - not a high priority to fix but we should figure it out someday.
1 parent f03a2bf commit 839a791

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

invokeai/app/api/extract_metadata_from_image.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from PIL import Image
66

7-
from invokeai.app.services.shared.graph import Graph
87
from invokeai.app.services.workflow_records.workflow_records_common import WorkflowWithoutIDValidator
98

109

@@ -95,8 +94,25 @@ def extract_metadata_from_image(
9594
# always store graphs as a stringified JSON Graph. So, we expect it to be a string here.
9695
if isinstance(graph_raw, str):
9796
try:
98-
# Validate the graph JSON before storing it
99-
Graph.model_validate_json(graph_raw)
97+
# TODO(psyche): Due to pydantic's handling of None values, it is possible for the graph to fail validation,
98+
# even if it is a direct dump of a valid graph. Node fields in the graph are allowed to have be unset if
99+
# they have incoming connections, but something about the ser/de process cannot adequately handle this.
100+
#
101+
# In lieu of fixing the graph validation, we will just do a simple check here to see if the graph is dict
102+
# with the correct keys. This is not a perfect solution, but it should be good enough for now.
103+
104+
# FIX ME: Validate the graph JSON before storing it
105+
# Graph.model_validate_json(graph_raw)
106+
107+
# Crappy workaround to validate JSON
108+
graph_parsed = json.loads(graph_raw)
109+
if not isinstance(graph_parsed, dict):
110+
raise ValueError("Not a dict")
111+
if not isinstance(graph_parsed.get("nodes", None), dict):
112+
raise ValueError("'nodes' is not a dict")
113+
if not isinstance(graph_parsed.get("edges", None), list):
114+
raise ValueError("'edges' is not a list")
115+
100116
# Looks good, overwrite the fallback value
101117
stringified_graph = graph_raw
102118
except Exception as e:

0 commit comments

Comments
 (0)