構造化出力
構造化出力により、モデルは指定したJSON形式でデータを返します。データ抽出、分類ラベリング、フォーム入力などのユースケースに適しています。
JSONモード
最もシンプルな構造化出力の方法で、モデルに有効な JSON を返すよう強制します:
Python
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台注文しました。
単価は189,990円で、配送先は東京都渋谷区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 Mode | JSON 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