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

LostInTheMiddleRanker

Reorder documents to place the most relevant ones at the beginning and end of the list.

Key Features

  • Addresses the "lost in the middle" problem where LLMs struggle to use information from the middle of long contexts.
  • Places the most relevant documents at the start and end of the list for better LLM attention.
  • Configurable top_k to control the number of documents returned.
  • Complements semantic similarity rankers in multi-stage retrieval pipelines.
  • Improves answer quality for pipelines passing many documents to an LLM.

Configuration

  1. Drag the LostInTheMiddleRanker component onto the canvas from the Component Library.
  2. Click the component to open the configuration panel.
  3. Configure the parameters as needed.

Connections

LostInTheMiddleRanker receives a query string and a documents list — typically from a retriever or another ranker. It outputs a reordered documents list. Connect its output to ChatPromptBuilder or AnswerBuilder to pass the reordered documents to an LLM.

Usage Example

components:
LostInTheMiddleRanker:
type: haystack.components.rankers.lost_in_the_middle.LostInTheMiddleRanker
init_parameters:
top_k: 10
word_count_threshold:

Parameters

Inputs

ParameterTypeDefaultDescription
documentsList[Document]A list of documents to reorder.

Outputs

ParameterTypeDefaultDescription
documentsList[Document]Documents reordered so the most relevant ones appear at the beginning and end of the list.

Init Parameters

These are the parameters you can configure in Pipeline Builder:

ParameterTypeDefaultDescription
top_kOptional[int]NoneThe maximum number of documents to return. If None, returns all documents.
word_count_thresholdOptional[int]NoneThe maximum total number of words across all documents. If the threshold is exceeded, the remaining documents are discarded.

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.