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

구조화 출력 — JSON 모드 그 너머

~18 min · outputs, json, structured

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

이제 스키마를 지키게 강제할 수 있어

2023년에는 모델에게 JSON으로 돌려달라고 부탁한 뒤 깨지지 않길 바랐어. 2026년에는 주요 제공업체가 모두 구조화 출력 모드를 지원해. 생성 단계에서 토큰마다 스키마를 검사하고, 잘못된 JSON을 만드는 토큰은 내보내지 못하게 막아. 출력 구조가 프롬프트의 부탁이 아니라 서버의 보장이 된 거야.

제공업체별 방식

  • OpenAIresponse_format = {"type": "json_schema", "json_schema": {...}}를 쓰며, 엄격 모드는 스키마를 정확히 강제해.
  • Anthropic — 단일 도구의 input_schema를 원하는 출력으로 삼고 그 도구 사용을 강제하는 방식으로 JSON을 받을 수 있어. 새 SDK에는 구조화 출력 전용 끝점도 있어.
  • Geminiresponse_mime_type = "application/json"response_schema를 함께 써.

형식을 강제해도 의미는 프롬프트가 맡아

구조화 출력이 켜져도 각 필드의 뜻, 필드 사이 관계, null과 빈 값의 처리 규칙은 프롬프트가 설명해야 해. 스키마는 모양을 보장하고 프롬프트는 의미를 정해. 구조화 출력이 지시문을 대신한다고 생각하지 마.

Code

OpenAI의 엄격한 JSON 스키마·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 — 도구를 스키마로 쓰는 방식·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을 받는 프롬프트를 골라 엄격한 스키마를 가진 구조화 출력 모드로 옮겨봐. 입력 쉰 개를 실행해 파싱 실패율을 비교해.

Progress

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

댓글 0

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

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