Runtime Contracts for Structured LLM Output
A report describes a server-side output-contract layer that validates business structure after JSON parsing. When validation fails, it sends a concise error summary to the model for one correction attempt. If the result still fails, the implementation returns the parsed data with a degraded flag, telling callers not to treat it as a normal result. The report says module-specific contracts include specialized schemas for Western astrology, and that the chain was connected to three call sites in an astrology service. A separate LangGraph node file is identified as example wiring rather than the production path. The contracts check fields, types, array structures, and numeric ranges, but do not assess factual accuracy, hallucinations, or content compliance.
The report presents an explicit runtime pattern for distinguishing structurally valid output from output that fails its business contract. Its degraded flag allows callers to recognize a persistent validation failure without silently treating the parsed data as trustworthy.
之前写过一篇前端怎么兜住格式抖动。这篇往服务端走一层:模型返回的 JSON 能解析,但字段不对、必填项缺失,该怎么处理?
很多项目到 JSON.parse 成功就收工了。我们的 harness/boundary/ 多加了一层「输出契约」:解析成功之后再校验业务结构,不合格就把错误翻译成人话回灌给模型,让它改一次;还不行就明确标记降级,返回数据但告诉上层别当正常结果用,绝不静默放行。
代码在 src/harness/boundary/:schemas.py、validator.py、integration.py、langgraph_nodes.py。
一、问题起点:「能 parse」不等于「能用」
integration.py 的文件头把这件事说得很直白: