Skip to main content

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".

Document Processor ComponentDocument Processor Component

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.
Example Code

_32
from langflow import CustomComponent
_32
from langchain.schema import Document
_32
_32
class FlowRunner(CustomComponent):
_32
display_name = "Flow Runner"
_32
description = "Run other flows using a document as input."
_32
_32
def build_config(self):
_32
flows = self.list_flows()
_32
flow_names = [f.name for f in flows]
_32
return {"flow_name": {"options": flow_names,
_32
"display_name": "Flow Name",
_32
},
_32
"document": {"display_name": "Document"}
_32
}
_32
_32
_32
def build(self, flow_name: str, document: Document) -> Document:
_32
# List the flows
_32
flows = self.list_flows()
_32
# Get the flow that matches the selected name
_32
# You can also get the flow by id
_32
# using self.get_flow(flow_id=flow_id)
_32
tweaks = {}
_32
flow = self.get_flow(flow_name=flow_name, tweaks=tweaks)
_32
# Get the page_content from the document
_32
if document and isinstance(document, list):
_32
document = document[0]
_32
page_content = document.page_content
_32
# Use it in the flow
_32
result = flow(page_content)
_32
return Document(page_content=str(result))


_12
from langflow import CustomComponent
_12
_12
_12
class MyComponent(CustomComponent):
_12
display_name = "Custom Component"
_12
description = "This is a custom component"
_12
_12
def build_config(self):
_12
...
_12
_12
def build(self):
_12
...
_12
_12
_12
_12
_12
_12
_12
_12

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


_12
from langflow import CustomComponent
_12
_12
_12
class FlowRunner(CustomComponent):
_12
display_name = "Flow Runner"
_12
description = "Run other flows"
_12
_12
def build_config(self):
_12
...
_12
_12
def build(self):
_12
...
_12
_12
_12
_12
_12
_12
_12
_12

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


_13
from langflow import CustomComponent
_13
from langchain.schema import Document
_13
_13
_13
class FlowRunner(CustomComponent):
_13
display_name = "Flow Runner"
_13
description = "Run other flows using a document as input."
_13
_13
def build_config(self):
_13
...
_13
_13
def build(self):
_13
...
_13
_13
_13
_13
_13
_13
_13

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


_13
from langflow import CustomComponent
_13
from langchain.schema import Document
_13
_13
_13
class FlowRunner(CustomComponent):
_13
display_name = "Flow Runner"
_13
description = "Run other flows using a document as input."
_13
_13
def build_config(self):
_13
...
_13
_13
def build(self, flow_name: str, document: Document) -> Document:
_13
...
_13
_13
_13
_13
_13
_13
_13

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).

_14
from langflow import CustomComponent
_14
from langchain.schema import Document
_14
_14
_14
class FlowRunner(CustomComponent):
_14
display_name = "Flow Runner"
_14
description = "Run other flows using a document as input."
_14
_14
def build_config(self):
_14
...
_14
_14
def build(self, flow_name: str, document: Document) -> Document:
_14
# List the flows
_14
flows = self.list_flows()
_14
_14
_14
_14
_14
_14

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


_19
from langflow import CustomComponent
_19
from langchain.schema import Document
_19
_19
_19
class FlowRunner(CustomComponent):
_19
display_name = "Flow Runner"
_19
description = "Run other flows using a document as input."
_19
_19
def build_config(self):
_19
...
_19
_19
def build(self, flow_name: str, document: Document) -> Document:
_19
# List the flows
_19
flows = self.list_flows()
_19
# Get the flow that matches the selected name
_19
# You can also get the flow by id
_19
# using self.get_flow(flow_id=flow_id)
_19
tweaks = {}
_19
flow = self.get_flow(flow_name=flow_name, tweaks=tweaks)
_19

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

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.


_19
from langflow import CustomComponent
_19
from langchain.schema import Document
_19
_19
_19
class FlowRunner(CustomComponent):
_19
display_name = "Flow Runner"
_19
description = "Run other flows using a document as input."
_19
_19
def build_config(self):
_19
...
_19
_19
def build(self, flow_name: str, document: Document) -> Document:
_19
# List the flows
_19
flows = self.list_flows()
_19
# Get the flow that matches the selected name
_19
# You can also get the flow by id
_19
# using self.get_flow(flow_id=flow_id)
_19
tweaks = {}
_19
flow = self.get_flow(flow_name=flow_name, tweaks=tweaks)
_19

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


_26
from langflow import CustomComponent
_26
from langchain.schema import Document
_26
_26
_26
class FlowRunner(CustomComponent):
_26
display_name = "Flow Runner"
_26
description = "Run other flows using a document as input."
_26
_26
def build_config(self):
_26
...
_26
_26
def build(self, flow_name: str, document: Document) -> Document:
_26
# List the flows
_26
flows = self.list_flows()
_26
# Get the flow that matches the selected name
_26
# You can also get the flow by id
_26
# using self.get_flow(flow_id=flow_id)
_26
tweaks = {}
_26
flow = self.get_flow(flow_name=flow_name, tweaks=tweaks)
_26
# Get the page_content from the document

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.


_32
from langflow import CustomComponent
_32
from langchain.schema import Document
_32
_32
_32
class FlowRunner(CustomComponent):
_32
display_name = "Flow Runner"
_32
description = "Run other flows using a document as input."
_32
_32
def build_config(self):
_32
flows = self.list_flows()
_32
flow_names = [f.name for f in flows]
_32
return {"flow_name": {"options": flow_names,
_32
"display_name": "Flow Name",
_32
},
_32
"document": {"display_name": "Document"}
_32
}
_32
_32
def build(self, flow_name: str, document: Document) -> Document:
_32
# List the flows
_32
flows = self.list_flows()

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.

caution

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

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

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

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

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).

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

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

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.

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

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.

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.

caution

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


_12
from langflow import CustomComponent
_12
_12
_12
class MyComponent(CustomComponent):
_12
display_name = "Custom Component"
_12
description = "This is a custom component"
_12
_12
def build_config(self):
_12
...
_12
_12
def build(self):
_12
...
_12
_12
_12
_12
_12
_12
_12
_12

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

Document Processor CodeDocument Processor Code
Document Processor ComponentDocument Processor Component

Hi, how can I help you?