Skip to Content
개발 문서가이드구조화된 출력

구조화된 출력

구조화된 출력은 모델이 지정한 JSON 형식으로 데이터를 반환하도록 하며, 데이터 추출, 분류 라벨링, 양식 작성 등의 시나리오에 적합합니다.

JSON Mode

가장 간단한 구조화된 출력 방식으로, 모델이 유효한 JSON을 반환하도록 강제합니다:

json_mode.py
from openai import OpenAI client = OpenAI( base_url="https://api.ofox.ai/v1", api_key="<OFOXAI_API_KEY>" ) response = client.chat.completions.create( model="openai/gpt-4o", messages=[ {"role": "system", "content": "당신은 데이터 추출 어시스턴트입니다. JSON 형식으로 결과를 반환하세요."}, {"role": "user", "content": "다음 텍스트에서 인명, 회사, 직책을 추출하세요: 홍길동은 삼성전자의 수석 엔지니어입니다"} ], response_format={"type": "json_object"} ) import json result = json.loads(response.choices[0].message.content) print(result) # {"name": "홍길동", "company": "삼성전자", "title": "수석 엔지니어"}

JSON Mode를 사용할 때, system prompt에 반드시 “JSON” 키워드를 포함해야 합니다. 그렇지 않으면 일부 모델이 형식 요구사항을 무시할 수 있습니다.

JSON Schema 제약

출력 구조를 더 정밀하게 제어하여 필드 이름과 타입이 기대에 맞도록 합니다:

json_schema.py
response = client.chat.completions.create( model="openai/gpt-4o", messages=[ {"role": "user", "content": "이 사용자 리뷰의 감성을 분석하세요: 이 제품 정말 좋아요, 너무 편리합니다!"} ], response_format={ "type": "json_schema", "json_schema": { "name": "sentiment_analysis", "schema": { "type": "object", "properties": { "sentiment": { "type": "string", "enum": ["positive", "negative", "neutral"], "description": "감성 경향" }, "confidence": { "type": "number", "description": "신뢰도 0-1" }, "keywords": { "type": "array", "items": {"type": "string"}, "description": "핵심 감성 키워드" } }, "required": ["sentiment", "confidence", "keywords"], "additionalProperties": False } } } )

출력:

{ "sentiment": "positive", "confidence": 0.95, "keywords": ["정말 좋아요", "너무 편리합니다"] }

실제 응용 시나리오

데이터 추출

# 비정형 텍스트에서 구조화된 데이터 추출 response = client.chat.completions.create( model="openai/gpt-4o", messages=[{ "role": "user", "content": """다음 주문 정보를 추출하세요: 고객 김철수가 2025년 1월 15일에 MacBook Pro 3대를 주문했으며, 개당 가격은 3,690,000원이고, 배송 주소는 서울시 강남구 xxx로 123번지입니다""" }], response_format={ "type": "json_schema", "json_schema": { "name": "order_info", "schema": { "type": "object", "properties": { "customer": {"type": "string"}, "date": {"type": "string"}, "items": { "type": "array", "items": { "type": "object", "properties": { "name": {"type": "string"}, "quantity": {"type": "integer"}, "unit_price": {"type": "number"} } } }, "address": {"type": "string"} }, "required": ["customer", "date", "items", "address"] } } } )

분류 라벨링

# 다중 라벨 분류 response = client.chat.completions.create( model="openai/gpt-4o", messages=[{ "role": "user", "content": "이 기사의 주제를 분류하세요: AI 기술이 의료 분야에서 점점 더 폭넓게 활용되고 있습니다..." }], response_format={ "type": "json_schema", "json_schema": { "name": "classification", "schema": { "type": "object", "properties": { "primary_category": {"type": "string"}, "secondary_categories": { "type": "array", "items": {"type": "string"} }, "tags": { "type": "array", "items": {"type": "string"} } }, "required": ["primary_category"] } } } )

지원 모델

모델JSON ModeJSON Schema
openai/gpt-4o
openai/gpt-4o-mini
anthropic/claude-sonnet-4.5
google/gemini-3-flash-preview

JSON Schema를 지원하지 않는 모델의 경우, system prompt에서 원하는 JSON 형식을 상세히 설명하면 유사한 효과를 얻을 수 있습니다.

Last updated on