Skip to main content

Custom Components

In Langflow, a Custom Component is a special component type that allows users to extend the platform's functionality by creating their own reusable and configurable components.

A Custom Component is created from a user-defined Python script that uses the CustomComponent class provided by the Langflow library. These components can be as simple as a basic function that takes and returns a string or as complex as a combination of multiple sub-components and API calls.

Let's take a look at the basic rules and features. Then we'll go over an example.

TL;DR

  • Create a class that inherits from CustomComponent and contains a build method.
  • Use arguments with Type Annotations (or Type Hints) of the build method to create component fields.
  • Use the build_config method to customize how these fields look and behave.
  • Set up a folder with your components to load them up in Langflow's sidebar.

Here is an example:


_25
from langflow import CustomComponent
_25
from langchain.schema import Document
_25
_25
class DocumentProcessor(CustomComponent):
_25
display_name = "Document Processor"
_25
description = "This component processes a document"
_25
_25
def build_config(self) -> dict:
_25
options = ["Uppercase", "Lowercase", "Titlecase"]
_25
return {
_25
"function": {"options": options,
_25
"value": options[0]}}
_25
_25
def build(self, document: Document, function: str) -> Document:
_25
if isinstance(document, list):
_25
document = document[0]
_25
page_content = document.page_content
_25
if function == "Uppercase":
_25
page_content = page_content.upper()
_25
elif function == "Lowercase":
_25
page_content = page_content.lower()
_25
elif function == "Titlecase":
_25
page_content = page_content.title()
_25
self.repr_value = f"Result of {function} function: {page_content}"
_25
return Document(page_content=page_content)

Document Processor ComponentDocument Processor Component
tip

Check out FlowRunner Component for a more complex example.


Rules

The Python script for every Custom Component should follow a set of rules. Let's go over them one by one:

Rule 1

The script must contain a single class that inherits from CustomComponent.


_12
from langflow import CustomComponent
_12
from langchain.schema import Document
_12
_12
class MyComponent(CustomComponent):
_12
display_name = "Custom Component"
_12
description = "This is a custom component"
_12
_12
def build_config(self) -> dict:
_12
...
_12
_12
def build(self, document: Document, function: str) -> Document:
_12
...
_12
_12
_12
_12
_12
_12
_12
_12

Rule 2

This class requires a build method used to run the component and define its fields.


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

The Return Type Annotation of the build method defines the component type (e.g., Chain, BaseLLM, or basic Python types). Check out all supported types in the component reference.


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


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

Rule 3

The class can have a build_config method, which defines configuration fields for the component. The build_config method should always return a dictionary with specific keys representing the field names and their corresponding configurations. It must follow the format described below:

  • Top-level keys are field names.
  • Their values are also of type dict. They specify the behavior of the generated fields.

Check out the component reference for more details on the available field configurations.


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

Example

Let's create a custom component that processes a document (langchain.schema.Document) using a simple function.

Pick a display name

To start, let's choose a name for our component by adding a display_name attribute. This name will appear on the canvas. The name of the class is not relevant, but let's call it DocumentProcessor.


_12
from langflow import CustomComponent
_12
from langchain.schema import Document
_12
_12
class DocumentProcessor(CustomComponent):
_12
display_name = "Document Processor"
_12
description = "This is a custom component"
_12
_12
def build_config(self) -> dict:
_12
...
_12
_12
def build(self) -> Document:
_12
...
_12
_12
_12
_12
_12
_12
_12
_12

Write a description

We can also write a description for it using a description attribute.


_12
from langflow import CustomComponent
_12
from langchain.schema import Document
_12
_12
class DocumentProcessor(CustomComponent):
_12
display_name = "Document Processor"
_12
description = "This component processes a document"
_12
_12
def build_config(self) -> dict:
_12
...
_12
_12
def build(self) -> Document:
_12
...
_12
_12
_12
_12
_12
_12
_12
_12


_22
from langflow import CustomComponent
_22
from langchain.schema import Document
_22
_22
class DocumentProcessor(CustomComponent):
_22
display_name = "Document Processor"
_22
description = "This component processes a document"
_22
_22
def build_config(self) -> dict:
_22
...
_22
_22
def build(self, document: Document, function: str) -> Document:
_22
if isinstance(document, list):
_22
document = document[0]
_22
page_content = document.page_content
_22
if function == "Uppercase":
_22
page_content = page_content.upper()
_22
elif function == "Lowercase":
_22
page_content = page_content.lower()
_22
elif function == "Titlecase":
_22
self.repr_value = f"Result of {function} function: {page_content}"

Add the build method

Here, the build method takes two input parameters: document, representing the input document to be processed, and function, a string representing the selected text transformation to be applied (either "Uppercase," "Lowercase," or "Titlecase"). The method processes the text content of the input Document based on the selected function.

