Skip to main content
For the complete documentation index for agents and LLMs, see llms.txt.

TransformersTextRouter

Route text strings to different pipeline branches based on a category label predicted by a Transformers text classification model.

Key Features

  • Uses a Hugging Face text classification model to classify input text.
  • Routes text to a named output matching the predicted label.
  • Automatically fetches labels from the model configuration if not provided.
  • Supports private Hugging Face models via API token.
  • Configurable device placement for GPU or CPU inference.

Configuration

  1. Drag the TransformersTextRouter component onto the canvas from the Component Library.
  2. Click on the component to open the configuration panel.
  3. On the General tab:
    • Set Model to the name or path of the Hugging Face text classification model (for example, cross-encoder/nli-deberta-v3-small).
    • Optionally, set Labels to the list of classification labels. If not provided, the component fetches the labels from the model's configuration on Hugging Face.
  4. Go to the Advanced tab to configure optional settings:
    • Set Device to specify the device for loading the model. If not set, the default device is selected automatically.
    • Set Token to provide a Hugging Face API token for downloading private models. Uses the HF_API_TOKEN or HF_TOKEN environment variable if set to True.
    • Set Hugging Face Pipeline Kwargs to pass additional keyword arguments when initializing the Hugging Face text classification pipeline.

Connections

TransformersTextRouter receives a single text string. For each label the model can predict, it creates a named output connection. Connect each label output to the appropriate downstream component.

The labels are specific to each model. You can find them in the model's description on Hugging Face.

Source Code

To check this component's source code, open transformers_text_router.py in the Haystack repository.

Usage Examples

Basic Configuration

  TransformersTextRouter:
type: components.routers.transformers_text_router.TransformersTextRouter
init_parameters: {}
components:
TransformersTextRouter:
type: components.routers.transformers_text_router.TransformersTextRouter
init_parameters:

Parameters

Inputs

ParameterTypeDescription
textstrA string of text to route.

Outputs

ParameterTypeDescription
(per label)strThe text routed to the output matching the predicted label.

Init Parameters

These are the parameters you can configure in Pipeline Builder:

ParameterTypeDefaultDescription
modelstrThe name or path of a Hugging Face model for text classification.
labelsOptional[List[str]]NoneThe list of labels. If not provided, the component fetches the labels from the model configuration on Hugging Face using transformers.AutoConfig.from_pretrained.
deviceOptional[ComponentDevice]NoneThe device for loading the model. If None, automatically selects the default device. If a device or device map is specified in huggingface_pipeline_kwargs, it overrides this parameter.
tokenOptional[Secret]Secret.from_env_var(['HF_API_TOKEN', 'HF_TOKEN'], strict=False)The API token for downloading private models from Hugging Face.
huggingface_pipeline_kwargsOptional[Dict[str, Any]]NoneA dictionary of keyword arguments for initializing the Hugging Face text classification pipeline.

Run Method Parameters

These are the parameters you can configure for the component's run() method. This means you can pass these parameters at query time through the API, in Playground, or when running a job. For details, see Modify Pipeline Parameters at Query Time.

ParameterTypeDefaultDescription
textstrA string of text to route.