Sub topic 1
What information extraction covers
Information extraction (IE) turns unstructured text into structured records that a program can use. The input is free text such as an email, a contract or a news article. The output is data with a fixed shape: a list of entity spans, a set of relations, or a record with named fields.
This chapter sets the vocabulary for the rest of the tutorial. The later chapters compare prompting a general-purpose large language model (LLM) with training a model on labeled examples. That comparison only makes sense if you know which kind of extraction task you have. The task type decides what the output looks like, how you label data, and how you score results.
Prerequisites
This chapter assumes you have the following:
- Basic Python: functions, dictionaries, lists, and string slicing.
- Familiarity with JSON objects, arrays, and
null. - A basic idea of what a language model is: a system that predicts text given earlier text.
- Precision, recall and F1 at an introductory level. Later chapters apply them to spans, so you do not need to compute them here.
You do not need any machine learning library for this chapter.
What extraction is, and what it is not
Extraction pulls out information that the text already states. The model does not decide what is true, and it does not write new content. This separates IE from neighboring tasks:
| Task | Output | How it differs from extraction |
|---|---|---|
| Text classification | One label for the whole text | Does not locate or return pieces of the text |
| Summarization | New, shorter text | Output is free-form and is not tied to a schema |
| Question answering | An answer to one question | Extraction asks a fixed set of questions about every document |
| Information extraction | Structured records and spans | Output follows a schema and is checked against the source text |
The boundaries are blurry in practice. Asking an LLM "What is the invoice total?" is question answering in form but field extraction in purpose. What matters is whether you want a fixed schema filled in for every document. If you do, treat it as extraction.
The main task types
Most extraction projects fall into a few families. Real systems often combine them.
Named entity recognition
Named entity recognition (NER) finds spans of text that name things and assigns each span a type. A span is a contiguous range of characters or tokens in the source. For "Angela Merkel visited Paris." a NER system returns PER for "Angela Merkel" and LOC for "Paris".
NER output is anchored to the source. Each entity has a start and an end position, so you can check it by slicing the text. Classic label sets are person, organization, location and miscellaneous. Domain projects define their own: drug names, part numbers, legal clauses.
Entity linking and normalization
Finding a span is often not enough. "IBM", "International Business Machines" and "Big Blue" are three spans for one organization. Entity linking maps a span to an identifier in a knowledge base. Normalization converts a surface form into a canonical value, for example "March 4th, 2026" to 2026-03-04.
Normalization is where the output stops being a span. The value 2026-03-04 does not appear in the text, so you cannot check it by slicing. This matters for evaluation, as the later section on spans and values explains.
Relation extraction
Relation extraction finds links between entities. The output is usually a triple of subject, relation and object. From "Acme Corp. invoiced Globex GmbH", a system might return (Acme Corp., invoiced, Globex GmbH).
Relations depend on entity detection. If the entity step misses "Globex GmbH", the relation cannot be found. Relation extraction also has direction: (A, acquired, B) is a different fact from (B, acquired, A).
Event extraction
An event has a trigger (the word that signals it) and arguments with roles. In "Acme hired Dana as CFO on Monday", the trigger is "hired", and the arguments are employer, employee, role and time. Event extraction is relation extraction with more than two participants.
Key-value and field extraction
Field extraction fills a fixed form. Typical examples are invoice fields, résumé fields, and lab report values. The schema lists the keys and their types, and the model returns one value per key.
This is the family you meet most often in business projects, and the one LLM prompting handles most naturally, because JSON output maps directly to a form. The first code example of the tutorial uses it. A field value may be a copied span ("Acme Corp.") or a normalized value (120.5), and a key may have no value at all.
Other families
You will also see these:
- Coreference resolution links mentions of the same entity ("Dana", "she", "the CFO").
- Slot filling asks for the value of one relation for a given entity, such as "headquarters of Acme".
- Open information extraction pulls out triples without a fixed schema. It is flexible but hard to evaluate, so this tutorial does not use it.
One sentence, three views
The sentence "Acme Corp. invoiced Globex GmbH 120.50 EUR on 2026-03-04." supports all of the task types above. The code below stores the same information as spans, as a relation, and as fields, and checks that every span matches the source text.
text = "Acme Corp. invoiced Globex GmbH 120.50 EUR on 2026-03-04."
def span(surface, label):
start = text.index(surface)
return {"text": surface, "start": start, "end": start + len(surface), "label": label}
entities = [
span("Acme Corp.", "ORG"),
span("Globex GmbH", "ORG"),
span("120.50 EUR", "MONEY"),
span("2026-03-04", "DATE"),
]
relations = [("Acme Corp.", "invoiced", "Globex GmbH")]
fields = {
"vendor": "Acme Corp.",
"customer": "Globex GmbH",
"total": 120.5,
"currency": "EUR",
"date": "2026-03-04",
"purchase_order": None,
}
for e in entities:
assert text[e["start"]:e["end"]] == e["text"]
print(e["label"], e["start"], e["end"], e["text"])
print(relations)
print(fields)Three things stand out in the output:
- Every entity can be verified by slicing, because it is a span.
- The relation refers to entities by their text, which breaks if the same name appears twice. Real annotation tools refer to spans by position or by an identifier.
- The field
totalis120.5, a number. The text says120.50 EUR. The value was derived from the span, not copied from it. purchase_orderisNonebecause the text never states one. The schema allows a missing value, and this is a deliberate decision.
Spans versus values
This distinction drives much of the comparison later in the tutorial.
- A span output points at text. Correctness means the right characters were selected with the right label. Token-classification models such as BERT-based NER taggers produce this kind of output.
- A value output states a result. Correctness means the value equals the expected one, after any normalization. Generative LLMs produce this kind of output, because they write text instead of selecting it.
A generative model that returns "Acme Corp" without the final period has produced a value that is close to, but not equal to, the gold span "Acme Corp.". Whether that counts as an error depends on your rules for matching. You will define those rules in the chapters on schemas and evaluation.
Edge cases that shape every project
Some properties of text make extraction harder than the clean example above:
- Nested entities. "Bank of America Plaza" contains an organization inside a location name. Standard BIO tagging, covered later, cannot represent nesting.
- Discontinuous mentions. "pain in the left and right knee" mentions two body parts that share words. A single contiguous span cannot capture both.
- Repeated mentions. The same company appears five times. You must decide whether to extract every mention or one record per entity.
- Multiple values per field. An invoice may list several line items. The schema needs a list, not a single value.
- Missing values. A field not stated in the text must be
null. Models trained or prompted to always answer will invent a value. - Ambiguous types. "Washington" can be a person, a city, or a government. The labeling rules must say how to decide.
Each of these is a schema decision, not a modeling detail. A model cannot be scored fairly on a case your guidelines never addressed.
How the task type affects the approach
The task type narrows your choice of method:
| Task family | Natural output | Prompting fits when | A trained model fits when |
|---|---|---|---|
| NER | Labeled spans | Label set is small and you have little data | You have thousands of labeled sentences and need speed |
| Relation or event extraction | Triples or role records | Relation types change often | Relation types are stable and labeled data exists |
| Field extraction | JSON record | Layouts vary and schemas change | Documents are uniform and volume is high |
| Normalization | Canonical values | Rules are easy to describe in words | A lookup table or rule set can do the job |
These are starting points. The final chapters give a decision procedure that also weighs cost, latency, and maintenance.
Common mistakes at this stage
- Starting with a model before defining the output. Teams often prompt first and discover later that nobody agreed what counts as a vendor name.
- Mixing task types in one label set. A label set that includes both entity types and relation-like labels (such as
ORG_THAT_INVOICED) is hard to annotate consistently. - Treating normalization as part of detection. Finding "March 4th" and converting it to a date are separate steps with separate errors. Keep them separate so you know which one failed.
- Ignoring the null case. If every test document contains every field, you will not see the most common LLM failure, which is making up a value.
Summary
Information extraction produces structured data from text, and the main families are NER, entity linking and normalization, relation extraction, event extraction, and field extraction. Outputs are either spans, which point at the source, or values, which state a result. The distinction decides how you annotate, what a correct answer means, and which kind of model fits naturally. The next chapter turns a task into a precise schema with labeling rules.
Exercise
A team wants to read support emails and return, for each email, the product name, the order number (or null if none), and the customer's requested action as one of "refund", "replace" or "other". Which task family is this? Name one field that is a span and one that is a normalized value, and say what the output must contain when an email has no order number.
Show answer
This is field extraction, returning one JSON record per email. The product name is a span, because it is copied from the text. The requested action is a normalized value, because the label "refund", "replace" or "other" is chosen from a fixed set and usually does not appear verbatim in the email. When the email states no order number, the order_number field must be null, not a guessed or empty string. The schema should say so explicitly.
Exercise
A model reads "Acme Corp. invoiced Globex GmbH 120.50 EUR" and returns the total as 120.5. The gold annotation is the span "120.50 EUR" at characters 31 to 41. Can a span-based exact-match check score this output? What would you need to decide first?
Show answer
No, not directly. The output is a normalized value, and the gold annotation is a span, so there are no offsets to compare. You must first decide on a matching rule. One option is to normalize the gold span to 120.5 and EUR and compare values. Another is to require the model to return the exact source text so offsets can be found. Both are valid, but the rule must be fixed before comparing systems.