The attribute repr_value is used to display the result of the component on the canvas. It is optional and can be used to display any string value.

The return type is Document.

Customize the component fields

The build_config method is here defined to customize the component fields.

  • options determines that the field will be a dropdown menu. The list values and field type must be str.
  • value is the default option of the dropdown menu.
  • display_name is the name of the field to be displayed.

_29
from langflow import CustomComponent
_29
from langchain.schema import Document
_29
_29
class DocumentProcessor(CustomComponent):
_29
display_name = "Document Processor"
_29
description = "This component processes a document"
_29
_29
def build_config(self) -> dict:
_29
options = ["Uppercase", "Lowercase", "Titlecase"]
_29
return {
_29
"function": {"options": options,
_29
"value": options[0],
_29
"display_name": "Function"
_29
},
_29
"document": {"display_name": "Document"}
_29
}
_29
_29
def build(self, document: Document, function: str) -> Document:
_29
if isinstance(document, list):
_29
self.repr_value = f"Result of {function} function: {page_content}"

Rule 1

The script must contain a single class that inherits from CustomComponent.

Rule 2

This class requires a build method used to run the component and define its fields.

The Return Type Annotation of the build method defines the component type (e.g., Chain, BaseLLM, or basic Python types). Check out all supported types in the component reference.

Rule 3

The class can have a build_config method, which defines configuration fields for the component. The build_config method should always return a dictionary with specific keys representing the field names and their corresponding configurations. It must follow the format described below:

  • Top-level keys are field names.
  • Their values are also of type dict. They specify the behavior of the generated fields.

Check out the component reference for more details on the available field configurations.

Example

Let's create a custom component that processes a document (langchain.schema.Document) using a simple function.

Pick a display name

To start, let's choose a name for our component by adding a display_name attribute. This name will appear on the canvas. The name of the class is not relevant, but let's call it DocumentProcessor.

Write a description

We can also write a description for it using a description attribute.

Add the build method

Here, the build method takes two input parameters: document, representing the input document to be processed, and function, a string representing the selected text transformation to be applied (either "Uppercase," "Lowercase," or "Titlecase"). The method processes the text content of the input Document based on the selected function.

The attribute repr_value is used to display the result of the component on the canvas. It is optional and can be used to display any string value.

The return type is Document.

Customize the component fields

The build_config method is here defined to customize the component fields.

  • options determines that the field will be a dropdown menu. The list values and field type must be str.
  • value is the default option of the dropdown menu.
  • display_name is the name of the field to be displayed.

_12
from langflow import CustomComponent
_12
from langchain.schema import Document
_12
_12
class MyComponent(CustomComponent):
_12
display_name = "Custom Component"
_12
description = "This is a custom component"
_12
_12
def build_config(self) -> dict:
_12
...
_12
_12
def build(self, document: Document, function: str) -> Document:
_12
...
_12
_12
_12
_12
_12
_12
_12
_12

All done! This is what our script and brand-new custom component look like:

Document Processor CodeDocument Processor Code
Document Processor ComponentDocument Processor Component

Loading Custom Components

For advanced customization, Langflow offers the option to create and load custom components outside of the standard interface. This process involves creating the desired components using a text editor and loading them using the Langflow CLI.

Folder Structure

Create a folder that follows the same structural conventions as the config.yaml file. Inside this main directory, use a custom_components subdirectory for your custom components.

Inside custom_components, you can create a Python file for each component. Similarly, any custom agents should be housed in an agents subdirectory.

If you use a subdirectory name that is not in our config.yaml file, your component will appear in an Other category in the sidebar.

Your structure should look something like this:


_10
.
_10
└── custom_components
_10
├── document_processor.py
_10
└── ...
_10
└── agents
_10
└── ...
_10
└── my_agents <-- Other category
_10
└── ...

Loading Custom Components

The recommended way to load custom components is to set the LANGFLOW_COMPONENTS_PATH environment variable to the path of your custom components directory. Then, run the Langflow CLI as usual.


_10
export LANGFLOW_COMPONENTS_PATH='["/path/to/components"]'
_10
langflow

Alternatively, you can specify the path to your custom components using the --components-path argument when running the Langflow CLI, as shown below:


_10
langflow --components-path /path/to/components

Langflow will attempt to load all of the components found in the specified directory. If a component fails to load due to errors in the component's code, Langflow will print an error message to the console but will continue loading the rest of the components.

Interacting with Custom Components

Once your custom components have been loaded successfully, they will appear in Langflow's sidebar. From there, you can add them to your Langflow canvas for use. However, please note that components with errors will not be available for addition to the canvas. Always ensure your code is error-free before attempting to load components.

Remember, creating custom components allows you to extend the functionality of Langflow to better suit your unique needs. Happy coding!

Hi, how can I help you?