Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle KeyError in template parameter mapping and suggest closest match if not found #3366

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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