Skip to content

Commit

Permalink
fix: Handle KeyError in template parameter mapping and suggest closes…
Browse files Browse the repository at this point in the history
…t match if not found (#3366)
  • Loading branch information
ogabrielluiz authored Aug 16, 2024
1 parent 16afd44 commit 212a566
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/backend/base/langflow/custom/custom_component/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from langflow.template.field.base import UNDEFINED, Input, Output
from langflow.template.frontend_node.custom_components import ComponentFrontendNode
from langflow.utils.async_helpers import run_until_complete
from langflow.utils.util import find_closest_match

from .custom_component import CustomComponent

Expand Down Expand Up @@ -415,7 +416,15 @@ def _map_parameters_on_frontend_node(self, frontend_node: ComponentFrontendNode)

def _map_parameters_on_template(self, template: dict):
for name, value in self._parameters.items():
template[name]["value"] = value
try:
template[name]["value"] = value
except KeyError:
close_match = find_closest_match(name, list(template.keys()))
if close_match:
raise ValueError(
f"Parameter '{name}' not found in {self.__class__.__name__}. " f"Did you mean '{close_match}'?"
)
raise ValueError(f"Parameter {name} not found in {self.__class__.__name__}. ")

def _get_method_return_type(self, method_name: str) -> List[str]:
method = getattr(self, method_name)
Expand Down
11 changes: 11 additions & 0 deletions src/backend/base/langflow/utils/util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import difflib
import importlib
import inspect
import json
Expand Down Expand Up @@ -465,3 +466,13 @@ def is_class_method(func, cls):

def escape_json_dump(edge_dict):
return json.dumps(edge_dict).replace('"', "œ")


def find_closest_match(string: str, list_of_strings: list[str]) -> str | None:
"""
Find the closest match in a list of strings.
"""
closest_match = difflib.get_close_matches(string, list_of_strings, n=1, cutoff=0.2)
if closest_match:
return closest_match[0]
return None

0 comments on commit 212a566

Please sign in to comment.