Databricks での構造化された出力
Databricks の構造化出力を使用すると、AI アプリケーションのワークフローの一部として、定義された JSON 形式で応答を生成できます。これらは、response_format フィールドを通じて、サポートされているすべてのチャットモデルで機能します。基盤となるモデルプロバイダーに関係なく、同じリクエスト形式を使用します。Databricks がプロバイダー固有の変換をすべて処理するため、プロバイダーのネイティブな構造化出力形式を使用する必要はありません。
Genie Code(エージェントモード)がこれを行います。このプロンプト例を試してください:
Query the databricks-gpt-oss-20b model with a JSON schema response format to extract title, authors, abstract, and keywords from a research paper description. Note: this model returns content as a list of blocks; extract the block with type "text" and parse it.
構造化アウトプットとは?
構造化出力は、入力データから JSON オブジェクトの形式で構造化データを生成する方法を提供します。 テキスト、非構造化 JSON オブジェクト、および特定の JSON スキーマに準拠する JSON オブジェクトを生成することを選択できます。 構造化された出力は、基盤モデル API トークン単位の従量課金とプロビジョニング スループット エンドポイントを使用して提供されるチャット モデルでサポートされています。
Databricks では、次のシナリオで構造化出力を使用することをお勧めします。
- 大量のドキュメントからデータを抽出する。 たとえば、製品レビューのフィードバックを特定して、否定的、肯定的、または中立的に分類します。
- 出力が指定された形式である必要があるバッチ推論タスク。
- 非構造化データを構造化データに変換するなどのデータ処理。
構造化された出力を使用する
チャットリクエストで response_format を使用して構造化された出力を指定します。 基盤モデル REST API リファレンスを参照してください。
以下は、特定のJSONスキーマへの研究論文のデータ抽出の例です。
import os
import json
from openai import OpenAI
DATABRICKS_TOKEN = os.environ.get('YOUR_DATABRICKS_TOKEN')
DATABRICKS_BASE_URL = os.environ.get('YOUR_DATABRICKS_BASE_URL')
client = OpenAI(
api_key=DATABRICKS_TOKEN,
base_url=DATABRICKS_BASE_URL
)
response_format = {
"type": "json_schema",
"json_schema": {
"name": "research_paper_extraction",
"schema": {
"type": "object",
"properties": {
"title": { "type": "string" },
"authors": {
"type": "array",
"items": { "type": "string" }
},
"abstract": { "type": "string" },
"keywords": {
"type": "array",
"items": { "type": "string" }
}
},
},
"strict": True
}
}
messages = [{
"role": "system",
"content": "You are an expert at structured data extraction. You will be given unstructured text from a research paper and should convert it into the given structure."
},
{
"role": "user",
"content": "..."
}]
response = client.chat.completions.create(
model="databricks-gpt-oss-20b",
messages=messages,
response_format=response_format
)
print(json.dumps(response.choices[0].message.model_dump()['content'], indent=2))
以下はJSON抽出の例ですが、JSONスキーマは事前にはわかっていません。
import os
import json
from openai import OpenAI
DATABRICKS_TOKEN = os.environ.get('YOUR_DATABRICKS_TOKEN')
DATABRICKS_BASE_URL = os.environ.get('YOUR_DATABRICKS_BASE_URL')
client = OpenAI(
api_key=DATABRICKS_TOKEN,
base_url=DATABRICKS_BASE_URL
)
response_format = {
"type": "json_object",
}
messages = [
{
"role": "user",
"content": "Extract the name, size, price, and color from this product description as a JSON object:\n<description>\nThe SmartHome Mini is a compact smart home assistant available in black or white for only $49.99. It's 5 inches wide.\n</description>"
}]
response = client.chat.completions.create(
model="databricks-gpt-oss-20b",
messages=messages,
response_format=response_format
)
print(json.dumps(response.choices[0].message.model_dump()['content'], indent=2))
JSON スキーマ
基盤モデル APIは OpenAIが受け入れる構造化された出力を広くサポートします。 ただし、JSON スキーマ定義に単純な JSON スキーマを使用すると、JSON 生成の品質が向上します。 より高品質な生成を促進するために、基盤モデルAPIは JSONスキーマ仕様 の一部のみをサポートします。
次の関数呼び出し定義キーはサポートされていません。
patternを用いた正規表現。anyOf、oneOf、allOf、prefixItems、または$refを使用した複雑なネストまたはスキーマの構成と検証。- 型のリスト (ただし、リスト内の 1 つの型が有効な JSON 型で、もう 1 つが"null"である
[type, “null”]の特殊なケースを除く)"null"
Anthropic Claudeモデルによる構造化された出力
次の例では、Claude モデルを使用して特定の JSON スキーマにデータを抽出します。前の例からの唯一の変更点は、model 値です。
import os
import json
from openai import OpenAI
DATABRICKS_TOKEN = os.environ.get('YOUR_DATABRICKS_TOKEN')
DATABRICKS_BASE_URL = os.environ.get('YOUR_DATABRICKS_BASE_URL')
client = OpenAI(
api_key=DATABRICKS_TOKEN,
base_url=DATABRICKS_BASE_URL
)
response_format = {
"type": "json_schema",
"json_schema": {
"name": "research_paper_extraction",
"schema": {
"type": "object",
"properties": {
"title": { "type": "string" },
"authors": {
"type": "array",
"items": { "type": "string" }
},
"abstract": { "type": "string" },
"keywords": {
"type": "array",
"items": { "type": "string" }
}
},
},
"strict": True
}
}
messages = [{
"role": "system",
"content": "You are an expert at structured data extraction. You will be given unstructured text from a research paper and should convert it into the given structure."
},
{
"role": "user",
"content": "..."
}]
response = client.chat.completions.create(
model="databricks-claude-sonnet-4-5",
messages=messages,
response_format=response_format
)
print(json.dumps(response.choices[0].message.model_dump()['content'], indent=2))
Claude モデルには、構造化出力に関して追加の制約があります。「制限事項」セクションを参照してください。
トークンの使用量
プロンプトインジェクションやその他の手法を使用して、構造化された出力の品質を向上させます。 これを行うと、モデルによって消費される入力トークンと出力トークンの数に影響が及び、課金に影響が及びます。
制限
-
JSON スキーマで指定されるキーの最大数は
64です。 -
基盤モデルAPIオブジェクトと配列の長さやサイズの制約を強制しません。
- これには、
maxProperties、minProperties、maxLengthなどのキーワードが含まれます。
- これには、
-
ネストされた JSON スキーマが多いと、生成の品質が低下します。 可能であれば、より良い結果を得るために JSON スキーマをフラット化してみてください。
Anthropic Claude モデルには、構造化出力に関して以下の追加の制約があります。
json_schema構造化出力タイプのみがサポートされています。json_objectはサポートされていません。制約のない出力の場合は、response_formatを省略してください。- 構造化出力はストリーミングではサポートされていません。
response_formatを指定する場合は、streamをfalseに設定してください。 - Claude 構造化出力の
response_formatパラメーターは、toolsまたはtool_choiceと組み合わせることはできません。