본문 바로가기
C.W.K.
Stream
Lesson 05 of 05 · published

PDF Understanding + Structured Output

~22 min · structured-outputs, pdf, input_file

Level 0Tokenizer
0 XP0/54 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

input_file 로 PDF 를 직접 보내면 별도 chunking 이나 OCR pipeline 없이 내용을 읽힐 수 있어. 여기에 response_format 의 Pydantic 모델을 결합하면 짧은 코드로 invoice 나 report 추출기를 만들 수 있어.

여러 전처리 단계를 한 호출로 줄여

예전에는 pdfplumber 로 text 를 꺼내고, chunking 과 embedding, vector search, prompt 조립, JSON parsing 을 이어 붙이곤 했어. 문서가 한 번에 처리 가능한 크기라면 input_fileresponse_format 으로 이 단계를 한 API 호출에 묶을 수 있어.

아주 긴 문서는 여전히 나눠야 해

input_file 은 PDF 전체를 한 번에 처리하므로 100 페이지가 넘는 긴 문서는 context window 한도에 걸릴 수 있어. 이런 경우에는 chunking 이 필요해. invoice, report, contract, 한 장짜리 form 에는 직접 입력 방식이 잘 맞아.

Pydantic 모델이 출력 구조를 정해

class Invoice(BaseModel): vendor: str; invoice_number: str; total: float; currency: str; line_items: list[LineItem] 를 만들고 response_format=Invoice 로 호출하면 그 구조에 맞는 결과를 받아 방어적인 parsing 을 줄일 수 있어.

Code

input_file 로 PDF 보내기·python
completion = client.chat.completions.create(
    model="gpt-5.4",
    messages=[{"role": "user", "content": "Return a JSON with name and age for 'Alice, 30'"}],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "person",
            "strict": True,
            "schema": {
                "type": "object",
                "properties": {
                    "name": {"type": "string"},
                    "age": {"type": "integer"},
                },
                "required": ["name", "age"],
                "additionalProperties": False,
            }
        }
    }
)
import json
person = json.loads(completion.choices[0].message.content)
response_format 으로 structured 추출·python
from pydantic import BaseModel

class InvoiceData(BaseModel):
    vendor: str
    total: float
    items: list[str]
    date: str

response = client.responses.parse(
    model="gpt-5.4",
    input=[{
        "role": "user",
        "content": [
            {"type": "input_text", "text": "Extract invoice data from this PDF:"},
            {"type": "input_file", "file_id": "file-abc123"},
        ]
    }],
    text_format=InvoiceData,
)
invoice = response.output_parsed  # typed InvoiceData object

External links

Exercise

PDF invoice 를 골라 input_file 과 response_format 으로 {vendor, invoice_number, total, currency, line_items[]}를 추출해. 서로 다른 PDF 다섯 개에 실행하고 가장 자주 실패하는 field 를 확인해.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.