From 5cbb86b1848425ba2f0db17d2a588bba0259af29 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 10 Aug 2023 13:46:25 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(utils.py):=20add=20UnbuiltOb?= =?UTF-8?q?ject=20class=20to=20improve=20code=20readability=20and=20mainta?= =?UTF-8?q?inability?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🐛 fix(base.py): change the error message when _built_object is None to provide more specific information and handle the case when _built_object is an instance of UnbuiltObject --- src/backend/langflow/graph/utils.py | 4 ++++ src/backend/langflow/graph/vertex/base.py | 13 ++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/backend/langflow/graph/utils.py b/src/backend/langflow/graph/utils.py index b78b2f961..e163d76c4 100644 --- a/src/backend/langflow/graph/utils.py +++ b/src/backend/langflow/graph/utils.py @@ -3,6 +3,10 @@ from typing import Any, Union from langflow.interface.utils import extract_input_variables_from_prompt +class UnbuiltObject: + pass + + def validate_prompt(prompt: str): """Validate prompt.""" if extract_input_variables_from_prompt(prompt): diff --git a/src/backend/langflow/graph/vertex/base.py b/src/backend/langflow/graph/vertex/base.py index cb7dc4905..71afc0c02 100644 --- a/src/backend/langflow/graph/vertex/base.py +++ b/src/backend/langflow/graph/vertex/base.py @@ -1,4 +1,5 @@ import ast +from langflow.graph.utils import UnbuiltObject from langflow.interface.initialize import loading from langflow.interface.listing import ALL_TYPES_DICT from langflow.utils.constants import DIRECT_TYPES @@ -22,7 +23,7 @@ class Vertex: self.edges: List["Edge"] = [] self.base_type: Optional[str] = base_type self._parse_data() - self._built_object = None + self._built_object = UnbuiltObject() self._built = False self.artifacts: Dict[str, Any] = {} @@ -245,8 +246,14 @@ class Vertex: """ Checks if the built object is None and raises a ValueError if so. """ - if self._built_object is None: - raise ValueError(f"Node type {self.vertex_type} not found") + if isinstance(self._built_object, UnbuiltObject): + raise ValueError(f"{self.vertex_type}: {self._built_object_repr()}") + elif self._built_object is None: + message = f"{self.vertex_type} returned None." + if self.base_type == "custom_components": + message += " Make sure your build method returns a component." + + raise ValueError(message) def build(self, force: bool = False) -> Any: if not self._built or force: