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

Pydantic·Zod 스키마가 계약이야

~14 min · outputs, schema, contracts

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

스키마 하나를 여러 소비자가 나눠 써

출력 스키마는 JSON Schema로 내보내 프롬프트에 들어가고, Pydantic이나 Zod 검증기가 읽으며, TypeScript·Python 코드의 형식과 시험 자료, API 문서에도 쓰여. 기준은 하나만 두고 나머지는 모두 거기서 만들어야 해.

한 줄기로 연결하는 법

  • Python이면 Pydantic, TypeScript면 Zod에서 원본 스키마를 작성해.
  • model_json_schema()zodToJsonSchema로 JSON Schema를 만들어.
  • 그 JSON Schema를 구조화 출력 명세로 프롬프트에 넣어.
  • 응답도 같은 Pydantic이나 Zod 모델로 검증해.
  • 소비자 코드의 형식 역시 같은 원본에서 만들어.

어긋남이 장애가 아니라 빌드 오류가 돼

스키마를 바꾸면 의존하는 계층이 컴파일 단계에서 깨져 무엇을 고쳐야 하는지 보여줘. 단일 기준이 없으면 프롬프트는 옛 모양을 요구하고 코드는 새 모양을 기다리는 불일치가 조용히 쌓이다 실행 중 장애로 터져.

Code

Pydantic → JSON Schema → 프롬프트·python
from pydantic import BaseModel, Field
from typing import Literal

class Verdict(BaseModel):
    verdict: Literal["approve", "reject", "hold"]
    reason_codes: list[Literal["low_confidence", "missing_evidence", "policy_block", "ok"]] = Field(min_length=1, max_length=3)

schema = Verdict.model_json_schema()
response_format = {"type": "json_schema", "json_schema": {"name": "verdict", "strict": True, "schema": schema}}

resp = client.chat.completions.create(model="gpt-5.5", messages=msgs, response_format=response_format)
parsed = Verdict.model_validate_json(resp.choices[0].message.content)

External links

Exercise

출력 명세를 프롬프트 안에서 손으로 관리하는 사례를 골라 Pydantic이나 Zod 스키마와 생성기로 바꿔봐. 소비자 형식과 프롬프트 스키마가 같은 원본에서 나오는지 확인해.

Progress

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

댓글 0

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

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