Custom Components
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.CustomComponent
and contains abuild
method. - Use arguments with Type Annotations (or Type Hints) of the
build
method to create component fields. - If applicable, use the
build_config
method 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.
Params
- 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.
Methods
-
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
build
instance method are used to create the fields of the component.Supported Types str
,int
,float
,bool
,list
,dict
langflow.field_typing.NestedDict
langchain.chains.base.Chain
langchain.PromptTemplate
langchain.llms.base.BaseLLM
langchain.Tool
langchain.document_loaders.base.BaseLoader
langchain.schema.Document
langchain.text_splitters.TextSplitter
langchain.vectorstores.base.VectorStore
langchain.embeddings.base.Embeddings
langchain.schema.BaseRetriever
The difference between
dict
andlangflow.field_typing.NestedDict
is that one adds a simple key-value pair field, while the other opens a more robust dictionary editor.infoUnlike Langchain types, base Python types do not add a handle to the field by default. To add handles, use the
input_types
key in thebuild_config
method. -
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 also of type
dict
. They specify the behavior of the generated fields.
Below are the available keys used to configure component fields:
Key Description field_type: str
The type of the field (can be any of the types supported by the build
method).is_list: bool
If the field can be a list of values, meaning that the user can manually add more inputs to the same field. options: List[str]
When defined, the field becomes a dropdown menu where a list of strings defines the options to be displayed. If the value
attribute is set to one of the options, that option becomes default. For this parameter to work,field_type
should invariably bestr
.multiline: bool
Defines if a string field opens a text editor. Useful for longer texts. input_types: List[str]
Used when you want a str
field to have connectable handles.display_name: str
Defines the name of the field. advanced: bool
Hide the field in the canvas view (displayed component settings only). Useful when a field is for advanced users. password: bool
To mask the input text. Useful to hide sensitive text (e.g. API keys). required: bool
Makes the field required. info: str
Adds a tooltip to the field. file_types: List[str]
This is a requirement if the field_type
is file. Defines which file types will be accepted. For example, json, yaml or yml. -
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 list_flows
Returns a list of Flow objects with an id
and aname
.get_flow
Returns a Flow object. Parameters are flow_name
orflow_id
.load_flow
Loads a flow from a given id
. -
Useful attributes:
Attribute Name Description repr_value
Displays the value it receives in the build
method. Useful for debugging.infoCheck out the FlowRunner example to understand how to call a flow from a custom component.