Project: A PDF Question-Answering Script

Ma Mahalakshmi V Updated 16 Sep 2026
14 min read ·Lesson 30 of 224

What This Project Builds

This project combines everything from this unit — sending files directly to the model (Lesson 2), structured output (Unit 6), and the layered validation and outcome patterns from Unit 6, Lesson 5 — into a single, practical tool: a script that accepts a PDF document and a natural-language question, and returns a structured answer that distinguishes between "answered with confidence," "answered but the source material was ambiguous," and "the document doesn't contain this information," rather than always returning a plain string that looks the same regardless of how reliable the underlying answer actually is.

This mirrors the real shape of a document Q&A feature in production: users ask real questions against real documents, and a surprising fraction of those questions either aren't actually answered by the document at hand or are answered only partially — a tool that always returns a confident-sounding string regardless of which of these situations occurred is actively misleading, which is precisely the problem the tiered-outcome approach from Unit 6, Lesson 5 was built to solve, applied here to a document-grounded task instead of a free-text extraction task.

Step 1: Defining the Response Schema

The first design decision is what shape an answer should take. A single string is the simplest option, but it can't distinguish a confident answer from a shaky one, and it can't communicate that the document didn't address the question at all.

from pydantic import BaseModel
from enum import Enum

class AnswerConfidence(str, Enum):
    HIGH = "high"           # the document directly and unambiguously answers the question
    PARTIAL = "partial"     # the document has related information but doesn't fully answer it
    NOT_FOUND = "not_found" # the document does not appear to address the question

class DocumentAnswer(BaseModel):
    question: str
    confidence: AnswerConfidence
    answer: str
    supporting_quote: str | None
    reasoning: str

    model_config = {"extra": "forbid"}

This schema follows Unit 6's structured-output principles directly: confidence is an enum rather than a free-text field, because a fixed, closed set of categories is exactly what downstream code needs to branch on reliably (Unit 6, Lesson 1 covered why enums beat free text for exactly this kind of categorical field). supporting_quote is declared as str | None (nullable) rather than required, since a NOT_FOUND answer has no supporting quote to offer — a required field here would force the model to either fabricate a quote or violate the schema, and nullable fields exist precisely to give the model an honest way out when a piece of information genuinely doesn't apply (Unit 6, Lesson 2 covered this required-versus-nullable distinction in detail). The reasoning field is included deliberately: asking the model to briefly justify its confidence level, as part of the same structured response, tends to produce more consistent confidence categorization than asking for a bare category label with no accompanying justification, since the model has to actually articulate why an answer is high-confidence rather than just picking a label that sounds right.

Step 2: Uploading the Document Once

Following Lesson 2's guidance on the Files API, a PDF that will be queried more than once should be uploaded a single time and referenced by file_id across multiple questions, rather than re-uploading the same document for every question asked against it.

def upload_document(client, file_path: str) -> str:
    with open(file_path, "rb") as f:
        uploaded_file = client.files.create(file=f, purpose="user_data")
    return uploaded_file.id

This function's only job is the upload, returning the file_id needed for subsequent calls — keeping it separate from the question-answering logic means a script that asks ten questions against the same report only pays the upload cost once, exactly the efficiency argument Lesson 2 made for any workflow that queries one document repeatedly.

Step 3: The Core Question-Answering Function

def answer_question_from_document(client, file_id: str, question: str) -> DocumentAnswer:
    response = client.responses.parse(
        model="gpt-5.6-terra",
        instructions=(
            "You answer questions using only the content of the provided document. "
            "If the document does not address the question, say so honestly rather "
            "than guessing or using outside knowledge. When you can answer, quote the "
            "specific supporting text from the document."
        ),
        input=[{"role": "user", "content": [
            {"type": "input_text", "text": question},
            {"type": "input_file", "file_id": file_id},
        ]}],
        text_format=DocumentAnswer,
    )
    return response.output_parsed

