Structured data extraction: turn unstructured text into verifiable fields
Structured data extraction finds needed values in unstructured text such as emails, forms, and documents, then places them in defined fields. A shipping inquiry, for example, might become a JSON record with an order identifier, request type, date, and missing information. Asking a model to answer in JSON shape alone does not guarantee valid data. Design schema validation, source-evidence checks, and business-rule checks as separate steps.
Structured data extraction finds needed values in unstructured text such as emails, forms, and documents, then places them in defined fields. A shipping inquiry, for example, might become a JSON record with an order identifier, request type, date, and missing information. Asking a model to answer in JSON shape alone does not guarantee valid data. Design schema validation, source-evidence checks, and business-rule checks as separate steps.
Define the values before extracting them
Define each field's meaning, data type, allowed values, representation when absent, and how to handle multiple candidates. If purchase_date could mean purchase date or delivery date, systems and reviewers may choose differently. Agree per field whether to use null, an empty string, or unknown. If “not provided” differs from “checked and not applicable,” represent them as separate enum values.
{
"issue_type": "delivery | refund | account | other | unknown",
"order_id": "string or null",
"purchase_date": "YYYY-MM-DD or null",
"missing_fields": ["string"],
"evidence": [
{ "field": "string", "quote": "string", "start": 0, "end": 0 }
]
}This schema is illustrative. Decide date rules, the character unit for offsets, and the meaning of empty values for your application. If you store source text, normalization by language or Unicode handling can shift offsets. Fix the character-index convention and verify that each offset maps back to the original.
How should you design the extraction prompt and schema?
**Keep the business fields narrow.** Start with the minimum fields downstream work needs. Do not add speculative personality or sentiment fields without demonstrated value.
**Define each field.** Specify inclusion and exclusion conditions and examples of easily confused values. If several dates appear, state their role, such as “the purchase date referred to by the request.”
**Link values to evidence.** For important values, return a short quote or a location reference such as document ID, page, or paragraph. If the source gives no support, report it as missing rather than guessing.
**Represent uncertainty and conflicts.** If the source contains conflicting values, do not choose arbitrarily. Return candidates, a conflict marker, and source locations, or route the case for human review.
**Separate input from instructions.** Tell the model to treat sentences inside the document as data, not instructions to execute. This prompt boundary is not itself a security control; separately limit tool permissions and application policy.
More fields can increase omissions, confusion, and review work. Too few can force downstream systems into riskier guesses. Review each field's necessity, decision rule, and cost of failure, then refine the schema with a small evaluation set.
Format validation is not fact validation
Schema validation checks that order_id is a string, issue_type is an allowed enum, and required keys exist. It does not prove the value matches the source, that the correct date was chosen, or that the ID identifies a real order. Separate at least three checks:
| Check layer | Question | Example |
|---|---|---|
| Structure | Does the output have the required shape and types? | Required keys, arrays, enum values |
| Evidence | Does the source support the extracted value? | Compare the quote with the matching source passage |
| Business rules | Does the value meet domain constraints? | Date range, order-ID lookup, cross-field conditions |
OpenAI's Structured Outputs documentation explains that supported JSON Schema can constrain completed non-refusal outputs, but applications must handle refusals, incomplete responses, and API errors and validate business rules the schema cannot express. JSON mode does not provide the same guarantee as following a specified schema. Supported schema features and model availability may change; check the official documentation during implementation. This feature constrains format; it does not prove extracted facts are true. OpenAI Structured Outputs documentation
How should missing values and conflicts be handled safely?
If an inquiry lacks an order number, do not invent one; return an explicit missing state. If document A says the purchase date was April 3 and document B says April 5, do not assume one is newer unless a rule establishes source priority. Apply a defined precedence rule when one exists; otherwise preserve both and mark the conflict.
Distinguish retries from repeated execution. For a format error such as JSON parsing failure, one retry may include a limited error message and the original input. When evidence is absent or documents conflict, repeating the same prompt is unlikely to resolve it; request information, quarantine the record, or send it for human review. Store refusals and timeouts as separate processing states, not empty records that could be mistaken for normal missing values.
Example: extracting a customer-support ticket
Hypothetical example: From “I want to cancel order 5831, delivered last week. I’m not sure whether it was opened,” extract intent, order_id, delivery_time_expression, opened_status, and needs_clarification.
Extract `5831` as `order_id` and link it to its source span.
Record the intent as “cancellation request”; do not conclude that cancellation is allowed.
Preserve “last week” as written, or normalize it only when a reference date and time zone are available. Otherwise do not invent a calendar date.
Set the opened status to `unknown` because the user says they do not know.
If opened status matters to refund or cancellation policy but is absent, mark it missing or request clarification.
This hypothetical example separates extraction from business decisions. It does not claim a real policy judgment or test result.
How should quality and operations be evaluated?
Build a set of representative and boundary cases. Measure accuracy per field, missing-value rate, unsupported-value rate, evidence-link accuracy, parse failures, and human-review rate. An overall accuracy score can hide errors in rare but important dates or amounts behind frequent easy fields, so record per-field results and high-cost errors separately. Include conflicting documents, multiple dates and IDs, typos, different languages, quoted instructions, and missing information.
When schemas or prompts change, compare versions on the same evaluation cases. Log extracted values and evidence locations only as needed for review, without copying personal data or source text more than necessary. Tracking retry cost and latency by error type helps determine whether to use prompt changes, schema changes, deterministic rules, or human review.
Reference
[OpenAI API: Structured model outputs](https://developers.openai.com/api/docs/guides/structured-outputs) — JSON Schema constraints, refusal and incomplete-response handling, and limits that require separate business-rule validation.