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

LostInTheMiddleRanker

Reorder documents so the most relevant ones appear at the beginning and end of the list, where LLMs tend to pay more attention.

Key Features

  • Addresses the "lost in the middle" phenomenon where LLMs pay less attention to documents in the middle of a long context.
  • Reorders documents so the most relevant ones appear at the beginning and end of the list.
  • Works with any list of documents; no model inference required.
  • Configurable number of documents to return via top_k.

Configuration

  1. Drag the LostInTheMiddleRanker component onto the canvas from the Component Library.
  2. Click on the component to open the configuration panel.
  3. On the General tab:
    • Set top_k to control how many documents to return.

Connections

LostInTheMiddleRanker accepts a list of documents as input. Connect it after a retriever or DocumentJoiner in a query pipeline.

It outputs a reordered list of documents. Connect its documents output to ChatPromptBuilder or AnswerBuilder.

Source Code

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

Usage Examples

Basic Configuration

  LostInTheMiddleRanker:
type: haystack.components.rankers.lost_in_the_middle.LostInTheMiddleRanker
init_parameters: {}

Parameters

Inputs

ParameterTypeDefaultDescription
documentsList[Document]A list of documents to reorder.
top_kOptional[int]NoneThe maximum number of documents to return.

Outputs

ParameterTypeDefaultDescription
documentsList[Document]Reordered list of documents with the most relevant ones at the beginning and end.

Init Parameters

These are the parameters you can configure in Pipeline Builder:

ParameterTypeDefaultDescription
top_kOptional[int]NoneThe maximum number of documents to return.

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
documentsList[Document]A list of documents to reorder.
top_kOptional[int]NoneThe maximum number of documents to return.