Sub topic 2
SQuAD-Style Data: Questions, Contexts, and Answer Spans
Prerequisites and the task format
This chapter assumes you know basic Python, have seen tokenization, and understand that a pretrained language model maps text to numerical representations.
Extractive question answering answers a question by copying a continuous span from a provided passage. The passage is called the context. The answer is not generated word by word. Instead, the model predicts where the answer starts and where it ends.
SQuAD, the Stanford Question Answering Dataset, stores each example with three main parts:
- A question, such as “What does the Amazon basin cover?”
- A context containing the information needed to answer it.
- One or more answer annotations, including the answer text and its character position in the context.
A SQuAD-style record commonly looks like this:
{
"id": "q1",
"question": "What does the Amazon basin cover?",
"context": "The Amazon River in South America is the largest river by discharge volume of water in the world. Its drainage basin covers about seven million square kilometres.",
"answers": {
"text": ["about seven million square kilometres"],
"answer_start": [117]
}
}The answer_start value is a character index. It points to the first character of the answer inside context. The answer ends after the number of characters in the answer text.
Why character positions matter
Models do not read raw strings directly. A tokenizer splits text into tokens, often subword pieces. Training therefore requires converting character positions into token positions.
For example, the answer might begin in the middle of a tokenized context:
| Data level | Example |
|---|---|
| Character span | Characters 117 through 151 |
| Token span | Tokens 18 through 22 |
| Model labels | `start_positions = 18`, `end_positions = 22` |
The character annotation remains useful because it refers to the original text exactly. Offset mappings later connect each token to its character range.
SQuAD examples can contain multiple valid answers. For example, both “seven million square kilometres” and “about seven million square kilometres” might be acceptable. The text and answer_start arrays must stay aligned: item 0 in each array describes the same answer.
Context, question, and answer relationships
The context should contain enough evidence to answer the question. The question tells the model what information to locate, but the context determines the allowed answer text.
This structure differs from two related approaches:
- Generative question answering produces new text and may use wording that does not appear in the passage.
- Retrieval-based question answering first searches a collection for relevant documents. Extractive answering usually assumes that a context has already been provided.
A SQuAD-style dataset can also include unanswerable questions. In SQuAD 2.0, a question may have no valid span in the context. The model must then choose a null answer instead of selecting a plausible but unsupported phrase.
Inspecting an example in Python
The following code verifies that an annotated answer matches the context at the declared character position.
example = {
"id": "q1",
"question": "What does the Amazon basin cover?",
"context": (
"The Amazon River in South America is the largest river by discharge "
"volume of water in the world. Its drainage basin covers about seven "
"million square kilometres."
),
"answers": {
"text": ["about seven million square kilometres"],
"answer_start": [122],
},
}
answer = example["answers"]["text"][0]
start = example["answers"]["answer_start"][0]
end = start + len(answer)
assert example["context"][start:end] == answer
print(example["context"][start:end])This check catches an important data error: an incorrect offset. If the extracted substring does not equal the annotation, later token-label construction will be wrong.
Exercise
Why does a SQuAD-style example store both the answer text and its character start position?
Show answer
The answer text identifies the expected span, while the character position locates that span in the original context so it can be aligned with token positions during training.