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

TransformersTextRouter

Route text strings to different connections based on a category label predicted by a Hugging Face text classification model. Use this component to branch pipelines by language, intent, or other categories defined by the model.

Key Features

  • Routes text to dynamically named outputs based on the predicted label.
  • Uses any Hugging Face text classification model.
  • Fetches label names from the model configuration when labels is not provided.
  • Supports configurable device placement and Hugging Face pipeline kwargs.

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:
    1. Set the model to a Hugging Face text classification model (for example, papluca/xlm-roberta-base-language-detection).
    2. Optionally set labels to override the labels fetched from the model configuration.
  4. Go to the Advanced tab to configure device, token, and huggingface_pipeline_kwargs.
note

The labels available as outputs depend on the model you choose. Check the model card on Hugging Face for the supported categories.

Connections

TransformersTextRouter accepts a text string as input. It outputs the text to a dynamically created output named after the predicted label. Connect each labeled output to the appropriate downstream component.

Source Code

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

Usage Examples

Basic Configuration

  TransformersTextRouter:
type: haystack_integrations.components.routers.transformers.text_router.TransformersTextRouter
init_parameters:
model: papluca/xlm-roberta-base-language-detection

Using the Component in a Pipeline

# haystack-pipeline
components:
TransformersTextRouter:
type: haystack_integrations.components.routers.transformers.text_router.TransformersTextRouter
init_parameters:
model: papluca/xlm-roberta-base-language-detection

english_prompt_builder:
type: haystack.components.builders.prompt_builder.PromptBuilder
init_parameters:
template: "Answer the question: {{query}}\nAnswer:"

german_prompt_builder:
type: haystack.components.builders.prompt_builder.PromptBuilder
init_parameters:
template: "Beantworte die Frage: {{query}}\nAntwort:"

connections:
- sender: TransformersTextRouter.en
receiver: english_prompt_builder.query
- sender: TransformersTextRouter.de
receiver: german_prompt_builder.query

max_runs_per_component: 100

metadata: {}

Parameters

Inputs

ParameterTypeDescription
textstrA string of text to route.

Outputs

ParameterTypeDescription
Dynamic label outputsstrOne output per model label. The output name matches the predicted label and carries the input text.

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 labels from the model configuration on Hugging Face.
deviceOptional[ComponentDevice]NoneThe device for loading the model. If None, automatically selects the default device.
tokenOptional[Secret]Secret.from_env_var(["HF_API_TOKEN", "HF_TOKEN"], strict=False)The API token used to download private models from Hugging Face.
huggingface_pipeline_kwargsOptional[Dict[str, Any]]NoneKeyword 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.