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:
parent
4d793371cc
commit
63a78a832f
1 changed files with 22 additions and 8 deletions
|
|
@ -37,20 +37,34 @@ class TableInput(BaseInputMixin, MetadataTraceMixin, TableMixin, ListableInputMi
|
||||||
@field_validator("value")
|
@field_validator("value")
|
||||||
@classmethod
|
@classmethod
|
||||||
def validate_value(cls, v: Any, _info):
|
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):
|
if isinstance(v, DataFrame):
|
||||||
v = v.to_dict(orient="records")
|
v = v.to_dict(orient="records")
|
||||||
|
# Verify the value is now a list.
|
||||||
if not isinstance(v, list):
|
if not isinstance(v, list):
|
||||||
msg = f"TableInput value must be a list of dictionaries or Data. Value '{v}' is not a list."
|
msg = (
|
||||||
raise ValueError(msg) # noqa: TRY004
|
"The table input must be a list of rows. You provided a "
|
||||||
|
f"{type(v).__name__}, which cannot be converted to table format. "
|
||||||
for item in v:
|
"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):
|
if not isinstance(item, dict | Data):
|
||||||
msg = (
|
msg = (
|
||||||
"TableInput value must be a list of dictionaries or Data. "
|
f"Row {i + 1} in your table has an invalid format. Each row must be either:\n"
|
||||||
f"Item '{item}' is not a dictionary or Data."
|
"- 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
|
return v
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue