Langflow Micro Tutorials — Enhanced Truncator

Rodrigo Nader
Langflow
Published in
2 min readSep 15, 2023

Welcome back to our Langflow micro tutorials series! We’re continuing with simple Langflow examples, emphasizing the design of custom components.

This article includes a link for downloading the described flow. Use it to modify and understand the components involved.

Today, we’re focusing on an enhanced version of our Document Truncator, demonstrating how to add options to a custom component. Enjoy!

Main Features

Enhanced Document Truncator: This custom component enables document truncation while preserving either the initial or final characters as determined by the truncate_size. The build_config method is used to customize the input fields, including the dropdown selection box.

Objective

The aim of this flow is to demonstrate the usefulness of field customization when creating custom components.

In this updated version of our custom component, we not only let the user decide how many chars to truncate but also add a dropdown field to decide how this process will happen — pass either the first or last truncate_size characters to the prompt template — which can be done by using the build_config method.

Here we created a str input field and assigned it to have options (meaning it is a dropdown selector) in the config dict. We also defined this field as required and created an info tooltip for it.

Below is the corresponding build_config function:

def build_config(self):
return {"how": { "options": ["first", "last"],
"required": True,
"info": "Select whether to retain the first or last characters as specified by the 'truncate_size'." }}

Finally, we also set the self.repr_value variable, which determines the content to be displayed in the component status icon. Here I chose to display the truncated text.

This status will show completely different outputs depending on the how field input.

Download Flow (gist)

--

--