From 0a628e1fb1e684c54df6a931cc1df50873441af0 Mon Sep 17 00:00:00 2001 From: Edwin Jose Date: Tue, 29 Apr 2025 00:33:27 -0400 Subject: [PATCH] fix: Dict Input Validation issue in Dynamic Components (#7754) * Update inputs.py * update dict validation approach * [autofix.ci] apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- src/backend/base/langflow/api/utils.py | 10 +++++++++- src/backend/base/langflow/inputs/inputs.py | 4 +++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/backend/base/langflow/api/utils.py b/src/backend/base/langflow/api/utils.py index 652b0e27e..a87bd2e81 100644 --- a/src/backend/base/langflow/api/utils.py +++ b/src/backend/base/langflow/api/utils.py @@ -1,6 +1,7 @@ from __future__ import annotations import uuid +from ast import literal_eval from datetime import timedelta from enum import Enum from typing import TYPE_CHECKING, Annotated, Any @@ -280,11 +281,18 @@ def get_suggestion_message(outdated_components: list[str]) -> str: def parse_value(value: Any, input_type: str) -> Any: """Helper function to parse the value based on input type.""" if value == "": - return value + return {} if input_type == "DictInput" else value if input_type == "IntInput": return int(value) if value is not None else None if input_type == "FloatInput": return float(value) if value is not None else None + if input_type == "DictInput": + if isinstance(value, dict): + return value + try: + return literal_eval(value) if value is not None else {} + except (ValueError, SyntaxError): + return {} return value diff --git a/src/backend/base/langflow/inputs/inputs.py b/src/backend/base/langflow/inputs/inputs.py index c10171906..1bc32c18c 100644 --- a/src/backend/base/langflow/inputs/inputs.py +++ b/src/backend/base/langflow/inputs/inputs.py @@ -458,7 +458,9 @@ class DictInput(BaseInputMixin, ListableInputMixin, InputTraceMixin, ToolModeMix """ field_type: SerializableFieldTypes = FieldTypes.DICT - value: dict | None = {} + # Note do not set value to an empty dict, it will break the component in dynamic update build config + # value: dict | None = {} + value: dict = Field(default_factory=dict) class DropdownInput(BaseInputMixin, DropDownMixin, MetadataTraceMixin, ToolModeMixin):