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

TextLanguageRouter

Route text strings to different pipeline branches based on their detected language.

Key Features

  • Detects the language of a text string using langdetect.
  • Routes text to a named output matching the detected language.
  • Supports multiple languages configured at initialization.
  • Routes text that doesn't match any configured language to an unmatched output.

Configuration

  1. Drag the TextLanguageRouter component onto the canvas from the Component Library.
  2. Click on the component to open the configuration panel.
  3. On the General tab:
    • Set Languages to the list of ISO 639-1 language codes you want to route (for example, ["en", "de", "fr"]). If not specified, defaults to ["en"]. See the supported languages in the langdetect documentation.

Connections

TextLanguageRouter receives a single text string. For each language you configure, it creates a named output connection (for example, en or de). Text that doesn't match any configured language is routed to the unmatched output. Connect each language output to the appropriate downstream component.

Routing Documents by Language

To route documents (not text strings) by language, use DocumentLanguageClassifier followed by MetadataRouter.

Source Code

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

Usage Examples

Basic Configuration

  TextLanguageRouter:
type: components.routers.text_language_router.TextLanguageRouter
init_parameters: {}

To route documents by language instead of raw text, use DocumentLanguageClassifier followed by MetadataRouter.

components:
TextLanguageRouter:
type: components.routers.text_language_router.TextLanguageRouter
init_parameters:

Parameters

Inputs

ParameterTypeDescription
textstrA text string to route.

Outputs

ParameterTypeDescription
(per language code)strThe text routed to the output matching the detected language.
unmatchedstrThe text when its language doesn't match any configured language.

Init Parameters

These are the parameters you can configure in Pipeline Builder:

ParameterTypeDefaultDescription
languagesOptional[List[str]]NoneA list of ISO 639-1 language codes. See the supported languages in the langdetect documentation. If not specified, defaults to ["en"].

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 text string to route.