Extraction & Classification Prompts

Ma Mahalakshmi V Updated 19 Sep 2026
9 min read ·Lesson 148 of 224

Prompt Patterns for Extraction and Classification

Extraction (pulling specific structured fields out of unstructured text) and classification (assigning input to one of a fixed set of categories) are two of the most common tasks built with the OpenAI SDK in production applications — invoice parsing, ticket routing, content moderation, entity recognition. Both tasks share a property that shapes how their prompts should be written: the desired output has a small, well-defined shape, which means the prompt's main job is constraining the model to that shape reliably, not eliciting creative or open-ended reasoning. This lesson covers concrete, reusable patterns for both.

Why Extraction and Classification Need a Different Prompt Shape Than Open-Ended Tasks

An open-ended task like "write a product description" has many acceptable outputs; a classification task like "is this email spam or not spam" has exactly one correct answer per input, chosen from a fixed set. This changes what the prompt needs to do:

  • The output format must be constrained tightly enough that downstream code can parse it without ambiguity.
  • The set of valid categories or fields must be stated explicitly, not left for the model to infer from context.
  • Edge cases (missing data, ambiguous category, no match) need an explicit instruction for what to output, because leaving this unspecified is one of the most common sources of production failures — the model inventing a plausible-sounding but wrong field value instead of indicating absence.

Pattern: Classification With a Constrained Category List

The foundational classification pattern states the exact category list and instructs the model to output only one of them, formatted for direct parsing:

from openai import OpenAI

client = OpenAI()

CLASSIFICATION_INSTRUCTIONS = """Classify the support ticket into exactly one of these categories:
billing, technical, account, other.

Respond with only the category name, in lowercase, with no punctuation
and no explanation."""

def classify_ticket(ticket_text: str) -> str:
    response = client.responses.create(
        model="gpt-5.6-terra",
        instructions=CLASSIFICATION_INSTRUCTIONS,
        input=ticket_text,
    )
    return response.output_text.strip().lower()
category = classify_ticket("I was charged twice for my subscription this month.")
print(category)  # billing

Three details in CLASSIFICATION_INSTRUCTIONS are load-bearing, not stylistic. First, the category list is enumerated explicitly (billing, technical, account, other) rather than described abstractly ("classify by topic") — an explicit, closed list is what makes "exactly one of these" enforceable; without it, the model has no fixed vocabulary to choose from and will produce inconsistent labels like "billing issue" versus "billing" across different calls. Second, "other" is included as an explicit escape category — without it, a ticket that doesn't cleanly fit billing, technical, or account forces the model to force-fit it into the nearest one, silently corrupting your category statistics. Third, the format instruction ("only the category name, in lowercase, with no punctuation") exists specifically so .strip().lower() in application code can safely compare the result against the known category strings without needing to handle variations like "Billing." or "Category: billing".

Pattern: Classification With Confidence or Abstention

Plain classification always returns one of the listed categories, even when the input is genuinely ambiguous. For applications where a wrong-but-confident answer is worse than an explicit "unsure" (routing a ticket to the wrong team causes more damage than flagging it for human review), add an abstention option and require the model to use it rather than guess:

CLASSIFICATION_WITH_ABSTENTION = """Classify the support ticket into exactly one of these categories:
billing, technical, account, other, uncertain.

Use "uncertain" only if the ticket genuinely does not provide enough
information to choose confidently among the other categories.

Respond with only the category name, in lowercase, with no punctuation."""

This is a small change in the instructions text but a meaningful change in what the application must handle: code that calls this version needs an explicit branch for "uncertain" that routes to human review rather than assuming every response is a final, actionable category. Adding this option without also adding the "use it only if genuinely unclear" qualifier tends to make the model overuse it — models given an easy escape hatch will sometimes prefer it even for cases they could actually classify correctly, so the qualifier is there to counteract that bias.

Pattern: Structured Extraction With an Explicit Field List

Extraction has the same "constrain the shape" goal as classification, but for several fields at once rather than a single label. State every field explicitly, including what to output when a field is absent from the input:

EXTRACTION_INSTRUCTIONS = """Extract the following fields from the invoice text below.
Respond with only a JSON object with exactly these keys:

- invoice_number (string)
- total_amount (number, no currency symbol)
- due_date (string, format YYYY-MM-DD)

If a field is not present in the text, use null for that field.
Do not include any keys other than the three listed above."""

