C.W.K.
Stream
Lesson 07 of 07 · published

Structured Outputs 와 Batch API

~22 min · structured-outputs, pydantic, batch

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

'JSON 으로 답해줘' 프롬프트는 folklore 야 — 모델이 가끔 commentary, code fence, trailing prose 를 끼워 넣어서 defensive 파싱 + retry 지옥. Structured output 은 다른 layer — response_format 에 Pydantic schema 박으면 모델이 그 shape 외엔 못 돌려줘.

왜 folklore 보다 강한가

Schema 강제는 server-side. API 가 non-conforming 출력을 거부하고 모델한테 다시 시켜. 코드는 typed Pydantic instance 받아. Defensive 파싱이 통째로 사라져.

Batch API — 50% 할인 대신 24h

50K prompt 처리해야 하는데 interactive 아니고 cost 를 반으로 줄이고 싶다면 Batch API 가 답. JSONL 업로드, 24h 안에 완료, 결과 JSONL 다운로드. 채팅엔 catastrophically wrong (latency unbounded), eval/dataset gen/moderation backfill 엔 perfect.

실전 비교 exercise

같은 작업을 prompt-json 방식과 response_format 방식 둘 다 돌려서 fail rate 측정해 봐. 보통 prompt-json 은 1-3% fail, response_format 은 거의 0. 그 1-3% 가 production 에서 alert 으로 돌아옴.

Code

Pydantic 으로 response_format structured output·python
from pydantic import BaseModel
from openai import OpenAI

client = OpenAI()

class ResearchSummary(BaseModel):
    title: str
    key_findings: list[str]
    conclusion: str
    confidence_score: float

# Using responses.parse() for structured output
response = client.responses.parse(
    model="gpt-5.4",
    input="Summarize the key findings of quantum computing research.",
    text_format=ResearchSummary,
)
summary: ResearchSummary = response.output_parsed
print(summary.title)
print(summary.key_findings)
Batch API: submit + poll + retrieve·python
import json
from openai import OpenAI

client = OpenAI()

# 1. Create JSONL batch file
requests = [
    {"custom_id": f"request-{i}", "method": "POST", "url": "/v1/responses",
     "body": {"model": "gpt-5.4", "input": f"Summarize: {text}"}}
    for i, text in enumerate(texts)
]
with open("batch_input.jsonl", "w") as f:
    for req in requests:
        f.write(json.dumps(req) + "\\n")

# 2. Upload → Create batch → Poll → Retrieve
with open("batch_input.jsonl", "rb") as f:
    batch_file = client.files.create(file=f, purpose="batch")
batch = client.batches.create(
    input_file_id=batch_file.id,
    endpoint="/v1/responses",
    completion_window="24h",
)

External links

Exercise

Pydantic 모델 RecipeCard{title:str, prep_min:int, ingredients:list[str], steps:list[str]} 정의. 5 개 레시피를 (1) response_format 으로, (2) 손으로 만든 prompt + json.loads 로 받아. Fail rate 비교.

Progress

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

댓글 0

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

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