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

Output repair loop

~14 min · outputs, repair

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

Strict 모드도 놓치는 거 있어

Structured-output strict 모드가 JSON syntactic validity 보장. 값이 semantically valid는 보장 X: enum mismatch, range 밖 값, business-rule 위반. validator와 repair loop 여전히 필요.

패턴

  1. Generate.
  2. Pydantic / Zod / JSON Schema validator로 semantic rule 가지고 validate.
  3. Invalid면 모델한테 validation error 보내고 fix 요청.
  4. Retry cap — 보통 2나 3.
  5. 여전히 invalid면 user한테 structured error.

이게 prompt 이슈인 이유

Repair loop는 프롬프트가 validation rule 미리 설명하면 cheap. "reason_codes는 enum [low_confidence, missing_evidence, policy_block, ok]에서 와야; 적용 enum 값 없으면 'ok' notes 있게 써"가 모델이 rejection으로 rule discover하게 두는 것보다 훨씬 cheap.

Code

Validator-driven repair·python
from pydantic import ValidationError

for attempt in range(3):
    raw = call_model(prompt, messages)
    try:
        parsed = VerdictSchema.model_validate_json(raw)
        return parsed
    except ValidationError as e:
        prompt = f"{prompt}\n\nLast attempt failed validation:\n{raw}\nError:\n{e}\nReturn a corrected JSON."
raise RuntimeError("validation failed")

External links

Exercise

기존 structured-output 프롬프트에 2-retry repair loop 추가. 두 번째 시도 성공 빈도 log. 높으면 missing rule로 프롬프트 harden.

Progress

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

댓글 0

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

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