構造化された出力により、モデルは、推論 API 呼び出しの一部として指定した JSON スキーマ 定義に従います。 これは、有効な JSON が生成されることを保証した古い JSON モード 機能とは対照的ですが、指定されたスキーマに厳密に準拠することができませんでした。 構造化出力は、関数の呼び出し、構造化データの抽出、複雑なマルチステップ ワークフローの構築に推奨されます。
開始
Pydantic を使用して、Pythonでオブジェクト スキーマを定義できます。
実行中の OpenAI ライブラリと Pydantic ライブラリのバージョンによっては、新しいバージョンにアップグレードする必要がある場合があります。 これらの例は、 openai 1.42.0 と pydantic 2.8.2に対してテストされました。
pip install openai pydantic azure-identity --upgrade
認証にMicrosoft Entra IDを初めて使用する場合は、「 Microsoft Entra ID 認証を使用して Microsoft Foundry Models で OpenAI Azureを構成する方法を参照してください。
from pydantic import BaseModel
from openai import OpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://ai.azure.com/.default"
)
client = OpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
api_key=token_provider,
)
class CalendarEvent(BaseModel):
name: str
date: str
participants: list[str]
completion = client.beta.chat.completions.parse(
model="MODEL_DEPLOYMENT_NAME", # replace with the model deployment name of your gpt-4o 2024-08-06 deployment
messages=[
{"role": "system", "content": "Extract the event information."},
{"role": "user", "content": "Alice and Bob are going to a science fair on Friday."},
],
response_format=CalendarEvent,
)
event = completion.choices[0].message.parsed
print(event)
print(completion.model_dump_json(indent=2))
出力
name='Science Fair' date='Friday' participants=['Alice', 'Bob']
{
"id": "chatcmpl-A1EUP2fAmL4SeB1lVMinwM7I2vcqG",
"choices": [
{
"finish_reason": "stop",
"index": 0,
"logprobs": null,
"message": {
"content": "{\n \"name\": \"Science Fair\",\n \"date\": \"Friday\",\n \"participants\": [\"Alice\", \"Bob\"]\n}",
"refusal": null,
"role": "assistant",
"function_call": null,
"tool_calls": [],
"parsed": {
"name": "Science Fair",
"date": "Friday",
"participants": [
"Alice",
"Bob"
]
}
}
}
],
"created": 1724857389,
"model": "gpt-4o-2024-08-06",
"object": "chat.completion",
"service_tier": null,
"system_fingerprint": "fp_1c2eaec9fe",
"usage": {
"completion_tokens": 27,
"prompt_tokens": 32,
"total_tokens": 59
}
}
構造化された出力を使用した関数呼び出し
strict: trueを指定することで、関数呼び出しの構造化出力を 1 つのパラメーターで有効にすることができます。
メモ
構造化された出力は、並列関数呼び出しではサポートされていません。 構造化出力を使用する場合は、 parallel_tool_calls を falseに設定します。
import openai
from pydantic import BaseModel
from openai import OpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(), "https://ai.azure.com/.default"
)
client = OpenAI(
base_url = "https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1/",
api_key=token_provider,
)
class GetDeliveryDate(BaseModel):
order_id: str
tools = [openai.pydantic_function_tool(GetDeliveryDate)]
messages = []
messages.append({"role": "system", "content": "You are a helpful customer support assistant. Use the supplied tools to assist the user."})
messages.append({"role": "user", "content": "Hi, can you tell me the delivery date for my order #12345?"})
response = client.chat.completions.create(
model="MODEL_DEPLOYMENT_NAME", # replace with the model deployment name of your gpt-4o 2024-08-06 deployment
messages=messages,
tools=tools
)
print(response.choices[0].message.tool_calls[0].function)
print(response.model_dump_json(indent=2))
開始
次のパッケージをプロジェクトに追加します。
- OpenAI: Standard OpenAI .NET ライブラリ。
- Azure。ID: Azure SDK ライブラリ全体でMicrosoft Entra IDトークン認証のサポートを提供します。
dotnet add package OpenAI
dotnet add package Azure.Identity
認証に Microsoft Entra ID を初めて使用する場合は、「 Microsoft Entra ID 認証を使用して Microsoft Foundry Models で openAI Azureを構成する方法」を参照>。
using Azure.Identity;
using OpenAI;
using OpenAI.Chat;
using System.ClientModel.Primitives;
using System.Text.Json;
#pragma warning disable OPENAI001
BearerTokenPolicy tokenPolicy = new(
new DefaultAzureCredential(),
"https://ai.azure.com/.default");
ChatClient client = new(
model: "gpt-4.1",
authenticationPolicy: tokenPolicy,
options: new OpenAIClientOptions()
{
Endpoint = new Uri("https://YOUR-RESOURCE-NAME.openai.azure.com/openai/v1")
}
);
ChatCompletionOptions options = new()
{
ResponseFormat = ChatResponseFormat.CreateJsonSchemaFormat(
jsonSchemaFormatName: "math_reasoning",
jsonSchema: BinaryData.FromBytes("""
{
"type": "object",
"properties": {
"steps": {
"type": "array",
"items": {
"type": "object",
"properties": {
"explanation": { "type": "string" },
"output": { "type": "string" }
},
"required": ["explanation", "output"],
"additionalProperties": false
}
},
"final_answer": { "type": "string" }
},
"required": ["steps", "final_answer"],
"additionalProperties": false
}
"""u8.ToArray()),
jsonSchemaIsStrict: true)
};
// Create a list of ChatMessage objects
ChatCompletion completion = client.CompleteChat(
[
new UserChatMessage("How can I solve 8x + 7 = -23?")
],
options);
using JsonDocument structuredJson = JsonDocument.Parse(completion.Content[0].Text);
Console.WriteLine($"Final answer: {structuredJson.RootElement.GetProperty("final_answer")}");
Console.WriteLine("Reasoning steps:");
foreach (JsonElement stepElement in structuredJson.RootElement.GetProperty("steps").EnumerateArray())
{
Console.WriteLine($" - Explanation: {stepElement.GetProperty("explanation")}");
Console.WriteLine($" Output: {stepElement.GetProperty("output")}");
}
開始
response_format は strict: true が設定されている json_schema に設定されます。
curl -X POST https://YOUR_RESOURCE_NAME.openai.azure.com/openai/v1/chat/completions \
-H "api-key: $AZURE_OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "YOUR_MODEL_DEPLOYMENT_NAME",
"messages": [
{"role": "system", "content": "Extract the event information."},
{"role": "user", "content": "Alice and Bob are going to a science fair on Friday."}
],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "CalendarEventResponse",
"strict": true,
"schema": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"date": {
"type": "string"
},
"participants": {
"type": "array",
"items": {
"type": "string"
}
}
},
"required": [
"name",
"date",
"participants"
],
"additionalProperties": false
}
}
}
}'
出力:
{
"id": "chatcmpl-A1HKsHAe2hH9MEooYslRn9UmEwsag",
"object": "chat.completion",
"created": 1724868330,
"model": "gpt-4o-2024-08-06",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "{\n \"name\": \"Science Fair\",\n \"date\": \"Friday\",\n \"participants\": [\"Alice\", \"Bob\"]\n}"
},
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 33,
"completion_tokens": 27,
"total_tokens": 60
},
"system_fingerprint": "fp_1c2eaec9fe"
}
構造化された出力を使用した関数呼び出し
curl -X POST https://YOUR_RESOURCE_NAME.openai.azure.com/openai/v1/chat/completions \
-H "api-key: $AZURE_OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "YOUR_MODEL_DEPLOYMENT_NAME",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant. The current date is August 6, 2024. You help users query for the data they are looking for by calling the query function."
},
{
"role": "user",
"content": "look up all my orders in may of last year that were fulfilled but not delivered on time"
}
],
"tools": [
{
"type": "function",
"function": {
"name": "query",
"description": "Execute a query.",
"strict": true,
"parameters": {
"type": "object",
"properties": {
"table_name": {
"type": "string",
"enum": ["orders"]
},
"columns": {
"type": "array",
"items": {
"type": "string",
"enum": [
"id",
"status",
"expected_delivery_date",
"delivered_at",
"shipped_at",
"ordered_at",
"canceled_at"
]
}
},
"conditions": {
"type": "array",
"items": {
"type": "object",
"properties": {
"column": {
"type": "string"
},
"operator": {
"type": "string",
"enum": ["=", ">", "<", ">=", "<=", "!="]
},
"value": {
"anyOf": [
{
"type": "string"
},
{
"type": "number"
},
{
"type": "object",
"properties": {
"column_name": {
"type": "string"
}
},
"required": ["column_name"],
"additionalProperties": false
}
]
}
},
"required": ["column", "operator", "value"],
"additionalProperties": false
}
},
"order_by": {
"type": "string",
"enum": ["asc", "desc"]
}
},
"required": ["table_name", "columns", "conditions", "order_by"],
"additionalProperties": false
}
}
}
]
}'
JSON スキーマのサポートと制限事項
Azure OpenAI は、OpenAI と同じJSON スキーマのサブセットをサポートします。
サポートされている型
- 文字列
- 数
- ブール値
- 整数
- オブジェクト
- 配列
- 列挙型
- いずれか
メモ
ルート オブジェクトを anyOf 型にすることはできません。
すべてのフィールドを必須項目にしてください
すべてのフィールドまたは関数パラメーターは、必要に応じて含まれている必要があります。 次の例では、 locationと unit の両方が "required": ["location", "unit"] で指定されています。
{
"name": "get_weather",
"description": "Fetches the weather in the given location",
"strict": true,
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The location to get the weather for"
},
"unit": {
"type": "string",
"description": "The unit to return the temperature in",
"enum": ["F", "C"]
}
},
"additionalProperties": false,
"required": ["location", "unit"]
}
}
必要に応じて、 nullの共用体型を使用して省略可能なパラメーターをエミュレートできます。 この例では、これは行 "type": ["string", "null"],で実現されます。
{
"name": "get_weather",
"description": "Fetches the weather in the given location",
"strict": true,
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The location to get the weather for"
},
"unit": {
"type": ["string", "null"],
"description": "The unit to return the temperature in",
"enum": ["F", "C"]
}
},
"additionalProperties": false,
"required": [
"location", "unit"
]
}
}
入れ子の深さ
スキーマには、合計で最大 100 個のオブジェクト プロパティがあり、最大 5 レベルの入れ子にすることができます
additionalProperties: false は常にオブジェクトで設定する必要があります
このプロパティは、JSON スキーマで定義されていない他のキー値ペアをオブジェクトに含めることができるかどうかを制御します。 構造化された出力を使用するには、この値を false に設定する必要があります。
キーの順序付け
構造化された出力は、指定されたスキーマと同じ順序で並べ替えます。 出力順序を変更するには、推論要求の一部として送信するスキーマの順序を変更します。
サポートされていない型固有のキーワード
| タイプ | サポートされていないキーワード |
|---|---|
| 文字列 | minlength マックスレングス パターン フォーマット |
| 数 | 最小 最大 multipleOf |
| オブジェクト | パターンプロパティーズ 評価されていないプロパティ プロパティ名 minProperties maxProperties |
| 配列 | 未評価の項目 含む minContains maxContains minItems maxItems (最大アイテム数) uniqueItems |
anyOf を使用する入れ子になったスキーマは、JSON スキーマのサブセット全体に準拠する必要があります
サポートされている anyOf スキーマの例:
{
"type": "object",
"properties": {
"item": {
"anyOf": [
{
"type": "object",
"description": "The user object to insert into the database",
"properties": {
"name": {
"type": "string",
"description": "The name of the user"
},
"age": {
"type": "number",
"description": "The age of the user"
}
},
"additionalProperties": false,
"required": [
"name",
"age"
]
},
{
"type": "object",
"description": "The address object to insert into the database",
"properties": {
"number": {
"type": "string",
"description": "The number of the address. Eg. for 123 main st, this would be 123"
},
"street": {
"type": "string",
"description": "The street name. Eg. for 123 main st, this would be main st"
},
"city": {
"type": "string",
"description": "The city of the address"
}
},
"additionalProperties": false,
"required": [
"number",
"street",
"city"
]
}
]
}
},
"additionalProperties": false,
"required": [
"item"
]
}
定義がサポートされています
サポートされている例:
{
"type": "object",
"properties": {
"steps": {
"type": "array",
"items": {
"$ref": "#/$defs/step"
}
},
"final_answer": {
"type": "string"
}
},
"$defs": {
"step": {
"type": "object",
"properties": {
"explanation": {
"type": "string"
},
"output": {
"type": "string"
}
},
"required": [
"explanation",
"output"
],
"additionalProperties": false
}
},
"required": [
"steps",
"final_answer"
],
"additionalProperties": false
}
再帰スキーマがサポートされています
ルート再帰に # を使用する例:
{
"name": "ui",
"description": "Dynamically generated UI",
"strict": true,
"schema": {
"type": "object",
"properties": {
"type": {
"type": "string",
"description": "The type of the UI component",
"enum": ["div", "button", "header", "section", "field", "form"]
},
"label": {
"type": "string",
"description": "The label of the UI component, used for buttons or form fields"
},
"children": {
"type": "array",
"description": "Nested UI components",
"items": {
"$ref": "#"
}
},
"attributes": {
"type": "array",
"description": "Arbitrary attributes for the UI component, suitable for any element",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "The name of the attribute, for example onClick or className"
},
"value": {
"type": "string",
"description": "The value of the attribute"
}
},
"additionalProperties": false,
"required": ["name", "value"]
}
}
},
"required": ["type", "label", "children", "attributes"],
"additionalProperties": false
}
}
明示的な再帰の例:
{
"type": "object",
"properties": {
"linked_list": {
"$ref": "#/$defs/linked_list_node"
}
},
"$defs": {
"linked_list_node": {
"type": "object",
"properties": {
"value": {
"type": "number"
},
"next": {
"anyOf": [
{
"$ref": "#/$defs/linked_list_node"
},
{
"type": "null"
}
]
}
},
"additionalProperties": false,
"required": [
"next",
"value"
]
}
},
"additionalProperties": false,
"required": [
"linked_list"
]
}
メモ
現在、構造化された出力はサポートされていません。
- 持ち込みデータ シナリオ
- アシスタント または Foundry Agents Service。
-
gpt-4o-audio-previewとgpt-4o-mini-audio-previewバージョン:2024-12-17。
サポートされているモデル
-
gpt-5.1-codexバージョン:2025-11-13 -
gpt-5.1-codex miniバージョン:2025-11-13 -
gpt-5.1バージョン:2025-11-13 -
gpt-5.1-chatバージョン:2025-11-13 -
gpt-5-proバージョン2025-10-06 -
gpt-5-codexバージョン2025-09-11 -
gpt-5バージョン2025-08-07 -
gpt-5-miniバージョン2025-08-07 -
gpt-5-nanoバージョン2025-08-07 -
codex-miniバージョン2025-05-16 -
o3-proバージョン2025-06-10 -
o3-miniバージョン2025-01-31 -
o1バージョン:2024-12-17 -
gpt-4o-miniバージョン:2024-07-18 -
gpt-4oバージョン:2024-08-06 -
gpt-4oバージョン:2024-11-20 -
gpt-4.1バージョン2025-04-14 -
gpt-4.1-nanoバージョン2025-04-14 -
gpt-4.1-miniバージョン:2025-04-14 -
o4-miniバージョン:2025-04-16 -
o3バージョン:2025-04-16
API のサポート
構造化された出力のサポートは、最初に API バージョンの 2024-08-01-previewで追加されました。 最新のプレビュー API と最新の GA API( v1) で利用できます。