Outputs
Text Output and Chat Output components define where data exits your flow. They can send data to the user or the Playground, or define how the data is presented in the Playground.
-
Text Output takes
Text`input with no additional formatting or metadata and creates a `Message
object. -
Chat Output handles multiple input types and creates a
Message
object that includes chat-specific features like message history and sender information.
Chat Output
This component displays chat messages in the Playground.
The Chat Output component creates a Message
object that includes the input text, sender information, session ID, and styling properties.
It can optionally store the message in a chat history.
The component accepts the following input types.
-
Data
-
DataFrame
-
Message
Parameters
Name | Display Name | Info |
---|---|---|
input_value |
Text |
The message to be passed as output. |
should_store_message |
Store Messages |
The flag to store the message in the history. |
sender |
Sender Type |
The type of sender. |
sender_name |
Sender Name |
The name of the sender. |
session_id |
Session ID |
The session ID of the chat. If this option is empty, the current session ID parameter is used. |
data_template |
Data Template |
The template to convert Data to Text. If this option is empty, it is dynamically set to the Data’s text key. |
background_color |
Background Color |
The background color of the icon. |
chat_icon |
Icon |
The icon of the message. |
text_color |
Text Color |
The text color of the name. |
clean_data |
Basic Clean Data |
When enabled, |
Name | Display Name | Info |
---|---|---|
message |
Message |
The resulting chat message object with all specified properties. |
Component code
chat.py
from collections.abc import Generator
from typing import Any
from langflow.base.io.chat import ChatComponent
from langflow.inputs import BoolInput
from langflow.inputs.inputs import HandleInput
from langflow.io import DropdownInput, MessageTextInput, Output
from langflow.schema.data import Data
from langflow.schema.dataframe import DataFrame
from langflow.schema.message import Message
from langflow.schema.properties import Source
from langflow.utils.constants import (
MESSAGE_SENDER_AI,
MESSAGE_SENDER_NAME_AI,
MESSAGE_SENDER_USER,
)
class ChatOutput(ChatComponent):
display_name = "Chat Output"
description = "Display a chat message in the Playground."
icon = "MessagesSquare"
name = "ChatOutput"
minimized = True
inputs = [
HandleInput(
name="input_value",
display_name="Text",
info="Message to be passed as output.",
input_types=["Data", "DataFrame", "Message"],
required=True,
),
BoolInput(
name="should_store_message",
display_name="Store Messages",
info="Store the message in the history.",
value=True,
advanced=True,
),
DropdownInput(
name="sender",
display_name="Sender Type",
options=[MESSAGE_SENDER_AI, MESSAGE_SENDER_USER],
value=MESSAGE_SENDER_AI,
advanced=True,
info="Type of sender.",
),
MessageTextInput(
name="sender_name",
display_name="Sender Name",
info="Name of the sender.",
value=MESSAGE_SENDER_NAME_AI,
advanced=True,
),
MessageTextInput(
name="session_id",
display_name="Session ID",
info="The session ID of the chat. If empty, the current session ID parameter will be used.",
advanced=True,
),
MessageTextInput(
name="data_template",
display_name="Data Template",
value="{text}",
advanced=True,
info="Template to convert Data to Text. If left empty, it will be dynamically set to the Data's text key.",
),
MessageTextInput(
name="background_color",
display_name="Background Color",
info="The background color of the icon.",
advanced=True,
),
MessageTextInput(
name="chat_icon",
display_name="Icon",
info="The icon of the message.",
advanced=True,
),
MessageTextInput(
name="text_color",
display_name="Text Color",
info="The text color of the name",
advanced=True,
),
BoolInput(
name="clean_data",
display_name="Basic Clean Data",
value=True,
info="Whether to clean the data",
advanced=True,
),
]
outputs = [
Output(
display_name="Message",
name="message",
method="message_response",
),
]
def _build_source(self, id_: str | None, display_name: str | None, source: str | None) -> Source:
source_dict = {}
if id_:
source_dict["id"] = id_
if display_name:
source_dict["display_name"] = display_name
if source:
# Handle case where source is a ChatOpenAI object
if hasattr(source, "model_name"):
source_dict["source"] = source.model_name
elif hasattr(source, "model"):
source_dict["source"] = str(source.model)
else:
source_dict["source"] = str(source)
return Source(**source_dict)
async def message_response(self) -> Message:
# First convert the input to string if needed
text = self.convert_to_string()
# Get source properties
source, icon, display_name, source_id = self.get_properties_from_source_component()
background_color = self.background_color
text_color = self.text_color
if self.chat_icon:
icon = self.chat_icon
# Create or use existing Message object
if isinstance(self.input_value, Message):
message = self.input_value
# Update message properties
message.text = text
else:
message = Message(text=text)
# Set message properties
message.sender = self.sender
message.sender_name = self.sender_name
message.session_id = self.session_id
message.flow_id = self.graph.flow_id if hasattr(self, "graph") else None
message.properties.source = self._build_source(source_id, display_name, source)
message.properties.icon = icon
message.properties.background_color = background_color
message.properties.text_color = text_color
# Store message if needed
if self.session_id and self.should_store_message:
stored_message = await self.send_message(message)
self.message.value = stored_message
message = stored_message
self.status = message
return message
def _validate_input(self) -> None:
"""Validate the input data and raise ValueError if invalid."""
if self.input_value is None:
msg = "Input data cannot be None"
raise ValueError(msg)
if isinstance(self.input_value, list) and not all(
isinstance(item, Message | Data | DataFrame | str) for item in self.input_value
):
invalid_types = [
type(item).__name__
for item in self.input_value
if not isinstance(item, Message | Data | DataFrame | str)
]
msg = f"Expected Data or DataFrame or Message or str, got {invalid_types}"
raise TypeError(msg)
if not isinstance(
self.input_value,
Message | Data | DataFrame | str | list | Generator | type(None),
):
type_name = type(self.input_value).__name__
msg = f"Expected Data or DataFrame or Message or str, Generator or None, got {type_name}"
raise TypeError(msg)
def _safe_convert(self, data: Any) -> str:
"""Safely convert input data to string."""
try:
if isinstance(data, str):
return data
if isinstance(data, Message):
return data.get_text()
if isinstance(data, Data):
if data.get_text() is None:
msg = "Empty Data object"
raise ValueError(msg)
return data.get_text()
if isinstance(data, DataFrame):
if self.clean_data:
# Remove empty rows
data = data.dropna(how="all")
# Remove empty lines in each cell
data = data.replace(r"^\s*$", "", regex=True)
# Replace multiple newlines with a single newline
data = data.replace(r"\n+", "\n", regex=True)
# Replace pipe characters to avoid markdown table issues
processed_data = data.replace(r"\|", r"\\|", regex=True)
processed_data = processed_data.map(
lambda x: str(x).replace("\n", "<br/>") if isinstance(x, str) else x
)
return processed_data.to_markdown(index=False)
return str(data)
except (ValueError, TypeError, AttributeError) as e:
msg = f"Error converting data: {e!s}"
raise ValueError(msg) from e
def convert_to_string(self) -> str | Generator[Any, None, None]:
"""Convert input data to string with proper error handling."""
self._validate_input()
if isinstance(self.input_value, list):
return "\n".join([self._safe_convert(item) for item in self.input_value])
if isinstance(self.input_value, Generator):
return self.input_value
return self._safe_convert(self.input_value)
Text Output
The Text output component takes a single input of text and returns a Message
object containing that text.
The output does not appear in the Playground.
Parameters
Name | Display Name | Info |
---|---|---|
input_value |
Text |
The text to be passed as output. |
Name | Display Name | Info |
---|---|---|
text |
Text |
The response message containing the input text. |
Component code
text.py
from langflow.base.io.text import TextComponent
from langflow.io import MultilineInput, Output
from langflow.schema.message import Message
class TextOutputComponent(TextComponent):
display_name = "Text Output"
description = "Display a text output in the Playground."
icon = "type"
name = "TextOutput"
inputs = [
MultilineInput(
name="input_value",
display_name="Text",
info="Text to be passed as output.",
),
]
outputs = [
Output(display_name="Message", name="text", method="text_response"),
]
def text_response(self) -> Message:
message = Message(
text=self.input_value,
)
self.status = self.input_value
return message