Skip to content

Commit 85787cf

Browse files
fix: fixed the parser component (#7303)
* fix: fixed the parser component * fix: fixed parser component test * fix: ruff error * chore: rename input * chore: remove beta attribute * fix: remove name attribute --------- Co-authored-by: Edwin Jose <[email protected]>
1 parent 6c36f48 commit 85787cf

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

src/backend/base/langflow/components/processing/parser.py

+20-18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
from typing import Any
23

34
from langflow.custom import Component
@@ -7,6 +8,7 @@
78
MessageTextInput,
89
MultilineInput,
910
Output,
11+
TabInput,
1012
)
1113
from langflow.schema import Data, DataFrame
1214
from langflow.schema.message import Message
@@ -19,18 +21,18 @@ class ParserComponent(Component):
1921
"Enable 'Stringify' to convert input into a readable string instead."
2022
)
2123
icon = "braces"
22-
beta = True
2324

2425
inputs = [
25-
BoolInput(
26-
name="stringify",
27-
display_name="Stringify",
28-
info="Enable to convert input to a string instead of using a template.",
29-
value=False,
26+
TabInput(
27+
name="mode",
28+
display_name="Mode",
29+
options=["Parser", "Stringify"],
30+
value="Parser",
31+
info="Convert into raw string instead of using a template.",
3032
real_time_refresh=True,
3133
),
3234
MultilineInput(
33-
name="template",
35+
name="pattern",
3436
display_name="Template",
3537
info=(
3638
"Use variables within curly brackets to extract column values for DataFrames "
@@ -69,9 +71,9 @@ class ParserComponent(Component):
6971

7072
def update_build_config(self, build_config, field_value, field_name=None):
7173
"""Dynamically hide/show `template` and enforce requirement based on `stringify`."""
72-
if field_name == "stringify":
73-
build_config["template"]["show"] = not field_value
74-
build_config["template"]["required"] = not field_value
74+
if field_name == "mode":
75+
build_config["pattern"]["show"] = self.mode == "Parser"
76+
build_config["pattern"]["required"] = self.mode == "Parser"
7577
if field_value:
7678
clean_data = BoolInput(
7779
name="clean_data",
@@ -118,18 +120,18 @@ def _clean_args(self):
118120
def parse_combined_text(self) -> Message:
119121
"""Parse all rows/items into a single text or convert input to string if `stringify` is enabled."""
120122
# Early return for stringify option
121-
if self.stringify:
123+
if self.mode == "Stringify":
122124
return self.convert_to_string()
123125

124126
df, data = self._clean_args()
125127

126128
lines = []
127129
if df is not None:
128130
for _, row in df.iterrows():
129-
formatted_text = self.template.format(**row.to_dict())
131+
formatted_text = self.pattern.format(**row.to_dict())
130132
lines.append(formatted_text)
131133
elif data is not None:
132-
formatted_text = self.template.format(text=data.get_text())
134+
formatted_text = self.pattern.format(**data.data)
133135
lines.append(formatted_text)
134136

135137
combined_text = self.sep.join(lines)
@@ -144,10 +146,7 @@ def _safe_convert(self, data: Any) -> str:
144146
if isinstance(data, Message):
145147
return data.get_text()
146148
if isinstance(data, Data):
147-
if data.get_text() is None:
148-
msg = "Empty Data object"
149-
raise ValueError(msg)
150-
return data.get_text()
149+
return json.dumps(data.data)
151150
if isinstance(data, DataFrame):
152151
if hasattr(self, "clean_data") and self.clean_data:
153152
# Remove empty rows
@@ -170,4 +169,7 @@ def convert_to_string(self) -> Message:
170169
else:
171170
result = self._safe_convert(self.input_data)
172171
self.log(f"Converted to string with length: {len(result)}")
173-
return Message(text=result)
172+
173+
message = Message(text=result)
174+
self.status = message
175+
return message

src/backend/tests/unit/components/processing/test_parser_component.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def default_kwargs(self):
1717
"""Return the default kwargs for the component."""
1818
return {
1919
"input_data": DataFrame({"Name": ["John"], "Age": [30], "Country": ["USA"]}),
20-
"template": "Name: {Name}, Age: {Age}, Country: {Country}",
20+
"pattern": "Name: {Name}, Age: {Age}, Country: {Country}",
2121
"sep": "\n",
2222
"stringify": False,
2323
"clean_data": False,
@@ -44,7 +44,7 @@ def test_parse_data_object(self, component_class):
4444
data = Data(text="Hello World")
4545
kwargs = {
4646
"input_data": data,
47-
"template": "text: {text}",
47+
"pattern": "text: {text}",
4848
"sep": "\n",
4949
"stringify": False,
5050
}
@@ -62,7 +62,7 @@ def test_stringify_dataframe(self, component_class):
6262
data_frame = DataFrame({"Name": ["John", "Jane"], "Age": [30, 25]})
6363
kwargs = {
6464
"input_data": data_frame,
65-
"stringify": True,
65+
"mode": "Stringify",
6666
"clean_data": False,
6767
}
6868
component = component_class(**kwargs)
@@ -101,7 +101,7 @@ def test_stringify_message_object(self, component_class):
101101
message = Message(text="Test message content")
102102
kwargs = {
103103
"input_data": message,
104-
"stringify": True,
104+
"mode": "Stringify",
105105
}
106106
component = component_class(**kwargs)
107107

@@ -119,7 +119,7 @@ def test_clean_data_with_stringify(self, component_class):
119119
)
120120
kwargs = {
121121
"input_data": data_frame,
122-
"stringify": True,
122+
"mode": "Stringify",
123123
"clean_data": True,
124124
}
125125
component = component_class(**kwargs)
@@ -150,7 +150,7 @@ def test_invalid_input_type(self, component_class):
150150
# Arrange
151151
kwargs = {
152152
"input_data": 123, # Invalid input type
153-
"template": "{value}",
153+
"pattern": "{value}",
154154
"sep": "\n",
155155
}
156156
component = component_class(**kwargs)
@@ -163,7 +163,7 @@ def test_none_input(self, component_class):
163163
# Arrange
164164
kwargs = {
165165
"input_data": None,
166-
"template": "{value}",
166+
"pattern": "{value}",
167167
"sep": "\n",
168168
}
169169
component = component_class(**kwargs)
@@ -177,7 +177,7 @@ def test_invalid_template(self, component_class):
177177
data_frame = DataFrame({"Name": ["John"]})
178178
kwargs = {
179179
"input_data": data_frame,
180-
"template": "{InvalidColumn}", # Invalid column name
180+
"pattern": "{InvalidColumn}", # Invalid column name
181181
"sep": "\n",
182182
"stringify": False,
183183
}
@@ -197,9 +197,9 @@ def test_multiple_rows_with_custom_separator(self, component_class):
197197
)
198198
kwargs = {
199199
"input_data": data_frame,
200-
"template": "{Name} is {Age} years old",
200+
"pattern": "{Name} is {Age} years old",
201201
"sep": " | ",
202-
"stringify": False,
202+
"mode": "Parser",
203203
}
204204
component = component_class(**kwargs)
205205

0 commit comments

Comments
 (0)