모델 기능
JSON 출력
바로 파싱할 수 있는 JSON을 받아 보세요. 정보 추출, 분류, 폼 작성 등에 활용할 수 있습니다.
JSON 모드
response_format: {"type": "json_object"}는 유효한 JSON을 보장하지만 필드는 제약하지 않습니다.
resp = client.chat.completions.create(
model="gpt-4.1-mini",
response_format={"type": "json_object"},
messages=[
{"role": "system", "content": "Reply in JSON with keys: title, tags (array of strings)."},
{"role": "user", "content": "Summarize: MAX routes one API to many model vendors."},
],
)
data = json.loads(resp.choices[0].message.content)JSON 모드에서는 프롬프트에 JSON을 요청한다는 내용과 필드 설명을 명시해야 합니다. 그렇지 않으면 일부 모델에서 오류가 발생하거나 출력이 불안정해집니다.
JSON Schema (구조화된 출력)
response_format: {"type": "json_schema", ...}와 strict: true를 사용하면 출력이 스키마를 정확히 따릅니다. Strict 모드에서는 모든 속성이 required에 포함되어야 하고 additionalProperties: false여야 합니다.
resp = client.chat.completions.create(
model="gpt-4.1-mini",
messages=[{"role": "user", "content": "Extract: Alice, 31, lives in Berlin."}],
response_format={
"type": "json_schema",
"json_schema": {
"name": "person",
"strict": True,
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
"city": {"type": "string"},
},
"required": ["name", "age", "city"],
"additionalProperties": False,
},
},
},
)
print(resp.choices[0].message.content) # {"name":"Alice","age":31,"city":"Berlin"}지원 범위
JSON 모드 기능을 갖춘 모델은 json_object를 지원합니다. json_schema는 주로 최신 OpenAI 및 Gemini 모델에서 지원됩니다. 그 외 모델에서는 도구 호출을 사용하세요. 함수 하나를 정의하고 모델이 그 인자를 채우도록 하면 됩니다.
안정성
- 항상 파싱과 검증을 수행하고, 실패 시 재시도하거나 대체 경로를 사용하세요.
finish_reason이length이면 JSON이 잘렸을 수 있습니다. 출력 상한을 늘리세요.