def extract_invoice_fields(invoice_text: str) -> dict:
    import json
    response = client.responses.create(
        model="gpt-5.6-terra",
        instructions=EXTRACTION_INSTRUCTIONS,
        input=invoice_text,
    )
    return json.loads(response.output_text)
invoice = "Invoice #A-4471. Amount due: $230.00. Payment due by March 3, 2027."
fields = extract_invoice_fields(invoice)
print(fields)
# {'invoice_number': 'A-4471', 'total_amount': 230.0, 'due_date': '2027-03-03'}

The explicit null instruction for missing fields is the single most important line here for production reliability. Without it, a prompt asked to extract a due_date from text that has none will sometimes hallucinate a plausible-looking date rather than indicate absence, because the model has been instructed to produce a JSON object with that key and, absent other guidance, will try to fill it with something. Stating the fallback value explicitly turns an implicit, unreliable behavior into an explicit, checkable one: application code can reliably check fields["due_date"] is None to detect a genuinely missing value.

Note: For extraction tasks where field structure matters for downstream code, prefer the SDK's structured output support (a JSON schema passed via response_format or the equivalent typed-output parameter for your SDK version) over parsing free-form JSON text with json.loads. This lesson uses plain JSON-in-text for clarity of the underlying pattern; check your SDK version's documentation for the current structured-output mechanism, since this is an area that has changed across SDK releases.

Pattern: Extraction With Source Grounding

For extraction from longer or more ambiguous documents, requiring the model to quote the exact source span it extracted a value from makes incorrect extractions much easier to catch — a wrong value with no source text looks the same as a correct one until manually checked, but a wrong value paired with a source quote that clearly doesn't support it is easy to flag automatically or by a human reviewer:

GROUNDED_EXTRACTION_INSTRUCTIONS = """Extract the total amount due from the invoice text.
Respond with a JSON object with two keys:

- total_amount (number, or null if not found)
- source_quote (the exact sentence or phrase the amount was taken from,
  or null if total_amount is null)

Do not paraphrase source_quote; it must be an exact substring of the input."""

Application code can then verify the grounding cheaply, without another model call:

def verify_grounding(extracted: dict, original_text: str) -> bool:
    quote = extracted.get("source_quote")
    if quote is None:
        return extracted.get("total_amount") is None
    return quote in original_text

verify_grounding checks a simple, mechanical property — is the claimed quote actually present verbatim in the source document — which catches a meaningful class of extraction errors (the model paraphrasing, or extracting from a hallucinated value) without needing any additional model call or human review for the common case where grounding checks out.

Comparing the Extraction and Classification Patterns

AspectClassificationExtraction
Output shapeSingle label from a fixed setMultiple named fields, often JSON
Core riskForce-fitting an ambiguous input into a wrong categoryHallucinating a value for an absent field
Key mitigationExplicit "other"/"uncertain" categoryExplicit null/absent instruction, optional source grounding
ParsingDirect string comparison after normalizationJSON parsing or structured-output schema validation
Typical downstream useRouting, filtering, taggingPopulating structured records, forms, databases

Both patterns rely on the same underlying principle from earlier lessons — reducing ambiguity by making the exact valid output shape explicit rather than implied (Lesson 7 covers this principle more generally, including for tasks that are neither classification nor extraction).

Testing Extraction and Classification Logic

The parts of this code that do not require a live model call — output parsing, normalization, and grounding checks — should be tested directly with fake model output:

def test_grounding_passes_for_valid_quote():
    original = "Invoice #A-4471. Amount due: $230.00 by March 3."
    extracted = {"total_amount": 230.0, "source_quote": "Amount due: $230.00"}
    assert verify_grounding(extracted, original) is True
    print("PASS: valid quote found in source text")

def test_grounding_fails_for_fabricated_quote():
    original = "Invoice #A-4471. Amount due: $230.00 by March 3."
    extracted = {"total_amount": 500.0, "source_quote": "Amount due: $500.00"}
    assert verify_grounding(extracted, original) is False
    print("PASS: fabricated quote correctly flagged as ungrounded")

def test_null_total_requires_null_quote():
    extracted = {"total_amount": None, "source_quote": None}
    assert verify_grounding(extracted, "any text") is True
    print("PASS: null total with null quote is considered valid")

test_grounding_passes_for_valid_quote()
test_grounding_fails_for_fabricated_quote()
test_null_total_requires_null_quote()

This test suite exercises verify_grounding against hand-constructed extraction results, exactly mimicking both a correct and an incorrect model response, without spending any API quota. This is the dependency-injection pattern that matters throughout prompt engineering testing: the function under test takes plain data as input, so tests supply that data directly instead of needing a real model call to produce it.

