Skip to content

Commit

Permalink
fix(edge): Handle invalid input types when creating TargetHandle (#3368)
Browse files Browse the repository at this point in the history
* fix(edge): Handle invalid input types when creating TargetHandle

* test(edge): add test for raising error on invalid target handle

* fix: mypy error union-attr

---------

Co-authored-by: italojohnny <[email protected]>
  • Loading branch information
ogabrielluiz and italojohnny authored Aug 16, 2024
1 parent 8de100e commit c4d2dc5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/backend/base/langflow/graph/edge/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,23 @@ def __init__(self, source: "Vertex", target: "Vertex", edge: EdgeData):
self._target_handle = cast(TargetHandleDict, data.get("targetHandle", {}))
self.source_handle: SourceHandle = SourceHandle(**self._source_handle)
if isinstance(self._target_handle, dict):
self.target_handle: TargetHandle = TargetHandle(**self._target_handle)
try:
self.target_handle: TargetHandle = TargetHandle(**self._target_handle)
except Exception as e:
if "inputTypes" in self._target_handle and self._target_handle["inputTypes"] is None:
# Check if self._target_handle['fieldName']
if hasattr(target, "_custom_component"):
display_name = getattr(target._custom_component, "display_name", "")
raise ValueError(
f"Component {display_name} field '{self._target_handle['fieldName']}' might not be a valid input."
) from e
else:
raise ValueError(
f"Field '{self._target_handle['fieldName']}' on {target.display_name} might not be a valid input."
) from e
else:
raise e

else:
raise ValueError("Target handle is not a dictionary")
self.target_param = self.target_handle.field_name
Expand Down
35 changes: 35 additions & 0 deletions src/backend/tests/unit/graph/edge/test_edge_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import pytest

from langflow.components.inputs.ChatInput import ChatInput
from langflow.components.models.OpenAIModel import OpenAIModelComponent
from langflow.components.outputs.ChatOutput import ChatOutput
from langflow.components.prompts.Prompt import PromptComponent
from langflow.graph.graph.base import Graph


@pytest.fixture
def client():
pass


def test_edge_raises_error_on_invalid_target_handle(client):
template = """Answer the user as if you were a pirate.
User: {user_input}
Answer:
"""
chat_input = ChatInput()
prompt_component = PromptComponent()
prompt_component.set(
template=template,
user_input=chat_input.message_response,
)

openai_component = OpenAIModelComponent()
openai_component.set(input_values=prompt_component.build_prompt)

chat_output = ChatOutput()
chat_output.set(input_value=openai_component.text_response)
with pytest.raises(ValueError, match="Component OpenAI field 'input_values' might not be a valid input."):
Graph(start=chat_input, end=chat_output)

0 comments on commit c4d2dc5

Please sign in to comment.