Extracting Entities, Topics and Summaries with Structured Outputs
This lesson explains how to design extraction prompts and schemas so that language models extract entities, topics and summaries reliably enough for downstream code.
Learning outcomes
After completing this lesson, you will be able to:
- Explain what structured outputs guarantee and what they leave to validation.
- Design an extraction schema with names, types, descriptions and constraints.
- Identify where generative extraction suits custom entities and topics.
- Choose between generative and prebuilt extraction.
From free text to reliable data
Text analysis turns unstructured text, such as emails, reviews, reports and transcripts, into information that software can act on: who is mentioned, what the text is about, how the writer feels and what the key points are. Two families of tools do this in Foundry. Prebuilt Foundry Tools such as Azure Language return fixed categories with offsets and confidence scores. Language models can extract almost anything that can be described in words, which makes them the natural choice for an organization's own fields and categories.
The difficulty with language models is that their natural output is prose, which code cannot parse reliably. Structured outputs, introduced in the lesson Calling Language, Code and Multimodal Models with the Responses API, constrain the response to a JSON Schema so that every required field is present with the declared type. This lesson applies them to text analysis. The quality of the extraction then depends on two things that the schema itself cannot guarantee: how clearly each field is defined, and whether the values the model places in the fields are correct, which is checked with validation and evaluation.
Designing the schema
The schema is the contract between the model and the code that uses its output. Each field has a clear name, a type, a description and, where possible, constraints: enums for categories, formats for dates, patterns for identifiers, and arrays for repeated items. Optional information is represented with nullable fields so that the model can return null rather than invent a value.
In Python, the schema is most conveniently written as a Pydantic model. In the following example, responses.parse() converts the Complaint class into a strict JSON Schema, sends it with the request and returns the response already parsed into a Complaint object. The Literal types become enums, so topics can only contain the five listed values, and the field descriptions act as instructions for each field.
from pydantic import BaseModel, Field
from typing import Literal, Optional
class Complaint(BaseModel):
product: str
order_number: Optional[str] = Field(description="Format ORD-123456, or null if absent")
topics: list[Literal["delivery", "quality", "billing", "support", "other"]]
sentiment: Literal["positive", "neutral", "negative"]
summary: str = Field(description="One sentence, at most 25 words")
parsed = openai.responses.parse(
model="extractor", input=complaint_text, text_format=Complaint,
instructions="Extract the fields from the customer complaint. Use null when information is missing.",
)Entities, topics and summaries
Generative extraction suits custom entities that prebuilt models do not recognize, such as contract parties, clause types, product configurations or medication dosages; topics from the organization's own taxonomy, expressed as an enum; and summaries in a prescribed format, such as a three-bullet executive summary with a risk rating. Long documents are processed in sections (map-reduce) or with a model whose context window holds the whole text.
Generative or prebuilt?
Azure Language provides prebuilt extraction with entity categories, character offsets and confidence scores. The choice between it and generative prompting depends on what the output must contain and how it will be used. Generative extraction wins when the fields or categories are the organization's own, because they are defined in a schema rather than trained. Azure Language wins when the application needs the exact position of each entity in the text, for example to highlight or redact it, when a calibrated confidence score drives automation, or when identical input must always give identical output for audit purposes.
Cost behaves differently as well. Azure Language is priced per text record, so cost is predictable and independent of prompt length, while a language model is priced per token, so long instructions and long documents raise the cost of every call. The following table compares the two approaches.
Generative or Azure Language?
Extraction for routing
A support team wants each incoming email tagged with product, order number, topics from a fixed list of eight and a one-sentence summary, written to a ticketing database. Some emails have no order number.
Use structured outputs with an enum for topics, a nullable order number with a pattern description, and a length-limited summary, at low temperature. Azure Language named entity recognition would not produce the custom topics or product field without training.
How does a schema stop a model from inventing missing values?
Show answer
Nullable fields let the model return null when a value is not present, and enums restrict categories to allowed values.
Exam Tips
Remember for the exam
- Use enums for categories and nullable fields so the model does not invent missing values.
- Custom entities and taxonomies: generative extraction with structured outputs.
- Offsets and confidence scores: Azure Language.