Search⌘ K

Question Answering

Learn to answer questions automatically using Hugging Face.

Question answering (QA) enables AI systems to understand questions in natural language and provide accurate, relevant answers, just like a human expert would. Whether you’re building chatbots, document search engines, or AI assistants, QA is fundamental to creating intelligent, interactive applications.

QA systems take two primary inputs: a question (what you want to know) and context (the information source). The system analyzes the context to find or generate an answer. Unlike search engines that return entire documents, QA systems provide specific, precise answers to your questions.

Note: The first question answering (QA) system, BASEBALL, was created at MIT in 1961. It could answer questions about baseball games stored in a database using natural language queries, such as “Who did the Red Sox lose to on July 5?” While limited to a tiny domain, it demonstrated that machines could understand and respond to questions!

Extractive vs. generative QA

Question Answering comes in two fundamentally different flavors, each with distinct strengths and ideal use cases. Understanding when to use each approach is crucial for building effective QA systems.

  • Extractive question answering works like a highlighter pen. It identifies the exact span of text in the context that answers your question. The system doesn’t create new text; instead, it carefully selects and extracts existing words from your source material. The model calculates start and end probabilities for each token (word) in the context and identifies the span with the highest combined probability.

  • Generative question answering works more like a human writer. It understands both the question and context, then creates new text to answer. The system may paraphrase, synthesize information from multiple sources, or elaborate beyond what’s explicitly stated. A decoder generates the answer token by token, with each word predicted based on the question, context, and previously generated words.

To make the comparison clearer, let’s look at how these approaches differ across key dimensions:

Aspect

Extractive QA

Generative QA

Method

Extracts exact text spans from context

Generates new text as answer

Speed

Fast (single forward pass)

Slower (multiple generation steps)

Accuracy

Factually grounded, no hallucination

Risk of hallucination

Flexibility

Answer must exist verbatim

Can synthesize and paraphrase

Best for

Legal, medical, fact verification

Conversational AI, explanations

Output

Exact quotes from source

Natural, conversational responses

Note: For production systems handling sensitive information such as legal documents, medical records, or financial data, extractive QA is generally safer due to its grounding in source material and elimination of hallucination risk.

Closed-book vs. open-book QA

Beyond the extractive/generative divide, QA systems are also categorized by whether they have access to external information.

  • Open-book QA is like taking a test where you can reference your textbook. The system receives both a question and context, then reads the provided material to find answers. This is what most Hugging Face QA pipelines implement. It’s used for reading ...