Common Mistakes

Omitting an explicit fallback for missing or ambiguous cases. Both classification and extraction prompts that do not specify what to output for absent data or ambiguous input tend to produce confidently wrong answers instead of clear signals that a case needs special handling.

Leaving output format loosely specified. Instructions like "return the category" without specifying exact casing, punctuation, or whether to include an explanation lead to inconsistent output that breaks naive string comparison or JSON parsing in application code.

Skipping grounding or validation for high-stakes extraction. Trusting extracted values without any mechanism to catch hallucinated fields is acceptable for low-stakes internal tools, but risky for anything that feeds financial, medical, or legal downstream processing.

Best Practices

Enumerate the full category or field list explicitly in the prompt. Do not rely on the model inferring an implicit taxonomy; state every valid category or field name directly.

Always specify the behavior for missing, ambiguous, or out-of-scope input. An explicit "other," "uncertain," or "null" instruction converts an unhandled edge case into a defined, testable behavior.

Add source grounding for extraction tasks feeding critical downstream systems. Requiring an exact-quote field lets application code mechanically verify extracted values against the source text without an additional model call.

0 Comments

Reviewed before they appear

No comments yet.

OpenAI SDK
Introduction to the OpenAI SDK Setting Up Python Creating an API Key Your First Call — client.responses.create() and response.output_text Understanding Billing, Credits, and What a Request Costs Why Responses Replaced Chat Completions Anatomy of a Request: model, input, and instructions Anatomy of a Response: The Typed output Array, Not Just Text Roles: User, Assistant, and Developer/System Choosing a Model, and Reading the Models Page Instead of Memorizing Names Instructions vs. Input Writing Prompts That Get Consistent Results Few-Shot Examples Reasoning Models and the reasoning Parameter Debugging a Prompt That Misbehaves Why Streaming Matters for User Experience stream=True and Iterating Over Events Handling the Event Types You Actually Care About Background Mode for Long-Running Jobs Project — Add Live Streaming to Your Chatbot The Problem With Parsing Free Text JSON Schema and Strict Mode Pydantic Models With the SDK's Parse Helpers Handling Refusals and Validation Failures Project — A Resume-to-JSON Extractor Working With input_image input_file, PDFs, and the Files API Image Generation Speech-to-Text and Text-to-Speech Project: A PDF Question-Answering Script What Function Calling Is Defining a Tool Schema The Full Loop Multiple Tools Errors, Timeouts, and Untrusted Arguments Project: A Weather Assistant Web Search File Search and Vector Stores Code Interpreter Remote MCP Servers and Connectors Project: A Research Assistant What an Embedding Is, Without the Maths Generating and Storing Embeddings Similarity Search From Scratch Hosted Vector Stores vs. Rolling Your Own A Small RAG App Over a Folder of Notes Agents vs. a Single API Call — When You Need One pip install openai Giving Agents Tools Handoffs and Multi-Agent Triage Guardrails and Approvals Tracing and Observing What Your Agent Did A Multi-Agent Support Desk Error Codes and What Each One Means Retries, Timeouts, and Backoff Rate Limits and Spend Limits Prompt Caching and Cost Optimisation The Batch API for Bulk Work Async Clients and Concurrency Moderation and Safety Best Practices Designing the App Backend With FastAPI Streaming to a Simple Frontend Deploying and a Cost/Safety Checklist Why Web Search Is Useful for Current Information Using the Web Search Tool with the Responses API Configuring Search Behavior for Application Use Cases Understanding Citations and Source Attribution Where to Go Next Building a Research Assistant with Web Search Combining Web Search with Structured Outputs Handling Conflicting or Low-Quality Web Sources Reducing Unsupported Claims with Grounded Generation Testing Freshness-Sensitive AI Answers Production Considerations for Web-Grounded Applications Understanding File Search and Retrieval-Augmented Generation Creating and Organizing Vector Stores Uploading Documents for Retrieval Connecting Vector Stores to Responses API Requests Designing Document Metadata and Filtering Strategies Building a PDF Question-Answering Application Improving Retrieval Quality With Better Document Preparation Handling Missing Evidence and Retrieval Failures Combining File Search With Web Search Building a Production Knowledge-Base Assistant What the Code Interpreter Tool Is Designed For Running Python-Based Analysis Through the OpenAI SDK Uploading Datasets for Analysis Analyzing CSV and Spreadsheet Data Generating Charts and Data Summaries Handling Generated Files and Downloadable Artifacts Building a Data-Analysis Assistant Combining Code Execution with Structured Outputs Validating Generated Calculations and Results Security and Sandbox Considerations for Code Execution Understanding Multimodal Input with the OpenAI SDK Sending Images to a Model Image Analysis from URLs and Uploaded Files Extracting Text and Information from Screenshots Building an Image-Question-Answering Application Combining Image Input with Structured Output Analyzing Multiple Images in One Request Handling Image Quality and Input Limitations Designing Multimodal Prompts for Reliable Results Building a Practical Vision-Powered Python Application Understanding Speech-to-Text and Text-to-Speech Workflows Transcribing Audio with the OpenAI SDK Working with Uploaded Audio Files Handling Timestamps and Transcription Metadata Building a Meeting Transcription Workflow Generating Spoken Responses from Text Handling Long Audio and Processing Failures Combining Audio with Text and Tool Calling Building an End-to-End Python Voice Application What Embeddings Are and When to Use Them Generating Embeddings With the OpenAI API Preparing Text for Embedding Comparing Vectors With Cosine Similarity Building a Simple Semantic Search Engine in Python Storing Embeddings in a Database Metadata Filtering for Semantic Search Chunking Strategies for Better Retrieval Evaluating Semantic Search Quality Building a Document Similarity Application When Batch Processing Makes Sense Designing Large-Volume AI Processing Pipelines Using Asynchronous Python with the OpenAI SDK Running Concurrent Requests Safely Controlling Concurrency and Avoiding Rate Limits Tracking Batch Job Progress Handling Partial Failures in Bulk Workloads Retrying Failed Items Without Duplicating Successful Work Designing Resumable AI Processing Jobs Building a Production Batch-Processing Pipeline Batch Processing Makes Sense Large-Scale AI Processing Pipelines Async Python with OpenAI SDK Safe Concurrent Requests Concurrency & Rate Limits Batch Progress Tracking Partial Failure Handling Safe Retry Handling Resumable AI Jobs Production Batch Pipeline System–User Data Separation Reusable App Instructions Prompt Templates & Variables Extraction & Classification Prompts Summarization & Transformation Prompts Explicit Output Requirements Prompt Version Management Prompt Testing & Evaluation Reusable Python Prompt Library API Key Security Secure API Key Storage Secure Secret Management Prompt Injection Prevention Trusted vs. Untrusted Content Tool Argument Validation Sensitive Data Handling Secure Logging AI Action Authorization Production AI Security Checklist Why AI Applications Need Evaluation Beyond Unit Tests Unit Testing OpenAI SDK Integration Code Mocking API Responses in Python Tests Testing Structured Outputs Against Schemas Testing Tool-Calling Workflows Building a Small Evaluation Dataset Measuring Accuracy, Consistency, and Failure Rates Regression Testing Prompts and Model Changes Human Evaluation Versus Automated Evaluation Creating a Repeatable Evaluation Pipeline AI Request Monitoring Token Cost Management Usage Metrics Design Reducing Model Calls Prompt & Context Optimization Model Selection & Optimization AI Caching Strategies Interactive Latency Optimization Usage Dashboards & Budget Alerts Performance & Cost Checklist Every API Call Starts Fresh Fixing API Statelessness Server-Side Conversation Memory Limits of Response Chaining What We're Building Conversation Memory Challenges Preparing an OpenAI SDK Application for Deployment Environment-Specific Configuration for Development and Production Deploying a Python AI Service with Docker Container Health Checks and Startup Configuration Managing Secrets in Cloud Deployments Background Workers for Long-Running AI Tasks Queues and Asynchronous Job Architectures Scaling AI Workloads Horizontally Monitoring Production Incidents and Failures Production Deployment Checklist for OpenAI SDK Applications Reusable OpenAI Service Classes AI Client Dependency Injection Typed AI Responses Python Configuration Management AI Request Decorators Centralized AI Error Handling Clean SDK Abstractions Reusable OpenAI Utilities Internal AI Python Libraries SDK Integration Maintenance Production AI Chatbot Document Q&A System Web Research Assistant Customer Support Agent AI Data Analysis Assistant Image Analysis App Meeting Transcription & Summary Semantic Document Search Multi-Tool AI Agent Production OpenAI SDK App Why "It Looked Fine When I Tested It" Isn't Enough Timing Note Status Note Pre-Decision Status Note Current Availability Note
Ask about this post
AI Ask about this post

Ask questions about Extraction & Classification Prompts and get answers drawn from it.

Signed-in readers only.