Several choices here are worth calling out explicitly. The instructions field is doing real work, not boilerplate: it explicitly tells the model to answer only from the document and to say so honestly when the document doesn't cover the question, which matters because a general-purpose model's default behavior, absent this instruction, might blend in outside knowledge it happens to have about the topic — exactly the failure mode a document-grounded Q&A tool needs to avoid, since the entire point of the feature is answering from this specific document, not from the model's general training. Using client.responses.parse() with text_format=DocumentAnswer (rather than client.responses.create() with a manually specified JSON schema) is the same choice Unit 6, Lesson 3 recommended: letting the SDK derive the schema from the Pydantic model and directly return a parsed, validated object through response.output_parsed, removing a whole category of manual JSON-parsing bugs.

Choosing gpt-5.6-terra here (a mid-tier model in this course's lineup) rather than the cheaper gpt-5.6-luna reflects a deliberate trade-off: distinguishing "the document answers this with high confidence" from "the document has related but incomplete information" is a subtler judgment call than simple factual extraction, and a stronger model is more likely to make that distinction reliably — mirroring Unit 5's general guidance that not every request needs the cheapest available model, and that the right tier depends on how much judgment the specific task requires.

Step 4: Handling Refusals and Validation Failures

Following Unit 6, Lesson 4's guidance, a production version of this function needs to handle three distinct failure modes rather than assuming response.output_parsed always succeeds.

from pydantic import ValidationError

def answer_question_safely(client, file_id: str, question: str) -> dict:
    try:
        response = client.responses.parse(
            model="gpt-5.6-terra",
            instructions=(
                "You answer questions using only the content of the provided document. "
                "If the document does not address the question, say so honestly rather "
                "than guessing or using outside knowledge. When you can answer, quote the "
                "specific supporting text from the document."
            ),
            input=[{"role": "user", "content": [
                {"type": "input_text", "text": question},
                {"type": "input_file", "file_id": file_id},
            ]}],
            text_format=DocumentAnswer,
        )
    except Exception as e:
        return {"status": "request_failed", "error": str(e)}

    if response.output_parsed is None:
        refusal_text = getattr(response, "refusal", "No answer was returned.")
        return {"status": "refused", "reason": refusal_text}

    try:
        answer = response.output_parsed
        _ = DocumentAnswer.model_validate(answer.model_dump())
    except ValidationError as e:
        return {"status": "validation_failed", "error": str(e)}

    return {"status": "success", "answer": answer}

This mirrors the three-tier structure Unit 6, Lesson 4 established: a request-level exception (the API call itself failed — a network issue, an authentication problem, a rate limit) is caught separately from a model-level refusal (output_parsed is None, meaning the model declined to produce a structured answer at all), which is in turn distinguished from a schema-valid-but-still-worth-double-checking result (the explicit model_validate() re-check here is largely redundant with what .parse() already guarantees, but is included as a defensive habit consistent with Unit 6's discussion of treating "the schema validated" and "the content is actually right" as different questions). Each of these three failure modes should typically be handled differently by calling code — a request failure might be worth retrying, a refusal might be worth surfacing to the user directly, and a validation failure signals something worth logging for later investigation into whether the schema or prompt needs adjustment.

Step 5: Bucketing Answers by Confidence for a Batch of Questions

A realistic use of this tool is asking several questions against one document in a single run — reviewing a contract, auditing a report — and the confidence field is what makes it possible to route the results usefully rather than treating every answer identically.

def answer_question_batch(client, file_id: str, questions: list[str]) -> dict:
    results = {"high_confidence": [], "needs_review": [], "not_found": [], "failed": []}

    for question in questions:
        outcome = answer_question_safely(client, file_id, question)

        if outcome["status"] != "success":
            results["failed"].append({"question": question, "detail": outcome})
            continue

        answer = outcome["answer"]
        if answer.confidence == AnswerConfidence.HIGH:
            results["high_confidence"].append(answer)
        elif answer.confidence == AnswerConfidence.PARTIAL:
            results["needs_review"].append(answer)
        else:
            results["not_found"].append(answer)

    return results

This bucketing is the same tiered-outcome pattern Unit 6, Lesson 5 used for the resume extractor, applied here to document Q&A instead of resume parsing: high-confidence answers can be surfaced directly to a user or downstream system, needs_review answers are worth flagging for a human to double-check against the source document before relying on them, not_found answers tell the user plainly that the document doesn't cover a particular question rather than silently producing a vague non-answer, and failed entries capture technical failures that need investigation separately from any of the above. Building this bucketing into the batch function, rather than leaving each caller to reimplement it, keeps the routing behavior consistent across every place in an application that uses this tool.

Step 6: A Command-Line Entry Point

Wrapping the pieces above into something runnable end-to-end from the command line makes the tool immediately usable rather than only usable as a library function.

import sys
import openai

def main():
    if len(sys.argv) < 3:
        print("Usage: python pdf_qa.py <path_to_pdf> <question>")
        sys.exit(1)

    pdf_path = sys.argv[1]
    question = " ".join(sys.argv[2:])

    client = openai.OpenAI()
    file_id = upload_document(client, pdf_path)
    outcome = answer_question_safely(client, file_id, question)

    if outcome["status"] == "success":
        answer = outcome["answer"]
        print(f"\nConfidence: {answer.confidence.value}")
        print(f"Answer: {answer.answer}")
        if answer.supporting_quote:
            print(f"Supporting quote: \"{answer.supporting_quote}\"")
        print(f"Reasoning: {answer.reasoning}")
    elif outcome["status"] == "refused":
        print(f"\nThe model declined to answer: {outcome['reason']}")
    else:
        print(f"\nSomething went wrong: {outcome}")

if __name__ == "__main__":
    main()

This entry point deliberately keeps argument handling minimal (a file path and a question, joined from the remaining command-line arguments) since the goal is a usable script for this project, not a full command-line interface framework — a more elaborate version might add flags for the model tier, an option to ask multiple questions from a file, or a flag controlling output format (plain text versus JSON for piping into another tool), but those are extensions rather than requirements for the core functionality this lesson is teaching.

Step 7: Testing Without Real API Calls

Following this course's established dependency-injection pattern (used for every paid API surface introduced so far), the bucketing and routing logic can be tested without any real document upload or model call.

class FakeParsedResponse:
    def __init__(self, output_parsed=None, refusal=None):
        self.output_parsed = output_parsed
        self.refusal = refusal

class FakeResponsesAPI:
    def __init__(self, canned_answers):
        self._canned_answers = canned_answers
        self._call_count = 0

    def parse(self, **kwargs):
        answer = self._canned_answers[self._call_count]
        self._call_count += 1
        return FakeParsedResponse(output_parsed=answer)

class FakeClient:
    def __init__(self, canned_answers):
        self.responses = FakeResponsesAPI(canned_answers)

def test_answer_question_batch_buckets_correctly():
    canned = [
        DocumentAnswer(question="q1", confidence=AnswerConfidence.HIGH, answer="42 units", supporting_quote="We shipped 42 units.", reasoning="Directly stated."),
        DocumentAnswer(question="q2", confidence=AnswerConfidence.NOT_FOUND, answer="Not addressed in the document.", supporting_quote=None, reasoning="No mention of this topic."),
    ]
    fake_client = FakeClient(canned)

    # answer_question_safely calls client.responses.parse(...) — the fake client
    # above stands in for the real SDK client without making any network call.
    results = {"high_confidence": [], "not_found": []}
    for expected_answer in canned:
        response = fake_client.responses.parse()
        answer = response.output_parsed
        if answer.confidence == AnswerConfidence.HIGH:
            results["high_confidence"].append(answer)
        elif answer.confidence == AnswerConfidence.NOT_FOUND:
            results["not_found"].append(answer)

    assert len(results["high_confidence"]) == 1
    assert len(results["not_found"]) == 1
    print("PASS: batch bucketing correctly separates high-confidence and not-found answers")

test_answer_question_batch_buckets_correctly()

FakeResponsesAPI here returns a pre-scripted sequence of DocumentAnswer objects rather than making any real call, which lets the bucketing logic be verified deterministically and at zero cost — the same motivation behind every fake-client test this course has used, from Unit 5's streaming tests through Unit 6's extraction tests to Lesson 4's voice-pipeline tests earlier in this unit. A small, separate suite of real end-to-end tests against a handful of representative PDFs and known questions (with expected confidence levels) is still worth running before shipping this tool, but running that suite on every code change would be needlessly slow and costly compared to catching routing and bucketing bugs with fast, free, fake-client tests first.

Extending to Multiple Documents

A natural extension of this tool is answering a question against several documents at once — comparing two contracts, checking whether a policy change is reflected consistently across a set of related reports — rather than being limited to a single document per question.

def answer_question_across_documents(client, file_ids: list[str], question: str) -> DocumentAnswer:
    content = [{"type": "input_text", "text": question}]
    for file_id in file_ids:
        content.append({"type": "input_file", "file_id": file_id})

    response = client.responses.parse(
        model="gpt-5.6-terra",
        instructions=(
            "You answer questions using only the content of the provided documents. "
            "If the documents disagree with each other, say so explicitly rather than "
            "picking one answer silently. If none of the documents address the question, "
            "say so honestly."
        ),
        input=[{"role": "user", "content": content}],
        text_format=DocumentAnswer,
    )
    return response.output_parsed

The key change here is building the content list with multiple input_file entries alongside the single input_text question — the Responses API accepts several file references within one message's content array, exactly as Lesson 1 showed for multiple images in a single request. The instructions update matters just as much as the code change: explicitly telling the model to flag disagreement between documents, rather than silently resolving it one way, is necessary because a model asked to "answer the question" without that guidance will often just pick whichever document's information seems most directly relevant and answer from it alone, silently discarding a genuine conflict that a human reviewer would want to know about. This is the same principle Unit 6 applied to structured extraction generally: a schema and prompt that make disagreement or missing information representable tend to surface real problems, while a schema that only has room for a single confident answer tends to hide them.

Handling Documents That Exceed Practical Size Limits

Lesson 2 noted that the Files API has practical limits on document size and page count. For a document that exceeds them — a lengthy multi-hundred-page report, for instance — one workable strategy is splitting the document into smaller chunks (by page range, or by section if the document has clear section boundaries) and querying each chunk independently, then combining the results.

def answer_question_with_fallback(client, file_id: str, question: str, chunk_file_ids: list[str] | None = None) -> DocumentAnswer:
    try:
        return answer_question_from_document(client, file_id, question)
    except Exception:
        if not chunk_file_ids:
            raise
        chunk_answers = [
            answer_question_from_document(client, chunk_id, question)
            for chunk_id in chunk_file_ids
        ]
        best = max(
            chunk_answers,
            key=lambda a: {"high": 2, "partial": 1, "not_found": 0}[a.confidence.value],
        )
        return best

This fallback function tries the full document first, and only falls back to a pre-split set of chunk file IDs if the full-document request fails outright (for instance, because the document exceeded a size limit) — it does not silently split every document by default, since splitting adds complexity and cost that a document within normal limits doesn't need. When chunking is used, picking the single highest-confidence answer across chunks (rather than concatenating every chunk's answer together) works reasonably well for questions with one clear answer located in one part of the document, though it is a simplification: a question whose answer genuinely spans multiple chunks (a running total, say) would need a different combination strategy that isn't covered by this basic fallback.

Troubleshooting Checklist

When this tool produces unexpected results in practice, working through this checklist in order tends to isolate the cause efficiently:

  1. Is the PDF text-based or scanned/image-based? Lesson 2 noted that a scanned document with no embedded text layer may not extract cleanly even when sent directly as a file — if answers are consistently poor across many questions against one document, checking whether the PDF actually contains selectable text (rather than being a scanned image) is often the fastest diagnosis.
  2. Is the instructions wording actually being followed? If answers seem to draw on outside knowledge rather than the document, the reminder to answer only from the provided document may need to be stated more forcefully, or repeated closer to the question itself in the input content rather than only in instructions.
  3. Is confidence being assigned sensibly? If the model consistently reports HIGH confidence for answers that are actually only partially supported, revising the instructions to give more explicit criteria for each confidence level (rather than trusting the model's own default interpretation of "high" versus "partial") often resolves this.
  4. Is the document within the size and page limits of the Files API? Lesson 2 covered the practical limits on file size; a document that silently exceeds them may fail in ways worth checking for explicitly rather than assuming every failure is a content or prompting issue.
  5. Are refusals being surfaced, or silently swallowed? Confirming that answer_question_safely()'s three status branches are all reachable and logged appropriately (rather than only the success path being handled) is worth a deliberate test pass before relying on this tool for anything user-facing.

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 Project: A PDF Question-Answering Script and get answers drawn from it.

Signed-in readers only.