C.W.K.
Stream
Lesson 01 of 10 · published

Structured output — JSON 모드 그 너머

~18 min · outputs, json, structured

Level 0수련생
0 XP0/100 lessons0/14 achievements
0/120 XP to next level120 XP to go0% complete

이제 모델이 schema 따르도록 강제 가능

2023엔 모델한테 정중히 JSON return 해달라 부탁하고 기도했어. 2026엔 모든 주요 provider가 structured-output 모드 지원 — 모델이 decoding 수준에서 constraint, sample되는 모든 토큰이 schema 체크되고, invalid JSON 만들 토큰은 emit 안 돼. Output 구조가 더 이상 prompt instruction이 아니라 server-side 보장이야.

provider별 feature

  • OpenAIresponse_format = {"type": "json_schema", "json_schema": {...}}. Strict 모드가 schema 정확히 enforce.
  • Anthropic — tool calling으로 JSON 모드 (단일 tool의 input_schema가 desired output, 사용 force). 신규 SDK에 native structured-output endpoint 있음.
  • Geminiresponse_mime_type = "application/json"response_schema.

프롬프트가 여전히 하는 일

structured output enforce돼도 프롬프트가 heavy lifting: 각 field의 의미, field 사이 관계, null/empty 값 policy를 설명. Schema는 shape, 프롬프트는 semantics. structured output을 instruction 대체로 다루지 마.

Code

OpenAI strict JSON schema·python
client.chat.completions.create(
    model="gpt-5.5",
    messages=[...],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "verdict",
            "strict": True,
            "schema": {
                "type": "object",
                "properties": {
                    "verdict": {"type": "string", "enum": ["approve", "reject", "hold"]},
                    "reason_codes": {"type": "array", "items": {"type": "string"}, "maxItems": 3}
                },
                "required": ["verdict", "reason_codes"],
                "additionalProperties": False
            }
        }
    }
)
Anthropic — tool-as-schema 패턴·python
tools = [{
    "name": "emit_verdict",
    "description": "Return the underwriting verdict.",
    "input_schema": {
        "type": "object",
        "properties": {
            "verdict": {"type": "string", "enum": ["approve", "reject", "hold"]},
            "reason_codes": {"type": "array", "items": {"type": "string"}}
        },
        "required": ["verdict", "reason_codes"]
    }
}]

client.messages.create(
    model="claude-opus-4-7",
    tools=tools,
    tool_choice={"type": "tool", "name": "emit_verdict"},
    messages=[...]
)

External links

Exercise

'please return JSON'으로 JSON return하는 프롬프트 골라. strict schema 가진 structured-output 모드로 migrate. 50개 input에 돌려. parse-failure rate 비교.

Progress

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

댓글 0

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

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