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

Pydantic / Zod / Schema가 contract야

~14 min · outputs, schema, contracts

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

한 schema, 여러 consumer

Output schema가 consume되는 곳: 프롬프트 (JSON Schema export), validator (Pydantic / Zod), downstream code (TypeScript / Python type), test suite (sample fixture), docs (API 묘사). 한 source of truth, 여러 derived artifact.

어떻게 wire

  • Pydantic (Python)이나 Zod (TypeScript)에서 author.
  • model_json_schema()zodToJsonSchema로 JSON Schema 생성.
  • JSON Schema를 structured-output spec로 프롬프트에 inject.
  • 같은 Pydantic / Zod 모델로 response validate.
  • 같은 type으로 consumer type.

win

Schema 바꾸면 모든 dependent layer가 compile time에 break. 뭐 바꿔야 할지 보임. Single source of truth 없으면 schema 변경이 silently desync — 프롬프트가 옛 shape 요청, 코드가 새 shape 기대, runtime crash.

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

inline output spec hand-maintain되는 프롬프트 골라. Pydantic / Zod schema와 generator로 교체. consumer type과 prompt schema가 한 source에서 derive되는지 verify.

Progress

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

댓글 0

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

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