Skip to main content

Langflow objects

In Langflow, the Data and Message objects are Pydantic models that serve as structured, functional representations of data.

Data object

The Data object is a Pydantic model that serves as a container for storing and manipulating data. It carries data—a dictionary that can be accessed as attributes—and uses text_key to specify which key in the dictionary should be considered the primary text content.

  • Main Attributes:
    • text_key: Specifies the key to retrieve the primary text data.
    • data: A dictionary to store additional data.
    • default_value: default value when the text_key is not present in the data dictionary.

Create a Data Object

Create a Data object by directly assigning key-value pairs to it. For example:


_10
from langflow.schema import Data
_10
_10
# Creating a Data object with specified key-value pairs
_10
data = Data(text="my_string", bar=3, foo="another_string")
_10
_10
# Outputs:
_10
print(data.text) # Outputs: "my_string"
_10
print(data.bar) # Outputs: 3
_10
print(data.foo) # Outputs: "another_string"

The text_key specifies which key in the data dictionary should be considered the primary text content. The default_value provides a fallback if the text_key is not present.


_10
# Creating a Data object with a specific text_key and default_value
_10
data = Data(data={"title": "Hello, World!"}, text_key="content", default_value="No content available")
_10
_10
# Accessing the primary text using text_key and default_value
_10
print(data.get_text()) # Outputs: "No content available" because "content" key is not in the data dictionary
_10
_10
# Accessing data keys by calling the attribute directly
_10
print(data.title) # Outputs: "Hello, World!" because "title" key is in the data dictionary

The Data object is also convenient for visualization of outputs, since the output preview has visual elements to inspect data as a table and its cells as pop ups for basic types. The idea is to create a unified way to work and visualize complex information in Langflow.

To receive Data objects in a component input, use the DataInput input type.


_10
inputs = [
_10
DataInput(name="data", display_name="Data", info="Helpful info about the incoming data object.", is_list=True),
_10
]

Message object

The Message object extends the functionality of Data and includes additional attributes and methods for chat interactions.

  • Core message data:

    • text: The main text content of the message
    • sender: Identifier for the sender ("User" or "AI")
    • sender_name: Name of the sender
    • session_id: Identifier for the chat session (string or UUID)
    • timestamp: Timestamp when the message was created (UTC)
    • flow_id: Identifier for the flow (string or UUID)
    • id: Unique identifier for the message
  • Content and files:

    • files: List of files or images associated with the message
    • content_blocks: List of structured content block objects
    • properties: Additional properties including visual styling and source information
  • Message state:

    • error: Boolean indicating if there was an error
    • edit: Boolean indicating if the message was edited
    • category: Message category ("message", "error", "warning", "info")

The Message object can be used to send, store, and manipulate chat messages within Langflow.

Create a Message object

You can create a Message object by directly assigning key-value pairs to it. For example:


_10
from langflow.schema.message import Message
_10
_10
message = Message(text="Hello, AI!", sender="User", sender_name="John Doe")

To receive Message objects in a component input, you can use the MessageInput input type or MessageTextInput when the goal is to extract just the text field of the Message object.

ContentBlock object

The ContentBlock object is a list of multiple ContentTypes. It allows you to include multiple types of content within a single Message, including images, videos, and text.

Content types are Pydantic base classes constructed from the types in content_types.py.

Each content type has specific fields related to its data type. For example:

  • TextContent has a text field for storing strings of text
  • MediaContent has a urls field for storing media file URLs
  • CodeContent has code and language fields for code snippets
  • JSONContent has a data field for storing arbitrary JSON data
  • ToolContent has a tool_input field for storing input parameters for the tool

Create a ContentBlock object

Create a ContentBlock object with a list of different content types.


_10
content_block = ContentBlock(
_10
title="Mixed Content Example",
_10
contents=[
_10
TextContent(text="This is a text content"),
_10
MediaContent(urls=["http://example.com/image.jpg"]),
_10
JSONContent(data={"key": "value"}),
_10
CodeContent(code="print('Hello')", language="python")
_10
],
_10
media_url=["http://example.com/additional_image.jpg"]
_10
)

Add ContentBlocks objects to a message

In this example, a text and a media ContentBlock are added to a message.


_23
from langflow.schema.message import Message
_23
from langflow.schema.content_block import ContentBlock
_23
from langflow.schema.content_types import TextContent, MediaContent
_23
_23
message = Message(
_23
text="Main message text",
_23
sender="User",
_23
sender_name="John Doe",
_23
content_blocks=[
_23
ContentBlock(
_23
title="Text Block",
_23
contents=[
_23
TextContent(type="text", text="This is some text content")
_23
]
_23
),
_23
ContentBlock(
_23
title="Media Block",
_23
contents=[
_23
MediaContent(type="media", urls=["http://example.com/image.jpg"])
_23
]
_23
)
_23
]
_23
)

DataFrame object

The DataFrame class is a custom extension of the Pandas DataFrame class, specifically designed to work seamlessly with Langflow's Data objects. The class includes methods for converting between DataFrame and lists of Data objects.

A DataFrame object accepts various input formats, including lists of Data objects, dictionaries, and existing DataFrames.

Create a DataFrame object

You can create a DataFrame object using different data formats:


_31
from langflow.schema import Data
_31
from langflow.schema.data import DataFrame
_31
_31
# From a list of Data objects
_31
data_list = [Data(data={"name": "John"}), Data(data={"name": "Jane"})]
_31
df = DataFrame(data_list)
_31
_31
# From a list of dictionaries
_31
dict_list = [{"name": "John"}, {"name": "Jane"}]
_31
df = DataFrame(dict_list)
_31
_31
# From a dictionary of lists
_31
data_dict = {"name": ["John", "Jane"], "age": [30, 25]}
_31
df = DataFrame(data_dict)
_31
Key Methods
_31
to_data_list(): Converts the DataFrame back to a list of Data objects.
_31
add_row(data): Adds a single row (either a Data object or a dictionary) to the DataFrame.
_31
add_rows(data): Adds multiple rows (list of Data objects or dictionaries) to the DataFrame.
_31
Usage Example
_31
python
_31
# Create a DataFrame
_31
df = DataFrame([Data(data={"name": "John"}), Data(data={"name": "Jane"})])
_31
_31
# Add a new row
_31
df = df.add_row({"name": "Alice"})
_31
_31
# Convert back to a list of Data objects
_31
data_list = df.to_data_list()
_31
_31
# Use pandas functionality
_31
filtered_df = df[df["name"].str.startswith("J")]

To use DataFrame objects in a component input,use the DataFrameInput input type.


_10
DataFrameInput(
_10
name="dataframe_input", display_name="DataFrame Input", info="Input for DataFrame objects.", tool_mode=True
_10
),

Hi, how can I help you?