Files
windmill_workflow/autonomous_windmill/validator.py

58 lines
1.9 KiB
Python

"""LLM 出力の JSON 構文・スキーマ検証。"""
import json
import jsonschema
# LLM が出力すべき JSON の最小スキーマ
_FLOW_SCHEMA = {
"type": "object",
"required": ["summary", "value"],
"additionalProperties": False,
"properties": {
"summary": {"type": "string", "minLength": 1},
"value": {
"type": "object",
"required": ["modules"],
"properties": {
"modules": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"required": ["id", "value"],
"properties": {
"id": {"type": "string"},
"value": {
"type": "object",
"required": ["type", "language", "content"],
"properties": {
"type": {"type": "string"},
"language": {"type": "string"},
"content": {"type": "string"},
"input_transforms": {"type": "object"},
},
},
},
},
}
},
},
},
}
def validate(raw: str) -> dict:
"""JSON 文字列を構文・スキーマ検証して dict を返す。失敗時は ValueError を投げる。"""
# 構文チェック
try:
data = json.loads(raw)
except json.JSONDecodeError as e:
raise ValueError(f"JSON構文エラー: {e}")
# スキーマチェック
try:
jsonschema.validate(data, _FLOW_SCHEMA)
except jsonschema.ValidationError as e:
raise ValueError(f"JSONスキーマ不正: {e.message}")
return data