CTCaliberTek
Course: Langflow: Visual AI Builder (10 Modules) · Lesson 8 of 10Log in to track your progress and earn a certificate when you finish the course.
AI & Machine Learning

Langflow Module 8: Writing Custom Components in Python

Published September 15, 2026·Updated October 29, 2026·9 min read
Like SaveLog in to like, save, and track progress.

Introduction

When no built-in component does what you need, you can write your own in Python. A custom component is a class with declared inputs and outputs and a method that does the work. This module builds a small example and shows how to make Langflow load it.

Prerequisites

  • Basic Python classes
  • Module 3 (components and ports)

Step-by-Step Guide

Step 1: Look at the building blocks

A component is a class that extends Component. It declares inputs from the lfx.io module, such as StrInput, MultilineInput, IntInput, BoolInput, DropdownInput, and DataInput, and outputs with Output, each pointing at a method by name. The method does the work and returns a Message, Data, or DataFrame, which appear in the UI as a message, JSON, or a table.

Step 2: Write a simple component

This component counts words. Each input becomes an attribute on self, named after the input's name. To confirm the exact import for Message in your version, open the Code view of any built-in component and copy its imports.

from lfx.custom.custom_component.component import Component
from lfx.io import StrInput, Output
from lfx.schema.message import Message   # confirm in a built-in component's Code view


class WordCounter(Component):
    display_name = "Word Counter"
    description = "Counts the words in a piece of text."
    name = "word_counter"

    inputs = [
        StrInput(name="text_to_count", display_name="Text"),
    ]
    outputs = [
        Output(name="count", display_name="Word Count", method="count_words"),
    ]

    def count_words(self) -> Message:
        words = len(self.text_to_count.split())
        self.status = str(words) + " words"
        return Message(text=str(words))

Step 3: Avoid reserved input names

An input's name must not match any method already defined on Component, such as index, start, stop, run, log, validate, or variables. Starting in Langflow 1.12.4 this is an error, so pick specific names like text_to_count.

Step 4: Place the file where Langflow looks

Create a base folder with a category folder inside it, and put the component file there. The category folder needs an __init__.py that exports the class. Components must sit inside a category folder, no more than two levels deep.

custom_components/
  my_tools/
    __init__.py        # from .word_counter import WordCounter
                       # __all__ = ["WordCounter"]
    word_counter.py

Step 5: Point Langflow at the folder and restart

Set LANGFLOW_COMPONENTS_PATH to the base folder and restart Langflow. The component then appears in the components menu under your category. With Docker, mount the folder and set the variable.

export LANGFLOW_COMPONENTS_PATH="$PWD/custom_components"
uv run langflow run

# Docker
docker run -d --name langflow -p 7860:7860 \
  -v ./custom_components:/app/custom_components \
  -e LANGFLOW_COMPONENTS_PATH=/app/custom_components \
  langflowai/langflow:latest

FAQs

Can I edit a component's code directly in the UI?
Yes. The Code option in a component's header menu opens its source, which is also the best way to learn how built-in components are written.
Are custom components safe to allow for every user?
They run arbitrary Python on the server. If untrusted people can use your Langflow, restrict them with the LANGFLOW_ALLOW_CUSTOM_COMPONENTS setting and run Langflow in an isolated container.

Summary

A custom component declares inputs and outputs and implements the output method. Put it in a category folder, set LANGFLOW_COMPONENTS_PATH, restart, and avoid input names that clash with built-in methods.

Related tutorials

Comments

Log in or create a free account to join the discussion.

  • No comments yet. Be the first to comment.