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

TransformersExtractiveReader

Locate and extract answers to a query from documents using a Hugging Face question answering model. Use this component for extractive question answering pipelines where answers must come directly from source text.

Key Features​

  • Scores every possible answer span independently across documents for easier comparison.
  • Supports long documents through configurable sequence length and stride-based splitting.
  • Returns ranked ExtractedAnswer objects with scores and document offsets.
  • Optionally returns a no-answer prediction when no span is confident enough.
  • Removes duplicate overlapping answers using a configurable overlap threshold.

Configuration​

  1. Drag the TransformersExtractiveReader component onto the canvas from the Component Library.
  2. Click on the component to open the configuration panel.
  3. On the General tab:
    1. Set the model to a Hugging Face question answering model. The default is deepset/roberta-base-squad2-distilled.
    2. If the model requires authentication, create a secret with your Hugging Face API token. Use HF_API_TOKEN or HF_TOKEN as the environment variable name. For instructions, see Create Secrets.
  4. Go to the Advanced tab to configure top_k, score_threshold, max_seq_length, stride, and no_answer.
note

Extractive readers run locally and may need GPU resources for larger models.

Connections​

TransformersExtractiveReader receives a query string and a list of documents. Connect it after a retriever in a query pipeline. It outputs a list of ExtractedAnswer objects you can connect to AnswerBuilder or other downstream components.

Source Code​

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

Usage Examples​

Basic Configuration​

TransformersExtractiveReader:
type: haystack_integrations.components.readers.transformers.extractive_reader.TransformersExtractiveReader
init_parameters:
model: deepset/roberta-base-squad2-distilled
top_k: 5
no_answer: true

Using the Component in a Pipeline​

# haystack-pipeline
components:
retriever:
type: haystack.components.retrievers.in_memory.embedding_retriever.InMemoryEmbeddingRetriever
init_parameters:
document_store:
type: haystack.document_stores.in_memory.document_store.InMemoryDocumentStore
init_parameters: {}
top_k: 10

reader:
type: haystack_integrations.components.readers.transformers.extractive_reader.TransformersExtractiveReader
init_parameters:
model: deepset/roberta-base-squad2-distilled
top_k: 3

connections:
- sender: retriever.documents
receiver: reader.documents

max_runs_per_component: 100

metadata: {}

inputs:
query:
- reader.query

outputs:
answers: reader.answers

Parameters​

Inputs​

ParameterTypeDescription
querystrThe question to answer.
documentsList[Document]Documents to search for answer spans.
top_kOptional[int]The maximum number of answers to return. Overrides the init-time value.
score_thresholdOptional[float]Returns only answers with a score above this threshold. Overrides the init-time value.
max_seq_lengthOptional[int]Maximum number of tokens before splitting a sequence. Overrides the init-time value.
strideOptional[int]Token overlap when a sequence is split. Overrides the init-time value.
max_batch_sizeOptional[int]Maximum number of samples processed at once. Overrides the init-time value.
answers_per_seqOptional[int]Number of answer candidates to consider per sequence. Overrides the init-time value.
no_answerOptional[bool]Whether to return a no-answer prediction. Overrides the init-time value.
overlap_thresholdOptional[float]Removes duplicate answers when overlap exceeds this threshold. Overrides the init-time value.

Outputs​

ParameterTypeDescription
answersList[ExtractedAnswer]Answers sorted by descending score. May include a no-answer entry when no_answer is enabled.

Init Parameters​

These are the parameters you can configure in Pipeline Builder:

ParameterTypeDefaultDescription
modelUnion[str, Path]deepset/roberta-base-squad2-distilledA Hugging Face question answering model name or local path.
deviceOptional[ComponentDevice]NoneThe device on which the model is loaded. If None, automatically selected.
tokenOptional[Secret]Secret.from_env_var(["HF_API_TOKEN", "HF_TOKEN"], strict=False)The API token used to download private models from Hugging Face.
top_kint20Number of answers to return per query. Required even if score_threshold is set.
score_thresholdOptional[float]NoneReturns only answers with a probability score above this threshold.
max_seq_lengthint384Maximum number of tokens before a sequence is split.
strideint128Number of overlapping tokens when a sequence is split.
max_batch_sizeOptional[int]NoneMaximum number of samples fed through the model at the same time.
answers_per_seqOptional[int]NoneNumber of answer candidates to consider per sequence. Defaults to 20 at run time.
no_answerboolTrueWhether to return an additional no-answer prediction with an empty text and a score representing low confidence in the other answers.
calibration_factorfloat0.1Factor used for calibrating probabilities.
overlap_thresholdOptional[float]0.01Removes duplicate answers when overlap exceeds this threshold. Set to None to keep all answers.
model_kwargsOptional[Dict[str, Any]]NoneAdditional keyword arguments passed to AutoModelForQuestionAnswering.from_pretrained.

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
querystrThe question to answer.
documentsList[Document]Documents to search for answer spans.
top_kOptional[int]NoneMaximum number of answers to return.
score_thresholdOptional[float]NoneReturns only answers above this score threshold.
max_seq_lengthOptional[int]NoneMaximum tokens before splitting.
strideOptional[int]NoneToken overlap when splitting.
max_batch_sizeOptional[int]NoneMaximum batch size for inference.
answers_per_seqOptional[int]NoneAnswer candidates per sequence.
no_answerOptional[bool]NoneWhether to include a no-answer prediction.
overlap_thresholdOptional[float]NoneOverlap threshold for deduplication.