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

LLMMessagesRouter

Route chat messages to different pipeline branches using a language model for classification.

Key Features

  • Uses a language model to classify messages and route them to different outputs.
  • Works with general-purpose LLMs and specialized moderation models such as Llama Guard.
  • Routes messages based on configurable output patterns (regular expressions).
  • Supports a customizable system prompt to adjust the model's classification behavior.

Configuration

  1. Drag the LLMMessagesRouter component onto the canvas from the Component Library.
  2. Click on the component to open the configuration panel.
  3. On the General tab:
    • Set the Chat Generator to the LLM you want to use for classification. This can be any Haystack chat generator.
    • Set Output Names to the list of output connection names. These names are used to connect the router to other components.
    • Set Output Patterns to the list of regular expressions to match against the LLM's output. Each pattern corresponds to one output name. Patterns are evaluated in order. For moderation models, refer to the model card for the expected output format.
  4. Go to the Advanced tab to optionally set a System Prompt to customize the model's classification behavior. For moderation models, refer to the model card for supported customization.

Connections

LLMMessagesRouter receives a list of ChatMessage objects (only user and assistant messages are supported). Based on the LLM's classification output, it routes messages to the first output whose pattern matches. Connect each named output to the appropriate downstream component.

Source Code

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

Usage Examples

Basic Configuration

  LLMMessagesRouter:
type: components.routers.llm_messages_router.LLMMessagesRouter
init_parameters: {}
components:
LLMMessagesRouter:
type: components.routers.llm_messages_router.LLMMessagesRouter
init_parameters:

Parameters

Inputs

ParameterTypeDescription
messagesList[ChatMessage]A list of chat messages to route. Only user and assistant messages are supported.

Outputs

ParameterTypeDescription
(per output name)List[ChatMessage]Messages routed to the output whose pattern matches the LLM's classification response.

Init Parameters

These are the parameters you can configure in Pipeline Builder:

ParameterTypeDefaultDescription
chat_generatorChatGeneratorA chat generator instance representing the LLM used for classification.
output_namesList[str]A list of output connection names used to connect the router to other components.
output_patternsList[str]A list of regular expressions matched against the LLM's output. Each pattern corresponds to one output name. Patterns are evaluated in order.
system_promptOptional[str]NoneAn optional system prompt to customize the behavior of the LLM. For moderation models, refer to the model card for supported customization options.

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
messagesList[ChatMessage]A list of chat messages to route. Only user and assistant messages are supported.