Used to create a custom component, a special type of Langflow component that allows users to extend the functionality of the platform by creating their own reusable and configurable components from a Python script.
To use a custom component, follow these steps:
- Create a class that inherits from
langflow.CustomComponentand contains a
- Use arguments with Type Annotations (or Type Hints) of the
buildmethod to create component fields.
- If applicable, use the
build_configmethod to customize how these fields look and behave.
For an in-depth explanation of custom components, their rules, and applications, make sure to read Custom Component guidelines.
- Code: The Python code to define the component.
The CustomComponent Class
The CustomComponent class serves as the foundation for creating custom components. By inheriting this class, users can create new, configurable components, tailored to their specific requirements.
build: This method is required within a Custom Component class. It defines the component's functionality and specifies how it processes input data to produce output data. This method is called when the component is built (i.e., when you click the Build ⚡ button in the canvas).
The type annotations of the
buildinstance method are used to create the fields of the component.
The difference between
langflow.field_typing.NestedDictis that one adds a simple key-value pair field, while the other opens a more robust dictionary editor.info
To use the
Prompttype, you must also add
buildmethod. This is because the
Prompttype passes new arbitrary keyword arguments to it.
If you want to add the values of the variables to the template you defined, you must format the PromptTemplate inside the CustomComponent class.info
Unlike Langchain types, base Python types do not add a handle to the field by default. To add handles, use the
input_typeskey in the
build_config: Used to define the configuration fields of the component (if applicable). It should always return a dictionary with specific keys representing the field names and corresponding configurations. This method is called when the code is processed (i.e., when you click Check and Save in the code editor). It must follow the format described below:
- Top-level keys are field names.
- Their values are can be of type
dict. They specify the behavior of the generated fields.
Below are the available keys used to configure component fields:
The type of the field (can be any of the types supported by the
If the field can be a list of values, meaning that the user can manually add more inputs to the same field.
When defined, the field becomes a dropdown menu where a list of strings defines the options to be displayed. If the
valueattribute is set to one of the options, that option becomes default. For this parameter to work,
field_typeshould invariably be
Defines if a string field opens a text editor. Useful for longer texts.
Used when you want a
strfield to have connectable handles.
Defines the name of the field.
Hide the field in the canvas view (displayed component settings only). Useful when a field is for advanced users.
To mask the input text. Useful to hide sensitive text (e.g. API keys).
Makes the field required.
Adds a tooltip to the field.
This is a requirement if the
field_typeis file. Defines which file types will be accepted. For example, json, yaml or yml.
This is a requirement if the
float. Defines the range of values accepted and the step size. If none is defined, the default is
[-1, 1, 0.1].
The CustomComponent class also provides helpful methods for specific tasks (e.g., to load and use other flows from the Langflow platform):
Method Name Description
Returns a list of Flow objects with an
Returns a Flow object. Parameters are
Loads a flow from a given
Attribute Name Description
Displays the value it receives in the
buildmethod. Useful for debugging.info
Check out the FlowRunner example to understand how to call a flow from a custom component.