구조화된 출력 파싱 및 검증
출력 파서와 스키마를 사용하여 LLM이 신뢰할 수 있는 구조화된 데이터를 반환하도록 하고, 모델이 잘못된 출력을 생성하면 검증하거나 다시 시도합니다.
구조화된 출력 파싱 및 검증은(는) CoddyKit의 무료 AI Agents with LangChain & Autonomous Workflows 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 AI Agents with LangChain & Autonomous Workflows 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. AI Agents with LangChain & Autonomous Workflows 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
The Problem with Free Text
LLMs return prose by default, but your code needs structured data: JSON, a list, a typed object. Parsing free text with regex is fragile.
This lesson covers getting reliable structured output from models.
Asking for a Format
The first step is simply instructing the model to produce a specific format. But instruction alone is not enough; models drift, add prose, or wrap output in markdown.
prompt = 'Extract name and age as JSON: "Lena is 30"'
# model might reply: 'Sure! {"name":"Lena","age":30}'Output Parsers
LangChain output parsers do two jobs: they generate format instructions to inject into the prompt, and they parse the model's response back into a structured object.
from langchain.output_parsers import CommaSeparatedListOutputParser
parser = CommaSeparatedListOutputParser()
print(parser.get_format_instructions())Schema-Based Parsing
Define the shape you want with a schema (e.g. a Pydantic model). The parser turns it into instructions and validates the result against the fields and types.
from pydantic import BaseModel
class Person(BaseModel):
name: str
age: intInjecting Format Instructions
Add the parser's instructions into your prompt template so the model knows exactly what structure to emit.
template = 'Extract info.\n{format_instructions}\nText: {text}'
prompt = template.format(
format_instructions=parser.get_format_instructions(),
text='Lena is 30')Parsing the Response
After the model replies, the parser converts the text into your typed object, raising an error if it does not match the schema.
result = parser.parse(model_output)
print(result.name, result.age)Handling Malformed Output
Models occasionally produce invalid JSON. A retry/fixing parser detects the failure and asks the model to correct its own output, turning a hard crash into a recoverable step.
from langchain.output_parsers import RetryOutputParser
robust = RetryOutputParser.from_llm(parser=parser, llm=llm)Native JSON / Tool Modes
Many modern models support a JSON mode or function/tool calling that constrains output to valid structured data at the API level. When available, this is far more reliable than prompt instructions alone.
Validation Beyond Types
A value can be the right type but still wrong: a negative age, an empty required field. Add validators so business rules are enforced, not just the data shape.
if result.age < 0 or result.age > 130:
raise ValueError('age out of range')Why It Matters for Agents
Agents chain steps together, feeding one output into the next. If a step emits malformed data, the whole chain breaks. Structured, validated output is what makes multi-step agents dependable.
A Reliable Output Workflow
Putting it together:
- Define a schema for the data you need
- Inject format instructions into the prompt
- Prefer native JSON/tool mode when available
- Parse and validate, with a retry parser as a safety net
Quick Check
Test your understanding of structured output.
Recap
You learned to get reliable structured data from LLMs.
- Output parsers generate instructions and parse responses
- Schemas validate shape and types
- Retry parsers recover from malformed output
- Native JSON/tool modes are most reliable when available
AI 튜터와 함께 AI Agents with LangChain & Autonomous Workflows을(를) 배우세요 — 무료
브라우저에서 실제 코드를 작성하고 실행하며, 24/7 AI 튜터로부터 즉각적인 도움을 받고, 웹이나 앱에서 중단한 부분부터 계속 학습하세요.
- 코스
- 12
- 레슨
- 50
자주 묻는 질문
“구조화된 출력 파싱 및 검증” 강의는 무료인가요?
네 — “구조화된 출력 파싱 및 검증” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 AI Agents with LangChain & Autonomous Workflows 강의 전체를 잠금 해제할 수 있습니다. AI Agents with LangChain & Autonomous Workflows 강의에는 총 4개의 강의가 포함되어 있습니다.
“구조화된 출력 파싱 및 검증”에서 뭘 배우나요?
출력 파서와 스키마를 사용하여 LLM이 신뢰할 수 있는 구조화된 데이터를 반환하도록 하고, 모델이 잘못된 출력을 생성하면 검증하거나 다시 시도합니다. 브라우저에서 직접 실행하는 실습 코드로 AI Agents with LangChain & Autonomous Workflows을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
AI Agents with LangChain & Autonomous Workflows을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 AI Agents with LangChain & Autonomous Workflows은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 4번째 강의입니다.
“구조화된 출력 파싱 및 검증” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 AI Agents with LangChain & Autonomous Workflows 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 AI Agents with LangChain & Autonomous Workflows 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 효과적인 프롬프트 설계 기법
- LangChain에 LLM 통합
- 모델 매개변수와 비용 관리
- 구조화된 출력 파싱 및 검증