From d43ebabbbc1d272c6aedbfb58082c547ab4d2f85 Mon Sep 17 00:00:00 2001 From: QuantumGhost Date: Mon, 2 Jun 2025 17:41:25 +0800 Subject: [PATCH] fix(api): Fix TypeError while serializing File object --- api/core/variables/utils.py | 18 ++++++++++++++++++ api/models/workflow.py | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/api/core/variables/utils.py b/api/core/variables/utils.py index e5d222af7d..692db3502e 100644 --- a/api/core/variables/utils.py +++ b/api/core/variables/utils.py @@ -1,8 +1,26 @@ +import json from collections.abc import Iterable, Sequence +from .segment_group import SegmentGroup +from .segments import ArrayFileSegment, FileSegment, Segment + def to_selector(node_id: str, name: str, paths: Iterable[str] = ()) -> Sequence[str]: selectors = [node_id, name] if paths: selectors.extend(paths) return selectors + + +class SegmentJSONEncoder(json.JSONEncoder): + def default(self, o): + if isinstance(o, ArrayFileSegment): + return [v.model_dump() for v in o.value] + elif isinstance(o, FileSegment): + return o.value.model_dump() + elif isinstance(o, SegmentGroup): + return [self.default(seg) for seg in o.value] + elif isinstance(o, Segment): + return o.value + else: + super().default(o) diff --git a/api/models/workflow.py b/api/models/workflow.py index 975ca882d9..6322770736 100644 --- a/api/models/workflow.py +++ b/api/models/workflow.py @@ -985,7 +985,7 @@ class WorkflowDraftVariable(Base): self._set_selector([self.node_id, name]) def set_value(self, value: Segment): - self.value = json.dumps(value.value) + self.value = json.dumps(value, cls=variable_utils.SegmentJSONEncoder) self.value_type = value.value_type def get_node_id(self) -> str | None: