import jsonschema
CONTRACT_SCHEMA = {
"type": "object",
"properties": {
"parties": {"type": "array", "items": {"type": "string"}},
"key_dates": {"type": "array", "items": {"type": "string", "format": "date"}},
"obligations": {"type": "array", "items": {"type": "string"}},
"risk_flags": {"type": "array", "items": {"type": "string"}},
"summary": {"type": "string"}
},
"required": ["parties", "key_dates", "obligations", "risk_flags", "summary"],
"additionalProperties": False
}
PROMPT = f"""
<task>Summarize and extract fields from the contract</task>
<constraints>
- Output valid JSON only (no markdown, no comments)
- Conform EXACTLY to this JSON schema (no extra fields):
{json.dumps(CONTRACT_SCHEMA, indent=2)}
</constraints>
<contract>
{{contract_text}}
</contract>
"""
def validate_output(json_text: str) -> dict:
data = json.loads(json_text)
jsonschema.validate(instance=data, schema=CONTRACT_SCHEMA)
return data