-
Notifications
You must be signed in to change notification settings - Fork 8.3k
feat: Add conversion between types and add unit tests for data conversion #7412
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
base: main
Are you sure you want to change the base?
Conversation
WalkthroughThe changes expand the input type compatibility for four input classes, enabling them to interchangeably accept and convert between Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant InputClass
participant Data
participant DataFrame
participant Message
User->>InputClass: Provide input (Data, DataFrame, Message, dict, etc.)
InputClass->>InputClass: Validate and convert input
alt Input is DataFrame
InputClass->>Data: Convert DataFrame to Data (if needed)
InputClass->>Message: Convert DataFrame to markdown text (if needed)
else Input is Data
InputClass->>DataFrame: Convert Data to DataFrame (if needed)
InputClass->>Message: Extract text or convert to string (if needed)
else Input is Message
InputClass->>Data: Convert Message to Data (if needed)
InputClass->>DataFrame: Convert Message to DataFrame (if needed)
else Input is dict
InputClass->>Data: Convert dict to Data
InputClass->>DataFrame: Convert dict to DataFrame
InputClass->>Message: Convert dict to Message
end
InputClass-->>User: Return standardized internal representation
Possibly related PRs
Suggested labels
Suggested reviewers
✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
♻️ Duplicate comments (1)
src/backend/base/langflow/inputs/inputs.py (1)
292-303: Duplicate transformation pipeline (see previous comment).Same rationale as above – please delegate to a shared utility.
🧹 Nitpick comments (2)
src/backend/base/langflow/inputs/inputs.py (1)
237-249: Data-frame-to-markdown logic duplicated – extract a helper.The exact same cleansing pipeline appears here and in
MessageTextInput. Encapsulate it (e.g.langflow.utils.df_to_markdown(df)) to:
- avoid divergence bugs,
- make unit tests simpler,
- keep inputs.py lean.
No functional breakage, but worth an internal refactor.
src/backend/tests/unit/io/test_data_convertion.py (1)
1-3: Typo in filename and module docstring – use “conversion”.
test_data_conversion.pymatches conventional spelling and grepping patterns.🧰 Tools
🪛 Pylint (3.3.7)
[error] 2-2: No name 'inputs' in module 'langflow'
(E0611)
[error] 3-3: No name 'schema' in module 'langflow'
(E0611)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/backend/base/langflow/inputs/inputs.py(6 hunks)src/backend/tests/unit/io/test_data_convertion.py(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/backend/base/langflow/inputs/inputs.py (3)
src/backend/base/langflow/schema/data.py (1)
Data(23-275)src/backend/base/langflow/schema/dataframe.py (1)
DataFrame(11-206)src/backend/base/langflow/schema/message.py (1)
Message(38-288)
🪛 Pylint (3.3.7)
src/backend/tests/unit/io/test_data_convertion.py
[error] 2-2: No name 'inputs' in module 'langflow'
(E0611)
[error] 3-3: No name 'schema' in module 'langflow'
(E0611)
⏰ Context from checks skipped due to timeout of 90000ms (4)
- GitHub Check: Optimize new Python code in this PR
- GitHub Check: Run Ruff Check and Format
- GitHub Check: Update Starter Projects
- GitHub Check: Ruff Style Check (3.13)
| if isinstance(v, DataFrame): | ||
| # Convert DataFrame to a list of dictionaries and wrap in a Data object | ||
| dict_list = v.to_dict(orient="records") | ||
| return Data(data={"results": dict_list}) | ||
| if isinstance(v, Message): | ||
| return Data(data=v.data) | ||
| if isinstance(v, dict): | ||
| return Data(data=v) | ||
| if not isinstance(v, Data): | ||
| msg = f"Invalid value type {type(v)} for input {info.data.get('name')}. Expected Data." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Message text gets silently discarded when converting Message → Data.
Message.data does not include the text field, therefore the current conversion loses the core payload.
- if isinstance(v, Message):
- return Data(data=v.data)
+ if isinstance(v, Message):
+ # Preserve the textual content together with the auxiliary payload
+ return Data(data={"text": v.text, **(v.data or {})})📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| if isinstance(v, DataFrame): | |
| # Convert DataFrame to a list of dictionaries and wrap in a Data object | |
| dict_list = v.to_dict(orient="records") | |
| return Data(data={"results": dict_list}) | |
| if isinstance(v, Message): | |
| return Data(data=v.data) | |
| if isinstance(v, dict): | |
| return Data(data=v) | |
| if not isinstance(v, Data): | |
| msg = f"Invalid value type {type(v)} for input {info.data.get('name')}. Expected Data." | |
| if isinstance(v, DataFrame): | |
| # Convert DataFrame to a list of dictionaries and wrap in a Data object | |
| dict_list = v.to_dict(orient="records") | |
| return Data(data={"results": dict_list}) | |
| if isinstance(v, Message): | |
| # Preserve the textual content together with the auxiliary payload | |
| return Data(data={"text": v.text, **(v.data or {})}) | |
| if isinstance(v, dict): | |
| return Data(data=v) | |
| if not isinstance(v, Data): | |
| msg = f"Invalid value type {type(v)} for input {info.data.get('name')}. Expected Data." |
🤖 Prompt for AI Agents
In src/backend/base/langflow/inputs/inputs.py around lines 115 to 124, when
converting a Message instance to Data, the current code only copies Message.data
and discards the Message.text field, losing important information. Modify the
conversion to include the Message.text field along with Message.data when
creating the Data object, ensuring the text payload is preserved in the
resulting Data.
| input_types: list[str] = ["DataFrame", "Data", "Message"] | ||
|
|
||
| @field_validator("value") | ||
| @classmethod | ||
| def validate_value(cls, v: DataFrame | Data | Message | dict, info) -> DataFrame: | ||
| if isinstance(v, Data): | ||
| data_dict = v.data | ||
| # If data contains only one key and the value is a list of dictionaries, convert to DataFrame | ||
| if ( | ||
| len(data_dict) == 1 | ||
| and isinstance(next(iter(data_dict.values())), list) | ||
| and all(isinstance(item, dict) for item in next(iter(data_dict.values()))) | ||
| ): | ||
| return DataFrame(data=next(iter(data_dict.values()))) | ||
| return DataFrame(data=[v]) | ||
| if isinstance(v, Message): | ||
| return DataFrame(data=[v]) | ||
| if isinstance(v, dict): | ||
| return DataFrame(data=[v]) | ||
| if not isinstance(v, DataFrame): | ||
| msg = f"Invalid value type {type(v)} for input {info.data.get('name')}. Expected DataFrame." | ||
| raise ValueError(msg) # noqa: TRY004 | ||
| return v |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DataFrameInput.validate_value builds unusable frames for Data / Message.
pandas.DataFrame([v]) where v is a Data/Message instance produces a single-column dataframe with an opaque object – breaking every downstream expectation (your own tests access a "key" column).
if isinstance(v, Data):
@@
- return DataFrame(data=[v])
+ return DataFrame(data=[v.data])
if isinstance(v, Message):
- return DataFrame(data=[v])
+ row = {"text": v.text, **(v.data or {})}
+ return DataFrame(data=[row])Likewise, validate that a dict with list-of-dict values still passes through the earlier branch.
This fix aligns the method with the unit-test expectations.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| input_types: list[str] = ["DataFrame", "Data", "Message"] | |
| @field_validator("value") | |
| @classmethod | |
| def validate_value(cls, v: DataFrame | Data | Message | dict, info) -> DataFrame: | |
| if isinstance(v, Data): | |
| data_dict = v.data | |
| # If data contains only one key and the value is a list of dictionaries, convert to DataFrame | |
| if ( | |
| len(data_dict) == 1 | |
| and isinstance(next(iter(data_dict.values())), list) | |
| and all(isinstance(item, dict) for item in next(iter(data_dict.values()))) | |
| ): | |
| return DataFrame(data=next(iter(data_dict.values()))) | |
| return DataFrame(data=[v]) | |
| if isinstance(v, Message): | |
| return DataFrame(data=[v]) | |
| if isinstance(v, dict): | |
| return DataFrame(data=[v]) | |
| if not isinstance(v, DataFrame): | |
| msg = f"Invalid value type {type(v)} for input {info.data.get('name')}. Expected DataFrame." | |
| raise ValueError(msg) # noqa: TRY004 | |
| return v | |
| input_types: list[str] = ["DataFrame", "Data", "Message"] | |
| @field_validator("value") | |
| @classmethod | |
| def validate_value(cls, v: DataFrame | Data | Message | dict, info) -> DataFrame: | |
| if isinstance(v, Data): | |
| data_dict = v.data | |
| # If data contains only one key and the value is a list of dictionaries, convert to DataFrame | |
| if ( | |
| len(data_dict) == 1 | |
| and isinstance(next(iter(data_dict.values())), list) | |
| and all(isinstance(item, dict) for item in next(iter(data_dict.values()))) | |
| ): | |
| return DataFrame(data=next(iter(data_dict.values()))) | |
| return DataFrame(data=[v.data]) | |
| if isinstance(v, Message): | |
| row = {"text": v.text, **(v.data or {})} | |
| return DataFrame(data=[row]) | |
| if isinstance(v, dict): | |
| return DataFrame(data=[v]) | |
| if not isinstance(v, DataFrame): | |
| msg = f"Invalid value type {type(v)} for input {info.data.get('name')}. Expected DataFrame." | |
| raise ValueError(msg) # noqa: TRY004 | |
| return v |
🤖 Prompt for AI Agents
In src/backend/base/langflow/inputs/inputs.py lines 130 to 152, the
validate_value method incorrectly constructs DataFrames from Data or Message
instances by wrapping them in a list, resulting in single-column DataFrames with
opaque objects. To fix this, modify the method to detect when a Data instance's
data dictionary contains a single key with a list of dictionaries as its value
and convert that list directly into a DataFrame. For Message and dict inputs,
ensure they are converted into DataFrames with proper structure rather than
wrapped as opaque objects. Also, validate that dicts with list-of-dict values
are handled by the earlier branch to maintain consistency with unit tests.
Improve input validation for Data, DataFrame, and Message types while adding unit tests to ensure proper data conversion functionality.
Summary by CodeRabbit
New Features
Bug Fixes
Tests