fix: add TableInput validation for single dict and Data instances (#6136)

* 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 <italojohnnydosanjos@gmail.com>
This commit is contained in:
Gabriel Luiz Freitas Almeida 2025-02-17 19:13:21 -03:00 committed by GitHub
commit 63a78a832f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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