From 63a78a832f734a8a79fc37db7643b3d830b9f7ea Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Mon, 17 Feb 2025 19:13:21 -0300 Subject: [PATCH] fix: add TableInput validation for single dict and Data instances (#6136) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: enhance TableInput validation to support single dict and Data instances * [autofix.ci] apply automated fixes * fix: Improve TableInput validation error handling Change TypeError to ValueError for Pydantic validation to ensure proper error catching. Added noqa comments to suppress linting warnings about error type. --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Ítalo Johnny --- src/backend/base/langflow/inputs/inputs.py | 30 ++++++++++++++++------ 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/backend/base/langflow/inputs/inputs.py b/src/backend/base/langflow/inputs/inputs.py index 48e0c0649..e72366756 100644 --- a/src/backend/base/langflow/inputs/inputs.py +++ b/src/backend/base/langflow/inputs/inputs.py @@ -37,20 +37,34 @@ class TableInput(BaseInputMixin, MetadataTraceMixin, TableMixin, ListableInputMi @field_validator("value") @classmethod def validate_value(cls, v: Any, _info): - # Check if value is a list of dicts + # Convert single dict or Data instance into a list. + if isinstance(v, dict | Data): + v = [v] + # Automatically convert DataFrame into a list of dictionaries. if isinstance(v, DataFrame): v = v.to_dict(orient="records") + # Verify the value is now a list. if not isinstance(v, list): - msg = f"TableInput value must be a list of dictionaries or Data. Value '{v}' is not a list." - raise ValueError(msg) # noqa: TRY004 - - for item in v: + msg = ( + "The table input must be a list of rows. You provided a " + f"{type(v).__name__}, which cannot be converted to table format. " + "Please provide your data as either:\n" + "- A list of dictionaries (each dict is a row)\n" + "- A pandas DataFrame\n" + "- A single dictionary (will become a one-row table)\n" + "- A Data object (Langflow's internal data structure)\n" + ) + raise ValueError(msg) # noqa: TRY004 Pydantic only catches ValueError or AssertionError + # Ensure each item in the list is either a dict or a Data instance. + for i, item in enumerate(v): if not isinstance(item, dict | Data): msg = ( - "TableInput value must be a list of dictionaries or Data. " - f"Item '{item}' is not a dictionary or Data." + f"Row {i + 1} in your table has an invalid format. Each row must be either:\n" + "- A dictionary containing column name/value pairs\n" + "- A Data object (Langflow's internal data structure for passing data between components)\n" + f"Instead, got a {type(item).__name__}. Please check the format of your input data." ) - raise ValueError(msg) # noqa: TRY004 + raise ValueError(msg) # noqa: TRY004 Pydantic only catches ValueError or AssertionError return v