Question Answering
Purpose and Scope
Question answering in Transformers covers models that return an answer for a user question, but the repository documentation separates that broad idea into several practical task families. The standard text guide focuses on extractive question answering, where a model selects a span from a provided context. The document guide applies the same span-selection idea to scanned or rendered documents by combining text, word positions, and images. The visual guide expands the task to open-ended questions about images, where answers may come from a classifier or a generative vision-language model.
Sources: docs/source/en/tasks/question_answering.md, docs/source/en/tasks/document_question_answering.md, docs/source/en/tasks/visual_question_answering.md
Use this page when deciding which Transformers question answering workflow fits your data. If your examples contain a plain text context, question, and answer offsets, start with the SQuAD-style extractive flow. If your examples are document images with OCR words or bounding boxes, use the document question answering flow and account for OCR and layout dependencies. If your examples are natural images paired with open-ended questions, use the visual question answering flow, where the training objective may be classification or generation depending on the selected architecture.
Sources: docs/source/en/tasks/question_answering.md, docs/source/en/tasks/document_question_answering.md, docs/source/en/tasks/visual_question_answering.md
Relevant Source Files
- docs/source/en/tasks/question_answering.md — English task guide for extractive text question answering with DistilBERT on SQuAD, including dataset loading, preprocessing, fine-tuning, and inference steps.
- docs/source/ar/tasks/question_answering.md — Arabic localization of the text question answering guide, preserving the same task concepts, setup commands, SQuAD fields, and preprocessing sequence.
- docs/source/es/tasks/question_answering.md — Spanish localization of the text question answering guide, including the same DistilBERT tokenizer setup and task-specific preprocessing cautions.
- docs/source/en/tasks/document_question_answering.md — English guide for document visual question answering with LayoutLMv2 on DocVQA-style data, including OCR, bounding boxes, dependency setup, and inference framing.
- docs/source/ja/tasks/document_question_answering.md — Japanese localization of the document question answering guide, including the DocVQA dataset fields and the LayoutLMv2 maximum-position constraint discussion.
- docs/source/en/tasks/visual_question_answering.md — English guide for image-based visual question answering with ViLT fine-tuning and zero-shot generative VQA examples.
Core Task Variants
The text question answering guide defines two common forms: extractive and abstractive. Extractive systems identify the exact answer text inside a context passage, so the training data must connect a question to both the context and the answer span. Abstractive systems generate an answer from the context rather than copying a span. The provided Transformers text guide is intentionally centered on the extractive path, using DistilBERT and SQuAD so the reader learns the span-labeling workflow before moving to generation-oriented tasks.
Sources: docs/source/en/tasks/question_answering.md, docs/source/ar/tasks/question_answering.md, docs/source/es/tasks/question_answering.md
Document question answering is a bridge between text QA and multimodal modeling. The guide describes Document Question Answering, also called Document Visual Question Answering, as answering questions about document images. LayoutLMv2 treats the task as extractive question answering by adding a question-answering head on top of token hidden states and predicting start and end token positions. The important difference is that the context comes from OCR output, so the workflow must preserve document text, word locations, and image information instead of treating the input as a single plain paragraph.
Sources: docs/source/en/tasks/document_question_answering.md, docs/source/ja/tasks/document_question_answering.md
Visual question answering uses images directly as the context for a natural-language question. The visual guide describes ViLT as a classification-style VQA model: text embeddings are incorporated into a Vision Transformer, and a classifier head over the final hidden state of the classification token predicts an answer class. The same guide also calls out newer generative approaches such as BLIP, BLIP-2, and InstructBLIP, which answer in natural language without the same fixed classification framing. Choose this path when the question depends on image content rather than a text passage or OCR transcript.
Sources: docs/source/en/tasks/visual_question_answering.md
Text Extractive Workflow
A typical text QA run begins with dependencies, account setup, and a small SQuAD subset. The guide installs Transformers, Datasets, and Evaluate, then encourages Hub login so the resulting model can be uploaded and shared. It loads a small slice of SQuAD with Datasets and splits that training data into train and test subsets for quick iteration. Each example has answer text with start positions, a context passage, and a question. Those fields define the contract that the tokenizer, model head, trainer, and inference step all rely on.
Sources: docs/source/en/tasks/question_answering.md
pip install transformers datasets evaluatefrom datasets import load_dataset
squad = load_dataset("squad", split="train[:5000]")
squad = squad.train_test_split(test_size=0.2)Preprocessing is the step that usually determines whether extractive QA training works. The guide loads the DistilBERT tokenizer with an auto class and processes both question and context. For long contexts, truncation should apply only to the context side, not the question, because the question is the instruction the model must answer. Offset mappings are returned so character-level answer spans can be mapped back to token positions. The tokenizer sequence identifiers then distinguish question tokens from context tokens, which prevents accidentally labeling a question token as the answer span.
Sources: docs/source/en/tasks/question_answering.md, docs/source/es/tasks/question_answering.md
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased")Document and Visual QA Workflows
The document workflow adds operational dependencies that do not appear in plain text QA. LayoutLMv2 depends on Detectron2, TorchVision, Tesseract OCR, and PyTesseract in addition to Transformers and Datasets. The guide uses a small preprocessed DocVQA sample from the Hub, with train and test splits already available. Its dataset features include document images, multilingual query fields, human-provided answers, OCR words, and bounding boxes. The guide also normalizes the examples by keeping the English question and selecting an answer from the annotated answer list.
Sources: docs/source/en/tasks/document_question_answering.md, docs/source/ja/tasks/document_question_answering.md
pip install -q transformers datasets
pip install 'git+https://github.com/facebookresearch/detectron2.git'
pip install torchvision
sudo apt install tesseract-ocr
pip install -q pytesseractDocument QA has an important length edge case. The Japanese localized guide notes that the LayoutLMv2 checkpoint used in the tutorial was trained with a maximum position embedding size of 512. Long documents can exceed that limit, and truncation can remove an answer near the end of the page. The tutorial handles this by filtering examples that may exceed the limit, while also pointing readers toward a sliding-window strategy when their documents are usually long. This is the same fundamental issue as long text QA, but OCR and layout make the failure mode easier to miss.
Sources: docs/source/ja/tasks/document_question_answering.md, docs/source/en/tasks/document_question_answering.md
For visual QA, the guide starts from a small validation slice of the Graphcore VQA dataset and uses ViLT as the fine-tuning model checkpoint. The training objective is classification, so answer labels must be represented in a way the model head can predict. The same page then distinguishes that fine-tuning path from zero-shot inference with generative models. This distinction matters when building applications: classification VQA is useful when the possible answers are shaped by the dataset, while generative VQA is more flexible for open-ended image conversations and broader natural-language responses.
Sources: docs/source/en/tasks/visual_question_answering.md
System-to-Code Mapping
The requested source files are documentation entry points rather than model implementation modules, so the mapping is best read as a task-surface map. The text QA files define the canonical extractive recipe and its localized equivalents. The document QA files define the multimodal document recipe and expose the additional dependencies, dataset fields, and OCR/layout constraints. The visual QA file defines image-based VQA, including both classification and generative framing. Together they show that Transformers presents question answering as a family of workflows unified by questions and answers, but differentiated by the form of context and model head.
Sources: docs/source/en/tasks/question_answering.md, docs/source/ar/tasks/question_answering.md, docs/source/es/tasks/question_answering.md, docs/source/en/tasks/document_question_answering.md, docs/source/ja/tasks/document_question_answering.md, docs/source/en/tasks/visual_question_answering.md
| Workflow | Context type | Example model | Example dataset | Main output framing |
|---|---|---|---|---|
| Text extractive QA | Text passage | DistilBERT | SQuAD | Start and end answer span |
| Document QA | Document image plus OCR/layout | LayoutLMv2 | DocVQA sample | Start and end answer span over document tokens |
| Visual QA | Natural image | ViLT, BLIP-2 style models | Graphcore VQA | Classification answer or generated answer |
Next Steps
Start with the text question answering guide if you need the simplest end-to-end training path, because it exposes the core span-labeling mechanics without OCR or vision preprocessing. Move to document QA when the question refers to invoices, forms, pages, or other document images, and plan for OCR quality, bounding boxes, and maximum sequence length. Move to visual QA when the question is about image content rather than document text. For adjacent concepts, read the preprocessing, processors, fine-tuning, Trainer, and multimodal task pages to understand how tokenizers, processors, data collators, and model heads fit into the larger Transformers workflow.