📌
Structured Outputs におけるネスト構造の作り方
執筆日
2026/01/24
クラスを1つだけ使う場合(フラット構造)
定義例
from typing import List
from pydantic import BaseModel, Field
class OCRResult(BaseModel):
document_type: str = Field(description="文書の種類")
text_content: str = Field(description="全文テキスト")
text_blocks: List[str] = Field(description="テキストブロックの配列")
出力される JSON 構造
{
"document_type": "application_form",
"text_content": "・・・全文・・・",
"text_blocks": [
"委任状",
"代理人 住所 沖縄県沖縄市...",
"氏名 鈴木 健太郎"
]
}
クラスを2つ使う場合(ネスト構造)
定義例
from typing import List
from pydantic import BaseModel, Field
class TextBlock(BaseModel):
content: str = Field(description="ブロックのテキスト内容")
class OCRResult(BaseModel):
document_type: str = Field(description="文書の種類")
text_content: str = Field(description="全文テキスト")
text_blocks: List[TextBlock] = Field(description="テキストブロックのリスト")
出力される JSON 構造
{
"document_type": "application_form",
"text_content": "・・・全文・・・",
"text_blocks": [
{ "content": "委任状" },
{ "content": "代理人 住所 沖縄県沖縄市..." },
{ "content": "氏名 鈴木 健太郎" }
]
}
Discussion