Create custom components
Custom components are created within Langflow and extend the platform's functionality with custom, reusable Python code.
Since Langflow operates with Python behind the scenes, you can implement any Python function within a Custom Component. This means you can leverage the power of libraries such as Pandas, Scikit-learn, Numpy, and thousands of other packages to create components that handle data processing in unlimited ways. You can use any type as long as the type is properly annotated in the output methods (e.g., > list[int]
).
Custom Components create reusable and configurable components to enhance the capabilities of Langflow, making it a powerful tool for developing complex processing between user and AI messages.
Create a custom component in Langflow
Creating custom components in Langflow involves creating a Python class that defines the component's functionality, inputs, and outputs. The default code provides a working structure for your custom component.
_25# from langflow.field_typing import Data_25from langflow.custom import Component_25from langflow.io import MessageTextInput, Output_25from langflow.schema import Data_25_25_25class CustomComponent(Component):_25 display_name = "Custom Component"_25 description = "Use as a template to create your own component."_25 documentation: str = "http://docs.langflow.org/components/custom"_25 icon = "custom_components"_25 name = "CustomComponent"_25_25 inputs = [_25 MessageTextInput(name="input_value", display_name="Input Value", value="Hello, World!"),_25 ]_25_25 outputs = [_25 Output(display_name="Output", name="output", method="build_output"),_25 ]_25_25 def build_output(self) -> Data:_25 data = Data(value=self.input_value)_25 self.status = data_25 return data
You can create your class in your favorite text editor outside of Langflow and paste it in later, or just follow along in the code pane.
- In Langflow, from under Helpers, drag a Custom Component into the workspace.
- Open the component's code pane.
- Import dependencies.
Your custom component inherits from the langflow
Component
class so you need to include it.
_10from langflow.custom import Component_10from langflow.io import MessageTextInput, Output_10from langflow.schema import Data
- Define the Class: Start by defining a Python class that inherits from
Component
. This class will encapsulate the functionality of your custom component.
_10class CustomComponent(Component):_10 display_name = "Custom Component"_10 description = "Use as a template to create your own component."_10 documentation: str = "http://docs.langflow.org/components/custom"_10 icon = "custom_components"_10 name = "CustomComponent"
- Specify Inputs and Outputs: Use Langflow's input and output classes to define the inputs and outputs of your component. They should be declared as class attributes.
_10 inputs = [_10 MessageTextInput(name="input_value", display_name="Input Value", value="Hello, World!"),_10 ]_10_10 outputs = [_10 Output(display_name="Output", name="output", method="build_output"),_10 ]
- Implement Output Methods: Implement methods for each output, which contains the logic of your component. These methods can access input values using
self.<input_name>
, return processed values and define what to be displayed in the component with theself.status
attribute.
_10 def build_output(self) -> Data:_10 data = Data(value=self.input_value)_10 self.status = data_10 return data
- Use Proper Annotations: Ensure that output methods are properly annotated with their types. Langflow uses these annotations to validate and handle data correctly. For example, this method is annotated to output
Data
.
_10 def build_output(self) -> Data:
- Click Check & Save to confirm your component works. You now have an operational custom component.
Add inputs and modify output methods
This code defines a custom component that accepts 5 inputs and outputs a Message.
Copy and paste it into the Custom Component code pane and click Check & Save.
_55from langflow.custom import Component_55from langflow.inputs import StrInput, MultilineInput, SecretStrInput, IntInput, DropdownInput_55from langflow.template import Output, Input_55from langflow.schema.message import Message_55_55class MyCustomComponent(Component):_55 display_name = "My Custom Component"_55 description = "An example of a custom component with various input types."_55_55 inputs = [_55 StrInput(_55 name="username",_55 display_name="Username",_55 info="Enter your username."_55 ),_55 SecretStrInput(_55 name="password",_55 display_name="Password",_55 info="Enter your password."_55 ),_55 MessageTextInput(_55 name="special_message",_55 display_name="special_message",_55 info="Enter a special message.",_55 ),_55 IntInput(_55 name="age",_55 display_name="Age",_55 info="Enter your age."_55 ),_55 DropdownInput(_55 name="gender",_55 display_name="Gender",_55 options=["Male", "Female", "Other"],_55 info="Select your gender."_55 )_55 ]_55_55 outputs = [_55 Output(display_name="Result", name="result", method="process_inputs"),_55 ]_55_55 def process_inputs(self) -> Message:_55 """_55 Process the user inputs and return a Message object._55_55 Returns:_55 Message: A Message object containing the processed information._55 """_55 try:_55 processed_text = f"User {self.username} (Age: {self.age}, Gender: {self.gender}) " \_55 f"sent the following special message: {self.special_message}"_55 return Message(text=processed_text)_55 except AttributeError as e:_55 return Message(text=f"Error processing inputs: {str(e)}")
Since the component outputs a Message
, you can wire it into a chat and pass messages to yourself.
Your Custom Component accepts the Chat Input message through MessageTextInput
, fills in the variables with the process_inputs
method, and finally passes the message User Username (Age: 49, Gender: Male) sent the following special message: Hello!
to Chat Output.
By defining inputs this way, Langflow can automatically handle the validation and display of these fields in the user interface, making it easier to create robust and user-friendly custom components.
All of the types detailed above derive from a general class that can also be accessed through the generic Input
class.
Use MessageInput
to get the entire Message object instead of just the text.
Input Types
Langflow provides several higher-level input types to simplify the creation of custom components. These input types standardize how inputs are defined, validated, and used. Here’s a guide on how to use these inputs and their primary purposes:
HandleInput
Represents an input that has a handle to a specific type (e.g., BaseLanguageModel
, BaseRetriever
, etc.).
- Usage: Useful for connecting to specific component types in a flow.
DataInput
Represents an input that receives a Data
object.
- Usage: Ideal for components that process or manipulate data objects.
- Input Types:
["Data"]
StrInput
Represents a standard string input field.
- Usage: Used for any text input where the user needs to provide a string.
- Input Types:
["Text"]
MessageInput
Represents an input field specifically for Message
objects.
- Usage: Used in components that handle or process messages.
- Input Types:
["Message"]
MessageTextInput
Represents a text input for messages.
- Usage: Suitable for components that need to extract text from message objects.
- Input Types:
["Message"]
MultilineInput
Represents a text field that supports multiple lines.
- Usage: Ideal for longer text inputs where the user might need to write extended text.
- Input Types:
["Text"]
- Attributes:
multiline=True
SecretStrInput
Represents a password input field.
- Usage: Used for sensitive text inputs where the input should be hidden (e.g., passwords, API keys).
- Attributes:
password=True
- Input Types: Does not accept input types, meaning it has no input handles for previous nodes/components to connect to it.
IntInput
Represents an integer input field.
- Usage: Used for numeric inputs where the value should be an integer.
- Input Types:
["Integer"]
FloatInput
Represents a float input field.
- Usage: Used for numeric inputs where the value should be a floating-point number.
- Input Types:
["Float"]
BoolInput
Represents a boolean input field.
- Usage: Used for true/false or yes/no type inputs.
- Input Types:
["Boolean"]
NestedDictInput
Represents an input field for nested dictionaries.
- Usage: Used for more complex data structures where the input needs to be a dictionary.
- Input Types:
["NestedDict"]
DictInput
Represents an input field for dictionaries.
- Usage: Suitable for inputs that require a dictionary format.
- Input Types:
["Dict"]
DropdownInput
Represents a dropdown input field.
- Usage: Used where the user needs to select from a predefined list of options.
- Attributes:
options
to define the list of selectable options. - Input Types:
["Text"]
FileInput
Represents a file input field.
- Usage: Used to upload files.
- Attributes:
file_types
to specify the types of files that can be uploaded. - Input Types:
["File"]
Generic Input
Langflow offers native input types, but you can use any type as long as they are properly annotated in the output methods (e.g., -> list[int]
).
The Input
class is highly customizable, allowing you to specify a wide range of attributes for each input field. It has several attributes that can be customized:
field_type
: Specifies the type of field (e.g.,str
,int
). Default isstr
.required
: Boolean indicating if the field is required. Default isFalse
.placeholder
: Placeholder text for the input field. Default is an empty string.is_list
: Boolean indicating if the field should accept a list of values. Default isFalse
.show
: Boolean indicating if the field should be shown. Default isTrue
.multiline
: Boolean indicating if the field should allow multi-line input. Default isFalse
.value
: Default value for the input field. Default isNone
.file_types
: List of accepted file types (for file inputs). Default is an empty list.file_path
: File path if the field is a file input. Default isNone
.password
: Boolean indicating if the field is a password. Default isFalse
.options
: List of options for the field (for dropdowns). Default isNone
.name
: Name of the input field. Default isNone
.display_name
: Display name for the input field. Default isNone
.advanced
: Boolean indicating if the field is an advanced parameter. Default isFalse
.input_types
: List of accepted input types. Default isNone
.dynamic
: Boolean indicating if the field is dynamic. Default isFalse
.info
: Additional information or tooltip for the input field. Default is an empty string.real_time_refresh
: Boolean indicating if the field should refresh in real-time. Default isNone
.refresh_button
: Boolean indicating if the field should have a refresh button. Default isNone
.refresh_button_text
: Text for the refresh button. Default isNone
.range_spec
: Range specification for numeric fields. Default isNone
.load_from_db
: Boolean indicating if the field should load from the database. Default isFalse
.title_case
: Boolean indicating if the display name should be in title case. Default isTrue
.
Create a Custom Component with Generic Input
Here is an example of how to define inputs for a component using the Input
class.
Copy and paste it into the Custom Component code pane and click Check & Save.
_76from langflow.template import Input, Output_76from langflow.custom import Component_76from langflow.field_typing import Text_76from langflow.schema.message import Message_76from typing import Dict, Any_76_76class TextAnalyzerComponent(Component):_76 display_name = "Text Analyzer"_76 description = "Analyzes input text and provides basic statistics."_76_76 inputs = [_76 Input(_76 name="input_text",_76 display_name="Input Text",_76 field_type="Message",_76 required=True,_76 placeholder="Enter text to analyze",_76 multiline=True,_76 info="The text you want to analyze.",_76 input_types=["Text"]_76 ),_76 Input(_76 name="include_word_count",_76 display_name="Include Word Count",_76 field_type="bool",_76 required=False,_76 info="Whether to include word count in the analysis.",_76 ),_76 Input(_76 name="perform_sentiment_analysis",_76 display_name="Perform Sentiment Analysis",_76 field_type="bool",_76 required=False,_76 info="Whether to perform basic sentiment analysis.",_76 ),_76 ]_76_76 outputs = [_76 Output(display_name="Analysis Results", name="results", method="analyze_text"),_76 ]_76_76 def analyze_text(self) -> Message:_76 # Extract text from the Message object_76 if isinstance(self.input_text, Message):_76 text = self.input_text.text_76 else:_76 text = str(self.input_text)_76_76 results = {_76 "character_count": len(text),_76 "sentence_count": text.count('.') + text.count('!') + text.count('?')_76 }_76_76 if self.include_word_count:_76 results["word_count"] = len(text.split())_76_76 if self.perform_sentiment_analysis:_76 # Basic sentiment analysis_76 text_lower = text.lower()_76 if "happy" in text_lower or "good" in text_lower:_76 sentiment = "positive"_76 elif "sad" in text_lower or "bad" in text_lower:_76 sentiment = "negative"_76 else:_76 sentiment = "neutral"_76_76 results["sentiment"] = sentiment_76_76 # Convert the results dictionary to a formatted string_76 formatted_results = "\n".join([f"{key}: {value}" for key, value in results.items()])_76_76 # Return a Message object_76 return Message(text=formatted_results)_76_76# Define how to use the inputs and outputs_76component = TextAnalyzerComponent()
In this custom component:
-
The
input_text
input is a required multi-line text field that accepts a Message object or a string. It's used to provide the text for analysis. -
The
include_word_count
input is an optional boolean field. When set to True, it adds a word count to the analysis results. -
The
perform_sentiment_analysis
input is an optional boolean field. When set to True, it triggers a basic sentiment analysis of the input text.
The component performs basic text analysis, including character count and sentence count (based on punctuation marks). If word count is enabled, it splits the text and counts the words. If sentiment analysis is enabled, it performs a simple keyword-based sentiment classification (positive, negative, or neutral).
Since the component inputs and outputs a Message
, you can wire the component into a chat and see how the basic custom component logic interacts with your input.
Create a Custom Component with Multiple Outputs
In Langflow, custom components can have multiple outputs. Each output can be associated with a specific method in the component, allowing you to define distinct behaviors for each output path. This feature is particularly useful when you want to route data based on certain conditions or process it in multiple ways.
- Definition of Outputs: Each output is defined in the
outputs
list of the component. Each output is associated with a display name, an internal name, and a method that gets called to generate the output. - Output Methods: The methods associated with outputs are responsible for generating the data for that particular output. These methods are called when the component is executed, and each method can independently produce its result.
This example component has two outputs:
process_data
: Processes the input text (e.g., converts it to uppercase) and returns it.get_processing_function
: Returns theprocess_data
method itself to be reused in composition.
_33from typing import Callable_33from langflow.custom import Component_33from langflow.inputs import StrInput_33from langflow.template import Output_33from langflow.field_typing import Text_33_33class DualOutputComponent(Component):_33 display_name = "Dual Output"_33 description = "Processes input text and returns both the result and the processing function."_33 icon = "double-arrow"_33_33 inputs = [_33 StrInput(_33 name="input_text",_33 display_name="Input Text",_33 info="The text input to be processed.",_33 ),_33 ]_33_33 outputs = [_33 Output(display_name="Processed Data", name="processed_data", method="process_data"),_33 Output(display_name="Processing Function", name="processing_function", method="get_processing_function"),_33 ]_33_33 def process_data(self) -> Text:_33 # Process the input text (e.g., convert to uppercase)_33 processed = self.input_text.upper()_33 self.status = processed_33 return processed_33_33 def get_processing_function(self) -> Callable[[], Text]:_33 # Return the processing function itself_33 return self.process_data
This example shows how to define multiple outputs in a custom component. The first output returns the processed data, while the second output returns the processing function itself.
The processing_function
output can be used in scenarios where the function itself is needed for further processing or dynamic flow control. Notice how both outputs are properly annotated with their respective types, ensuring clarity and type safety.
Special Operations
Advanced methods and attributes offer additional control and functionality. Understanding how to leverage these can enhance your custom components' capabilities.
self.inputs
: Access all defined inputs. Useful when an output method needs to interact with multiple inputs.self.outputs
: Access all defined outputs. This is particularly useful if an output function needs to trigger another output function.self.status
: Use this to update the component's status or intermediate results. It helps track the component's internal state or store temporary data.self.graph.flow_id
: Retrieve the flow ID, useful for maintaining context or debugging.self.stop("output_name")
: Use this method within an output function to prevent data from being sent through other components. This method stops next component execution and is particularly useful for specific operations where a component should stop from running based on specific conditions.
Contribute Custom Components to Langflow
See How to Contribute to contribute your custom component to Langflow.