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
ExtractedAnswerobjects 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
- Drag the
TransformersExtractiveReadercomponent onto the canvas from the Component Library. - Click on the component to open the configuration panel.
- On the General tab:
- Set the
modelto a Hugging Face question answering model. The default isdeepset/roberta-base-squad2-distilled. - If the model requires authentication, create a secret with your Hugging Face API token. Use
HF_API_TOKENorHF_TOKENas the environment variable name. For instructions, see Create Secrets.
- Set the
- Go to the Advanced tab to configure
top_k,score_threshold,max_seq_length,stride, andno_answer.
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
| Parameter | Type | Description |
|---|---|---|
query | str | The question to answer. |
documents | List[Document] | Documents to search for answer spans. |
top_k | Optional[int] | The maximum number of answers to return. Overrides the init-time value. |
score_threshold | Optional[float] | Returns only answers with a score above this threshold. Overrides the init-time value. |
max_seq_length | Optional[int] | Maximum number of tokens before splitting a sequence. Overrides the init-time value. |
stride | Optional[int] | Token overlap when a sequence is split. Overrides the init-time value. |
max_batch_size | Optional[int] | Maximum number of samples processed at once. Overrides the init-time value. |
answers_per_seq | Optional[int] | Number of answer candidates to consider per sequence. Overrides the init-time value. |
no_answer | Optional[bool] | Whether to return a no-answer prediction. Overrides the init-time value. |
overlap_threshold | Optional[float] | Removes duplicate answers when overlap exceeds this threshold. Overrides the init-time value. |
Outputs
| Parameter | Type | Description |
|---|---|---|
answers | List[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:
| Parameter | Type | Default | Description |
|---|---|---|---|
model | Union[str, Path] | deepset/roberta-base-squad2-distilled | A Hugging Face question answering model name or local path. |
device | Optional[ComponentDevice] | None | The device on which the model is loaded. If None, automatically selected. |
token | Optional[Secret] | Secret.from_env_var(["HF_API_TOKEN", "HF_TOKEN"], strict=False) | The API token used to download private models from Hugging Face. |
top_k | int | 20 | Number of answers to return per query. Required even if score_threshold is set. |
score_threshold | Optional[float] | None | Returns only answers with a probability score above this threshold. |
max_seq_length | int | 384 | Maximum number of tokens before a sequence is split. |
stride | int | 128 | Number of overlapping tokens when a sequence is split. |
max_batch_size | Optional[int] | None | Maximum number of samples fed through the model at the same time. |
answers_per_seq | Optional[int] | None | Number of answer candidates to consider per sequence. Defaults to 20 at run time. |
no_answer | bool | True | Whether to return an additional no-answer prediction with an empty text and a score representing low confidence in the other answers. |
calibration_factor | float | 0.1 | Factor used for calibrating probabilities. |
overlap_threshold | Optional[float] | 0.01 | Removes duplicate answers when overlap exceeds this threshold. Set to None to keep all answers. |
model_kwargs | Optional[Dict[str, Any]] | None | Additional 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.
| Parameter | Type | Default | Description |
|---|---|---|---|
query | str | The question to answer. | |
documents | List[Document] | Documents to search for answer spans. | |
top_k | Optional[int] | None | Maximum number of answers to return. |
score_threshold | Optional[float] | None | Returns only answers above this score threshold. |
max_seq_length | Optional[int] | None | Maximum tokens before splitting. |
stride | Optional[int] | None | Token overlap when splitting. |
max_batch_size | Optional[int] | None | Maximum batch size for inference. |
answers_per_seq | Optional[int] | None | Answer candidates per sequence. |
no_answer | Optional[bool] | None | Whether to include a no-answer prediction. |
overlap_threshold | Optional[float] | None | Overlap threshold for deduplication. |
Related Information
Was this page helpful?