Guidelines

Custom Components

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.

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.

  • 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:

Here is an example:

from langflow import CustomComponent
from langchain.schema import Document

class DocumentProcessor(CustomComponent):
    display_name = "Document Processor"
    description = "This component processes a document"

    def build_config(self) -> dict:
        options = ["Uppercase", "Lowercase", "Titlecase"]
        return {
            "function": {"options": options,
                    "value": options[0]}}

    def build(self, document: Document, function: str) -> Document:
        if isinstance(document, list):
            document = document[0]
        page_content = document.page_content
        if function == "Uppercase":
            page_content = page_content.upper()
        elif function == "Lowercase":
            page_content = page_content.lower()
        elif function == "Titlecase":
            page_content = page_content.title()
        self.repr_value = f"Result of {function} function: {page_content}"
        return Document(page_content=page_content)

from langflow import CustomComponent
from langchain.schema import Document

class DocumentProcessor(CustomComponent):
    display_name = "Document Processor"
    description = "This component processes a document"

    def build_config(self) -> dict:
        options = ["Uppercase", "Lowercase", "Titlecase"]
        return {
            "function": {"options": options,
                    "value": options[0]}}

    def build(self, document: Document, function: str) -> Document:
        if isinstance(document, list):
            document = document[0]
        page_content = document.page_content
        if function == "Uppercase":
            page_content = page_content.upper()
        elif function == "Lowercase":
            page_content = page_content.lower()
        elif function == "Titlecase":
            page_content = page_content.title()
        self.repr_value = f"Result of {function} function: {page_content}"
        return Document(page_content=page_content)

from langflow import CustomComponent
from langchain.schema import Document

class DocumentProcessor(CustomComponent):
    display_name = "Document Processor"
    description = "This component processes a document"

    def build_config(self) -> dict:
        options = ["Uppercase", "Lowercase", "Titlecase"]
        return {
            "function": {"options": options,
                    "value": options[0]}}

    def build(self, document: Document, function: str) -> Document:
        if isinstance(document, list):
            document = document[0]
        page_content = document.page_content
        if function == "Uppercase":
            page_content = page_content.upper()
        elif function == "Lowercase":
            page_content = page_content.lower()
        elif function == "Titlecase":
            page_content = page_content.title()
        self.repr_value = f"Result of {function} function: {page_content}"
        return Document(page_content=page_content)

TIP

Check out FlowRunner Component for a more complex example.

Rules

Rule 1

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

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

from langflow import CustomComponent
from langchain.schema import Document

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

    def build_config(self) -> dict:
        ...

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

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

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

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

#    def build_config(self) -> dict:
#        ...

    def build(self) -> Document:
        ...

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.

from langflow import CustomComponent
from langchain.schema import Document

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

    def build_config(self) -> dict:
        ...

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

Examples

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.

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

class DocumentProcessor(CustomComponent):
    display_name = "Document Processor"
#     description = "This is a custom component"

#     def build_config(self) -> dict:
#         ...

#     def build(self) -> Document:
#         ...

Write a description

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

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

class DocumentProcessor(CustomComponent):
    display_name = "Document Processor"
    description = "This component processes a document"

#     def build_config(self) -> dict:
#        ...

#    def build(self) -> Document:
#        ...

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.

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

# class DocumentProcessor(CustomComponent):
#    display_name = "Document Processor"
#     description = "This component processes a document"

#     def build_config(self) -> dict:
#         ...

    def build(self, document: Document, function: str) -> Document:
        if isinstance(document, list):
            document = document[0]
        page_content = document.page_content
        if function == "Uppercase":
            page_content = page_content.upper()
        elif function == "Lowercase":
            page_content = page_content.lower()
        elif function == "Titlecase":
            page_content = page_content.title()
        self.repr_value = f"Result of {function} function: {page_content}"
        return Document(page_content=page_content)

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.

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

# class DocumentProcessor(CustomComponent):
#     display_name = "Document Processor"
#    description = "This component processes a document"

    def build_config(self) -> dict:
        options = ["Uppercase", "Lowercase", "Titlecase"]
        return {
            "function": {"options": options,
                     "value": options[0],
                     "display_name": "Function"
                        },
            "document": {"display_name": "Document"}
                }

#    def build(self, document: Document, function: str) -> Document:
#         if isinstance(document, list):
#             document = document[0]
#         page_content = document.page_content
#         if function == "Uppercase":
            # page_content = page_content.upper()
#         elif function == "Lowercase":
            # page_content = page_content.lower()
#         elif function == "Titlecase":
            # page_content = page_content.title()
        # self.repr_value = f"Result of {function} function: {page_content}"
#         return Document(page_content=page_content)

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

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:



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.

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

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

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!

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…