TextLanguageRouter
Route text strings to different pipeline branches based on their detected language.
Key Features
- Detects the language of a text string using the langdetect library.
- Routes text to named output connections based on the detected language.
- Supports configurable list of target languages.
- Text in languages not in the configured list is routed to the
unmatchedoutput. - Useful for building multilingual query pipelines that need different processing per language.
Configuration
- Drag the
TextLanguageRoutercomponent onto the canvas from the Component Library. - Click on the component to open the configuration panel.
- On the General tab:
- Set
languagesto the list of ISO 639-1 language codes you want to support (for example,["en", "de"]). Each language creates a separate named output connection.
- Set
Connections
TextLanguageRouter receives a text string and routes it to one of its named output connections based on the detected language. Each configured language gets its own named output; text that doesn't match goes to the unmatched output. Connect each output to the appropriate retriever or processing component for that language.
Source Code
To check this component's source code, open text_language_router.py in the Haystack Core Integrations repository.
Usage Examples
Basic Configuration
TextLanguageRouter:
type: haystack_integrations.components.routers.langdetect.text_language_router.TextLanguageRouter
init_parameters:
languages:
- en
- de
Using the Component in a Pipeline
# haystack-pipeline
components:
TextLanguageRouter:
type: haystack_integrations.components.routers.langdetect.text_language_router.TextLanguageRouter
init_parameters:
languages:
- en
en_retriever:
type: haystack_integrations.components.retrievers.opensearch.bm25_retriever.OpenSearchBM25Retriever
init_parameters:
top_k: 10
document_store:
type: haystack_integrations.document_stores.opensearch.document_store.OpenSearchDocumentStore
init_parameters:
hosts:
index: english-index
connections:
- sender: TextLanguageRouter.en
receiver: en_retriever.query
max_runs_per_component: 100
metadata: {}
inputs:
query:
- TextLanguageRouter.text
Parameters
Inputs
| Parameter | Type | Description |
|---|---|---|
text | str | The text string to route based on its detected language. |
Outputs
The outputs are dynamic and depend on the languages configured at initialization. There is one output per language (named with the ISO code), plus an unmatched output for text in other languages.
| Parameter | Type | Description |
|---|---|---|
<language_code> | str | The input text, routed to the connection matching the detected language. |
unmatched | str | The input text, if its language is not in the configured list. |
Init Parameters
These are the parameters you can configure in Pipeline Builder:
| Parameter | Type | Default | Description |
|---|---|---|---|
languages | Optional[List[str]] | ["en"] | A list of ISO 639-1 language codes to route text to. Each code creates a named output connection. Text that doesn't match any configured language is routed to the unmatched output. |
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.
| Parameter | Type | Default | Description |
|---|---|---|---|
text | str | The text string to detect language for and route. |
Related Information
Was this page helpful?