Skip to main content

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 a build 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.
info

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
    langflow.field_typing.Prompt
    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 and langflow.field_typing.NestedDict is that one adds a simple key-value pair field, while the other opens a more robust dictionary editor.

    info

    To use the Prompt type, you must also add **kwargs to the build method. This is because the Prompt type 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_types key in the build_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 can be of type langflow.field_typing.TemplateField or dict. They specify the behavior of the generated fields.

    Below are the available keys used to configure component fields:

    KeyDescription
    field_type: strThe type of the field (can be any of the types supported by the build method).
    is_list: boolIf 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 be str.
    multiline: boolDefines 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: strDefines the name of the field.
    advanced: boolHide the field in the canvas view (displayed component settings only). Useful when a field is for advanced users.
    password: boolTo mask the input text. Useful to hide sensitive text (e.g. API keys).
    required: boolMakes the field required.
    info: strAdds 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.
    range_spec: langflow.field_typing.RangeSpecThis is a requirement if the field_type is 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 NameDescription
    list_flowsReturns a list of Flow objects with an id and a name.
    get_flowReturns a Flow object. Parameters are flow_name or flow_id.
    load_flowLoads a flow from a given id.
  • Useful attributes:

    Attribute NameDescription
    repr_valueDisplays the value it receives in the build method. Useful for debugging.
    info

    Check out the FlowRunner example to understand how to call a flow from a custom component.

Hi, how can I help you?