Step-by-Step Guides

FlowRunner Component

FlowRunner
Component

FlowRunner Component

The CustomComponent class allows us to create components that interact with Langflow itself. In this example, we will make a component that runs other flows available in "My Collection".

We will cover how to:

  • List Collection flows using the list_flows method.

  • Load a flow using the load_flow method.

  • Configure a dropdown input field using the options parameter.

The typical structure of a Custom Component is composed of display_name and description attributes, build and build_config methods.

from langflow import CustomComponent


class MyComponent(CustomComponent):
    display_name = "Custom Component"
    description = "This is a custom component"

    def build_config(self):
        ...

    def build(self):
        ...

Let's start by defining our component's display_name and description.

# from langflow import CustomComponent


class FlowRunner(CustomComponent):
    display_name = "Flow Runner"
    description = "Run other flows"

#    def build_config(self):
#        ...

#     def build(self):
#         ...

Second, we will import Document from the langchain.schema module. This will be the return type of the build method.

# from langflow import CustomComponent
from langchain.schema import Document

# class FlowRunner(CustomComponent):
#     display_name = "Flow Runner"
#     description = "Run other flows"

#    def build_config(self):
#        ...

#     def build(self):
#         ...

Now, let's add the parameters and the return type to the build method. The parameters added are:

  • flow_name is the name of the flow we want to run.

  • document is the input document to be passed to that flow.

    • Since Document is a Langchain type, it will add an input handle to the component (see more).

# from langflow import CustomComponent
from langchain.schema import Document

# class FlowRunner(CustomComponent):
#     display_name = "Flow Runner"
#     description = "Run other flows"

#    def build_config(self):
#        ...

   def build(self, flow_name: str, document: Document) -> Document:
        ...

We can now start writing the build method. Let's list available flows in "My Collection" using the list_flows method.

# from langflow import CustomComponent
# from langchain.schema import Document

# class FlowRunner(CustomComponent):
#     display_name = "Flow Runner"
#     description = "Run other flows"

#    def build_config(self):
#        ...

#    def build(self, flow_name: str, document: Document) -> Document:
        # List the flows
        flows = self.list_flows()

CAUTION

From version 0.4.0, names are unique, which was not the case in previous versions. This might lead to unexpected results if using flows with the same name.

And retrieve a flow that matches the selected name (we'll make a dropdown input field for the user to choose among flow names).

# from langflow import CustomComponent
# from langchain.schema import Document

# class FlowRunner(CustomComponent):
#     display_name = "Flow Runner"
#     description = "Run other flows"

#    def build_config(self):
#        ...

#    def build(self, flow_name: str, document: Document) -> Document:
        # List the flows
#       flows = self.list_flows()
        # Get the flow that matches the selected name
        # You can also get the flow by id
        # using self.get_flow(flow_id=flow_id)
        tweaks = {}
#       flow = self.get_flow(flow_name=flow_name, tweaks=tweaks)

You can load this flow using get_flow and set a tweaks dictionary to customize it. Find more about tweaks in our features guidelines.

from langflow import CustomComponent
from langchain.schema import Document


class FlowRunner(CustomComponent):
    display_name = "Flow Runner"
    description = "Run other flows using a document as input."

    def build_config(self):
        ...

    def build(self, flow_name: str, document: Document) -> Document:
        # List the flows
        flows = self.list_flows()
        # Get the flow that matches the selected name
        # You can also get the flow by id
        # using self.get_flow(flow_id=flow_id)
        tweaks = {}
        flow = self.get_flow(flow_name=flow_name, tweaks=tweaks)

We are using a Document as input because it is a straightforward way to pass text data in Langflow (specifically because you can connect it to many loaders). Generally, a flow will take a string or a dictionary as input because that's what LangChain components expect. In case you are passing a dictionary, you need to build it according to the needs of the flow you are using.

The content of a document can be extracted using the page_content attribute, which is a string, and passed as an argument to the selected flow.

from langflow import CustomComponent
from langchain.schema import Document


class FlowRunner(CustomComponent):
    display_name = "Flow Runner"
    description = "Run other flows using a document as input."

    def build_config(self):
        ...

    def build(self, flow_name: str, document: Document) -> Document:
        # List the flows
        flows = self.list_flows()
        # Get the flow that matches the selected name
        # You can also get the flow by id
        # using self.get_flow(flow_id=flow_id)
        tweaks = {}
        flow = self.get_flow(flow_name=flow_name, tweaks=tweaks)
        # Get the page_content from the document
        if document and isinstance(document, list):
            document = document[0]
        page_content = document.page_content
        # Use it in the flow
        result = flow(page_content)
        return Document(page_content=str(result))

CAUTION

Make sure that the field type is str and options values are strings.

Finally, we can add field customizations through the build_config method. Here we added the options key to make the flow_name field a dropdown menu. Check out the custom component reference for a list of available keys.

# from langchain.schema import Document


# class FlowRunner(CustomComponent):
#     display_name = "Flow Runner"
#     description = "Run other flows using a document as input."

    def build_config(self):
        flows = self.list_flows()
        flow_names = [f.name for f in flows]
        return {"flow_name": {"options": flow_names,
                                "display_name": "Flow Name",
        },
               "document": {"display_name": "Document"}
        }

#     def build(self, flow_name: str, document: Document) -> Document:
        # List the flows
#         flows = self.list_flows()
        # Get the flow that matches the selected name
        # You can also get the flow by id
        # using self.get_flow(flow_id=flow_id)

Done! This is what our script and custom component looks like:

Getting Started

👋 Welcome to Langflow
📦 How to install?
🤗 HuggingFace Spaces
🎨 Creating Flows

Guidelines

Sign up and Sign in
API Keys
Assynchronous Processing
Component
Features
Collection
Prompt Customization
Chat Interface
Chat Widget
Custom Components

Step-by-Step Guides

Async API
Integrating documents with prompt variables
Building chatbots with System Message
Integrating Langfuse with Langflow

Examples

FlowRunner Component
Conversation Chain
Buffer Memory
MidJourney Prompt Chain
CSV Loader
Serp API Tool
Multiple Vector Stores
Python Function
📚 How to Upload Examples?

Deployment

Deploy on Google Cloud Platform

Contributing

How to contribute?
GitHub Issues
Community

Search Docs…

Search Docs…