From 7e756b9db56677636e6920c1e6628d13e980aec7 Mon Sep 17 00:00:00 2001 From: Cristhian Zanforlin Lousa Date: Wed, 29 Jan 2025 16:43:20 -0300 Subject: [PATCH] refactor: improve artifact type handling and result processing (#6002) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🐛 (component.py): fix the logic to determine the artifact type based on raw data and status 🐛 (artifact.py): fix the default message assignment in post_process_raw function to ensure consistent behavior --- .../base/langflow/custom/custom_component/component.py | 8 ++++---- src/backend/base/langflow/schema/artifact.py | 6 ++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/backend/base/langflow/custom/custom_component/component.py b/src/backend/base/langflow/custom/custom_component/component.py index 639dbc240f1e..cd950a85f66b 100644 --- a/src/backend/base/langflow/custom/custom_component/component.py +++ b/src/backend/base/langflow/custom/custom_component/component.py @@ -957,19 +957,19 @@ def _build_artifact(self, result): custom_repr = str(custom_repr) raw = self._process_raw_result(result) - artifact_type = get_artifact_type(self.status or raw, result) + artifact_type = get_artifact_type(raw or self.status, result) raw, artifact_type = post_process_raw(raw, artifact_type) return {"repr": custom_repr, "raw": raw, "type": artifact_type} def _process_raw_result(self, result): - if self.status: - raw = self.status - elif hasattr(result, "data"): + if hasattr(result, "data"): raw = result.data elif hasattr(result, "model_dump"): raw = result.model_dump() elif isinstance(result, dict | Data | str): raw = result.data if isinstance(result, Data) else result + elif self.status: + raw = self.status else: raw = result return raw diff --git a/src/backend/base/langflow/schema/artifact.py b/src/backend/base/langflow/schema/artifact.py index fcf9109621e7..03be57867aa4 100644 --- a/src/backend/base/langflow/schema/artifact.py +++ b/src/backend/base/langflow/schema/artifact.py @@ -63,6 +63,8 @@ def _to_list_of_dicts(raw): def post_process_raw(raw, artifact_type: str): + default_message = "Built Successfully ✨" + if artifact_type == ArtifactType.STREAM.value: raw = "" elif artifact_type == ArtifactType.ARRAY.value: @@ -74,7 +76,7 @@ def post_process_raw(raw, artifact_type: str): artifact_type = ArtifactType.OBJECT.value except Exception: # noqa: BLE001 logger.opt(exception=True).debug(f"Error converting to json: {raw} ({type(raw)})") - raw = "Built Successfully ✨" + raw = default_message else: - raw = "Built Successfully ✨" + raw = default_message return raw, artifact_type