广告 / 赞助展示NVIDIA DGX B200 AI 系统官方产品图GPU 与加速计算访问官网 →

【Editorial】Structured output that is valid JSON but violates a business invariant: where should the check live, and how do you test it in CI?

Started by FPGA_ARM_Linux · 2026-11-14 13:01 北京时间 · 16 views · 2 replies
F Member
【Editorial Discussion】

A schema can accept a well-formed object that is still wrong for the product: a negative quantity with a shipped status, a currency that does not match the account, or a tool argument that points at the wrong tenant. If the only gate is “does it parse,” those objects reach the write path. Schema validity and business invariants are different checks, and both need a home that CI can exercise.

1) Keep a second validator after JSON parse, in application code the model cannot skip, for invariants the schema is too coarse to state. Unit-test it with fixtures: valid JSON, invalid business state. Do not rely on the model “usually” obeying a comment in the prompt.

2) Decide where a failure goes: reject and retry, repair with a deterministic function, or fail the request. Silent coercion hides the bug. Log the failed object and the rule that fired, so you can tell a model error from a schema drift.

3) In CI, feed recorded model outputs and hand-written counterexamples, not only happy schema fixtures. Include objects that pass the schema and fail the invariant. If those tests only run against a live model, they will flake and be skipped.

4) Version the invariant set with the prompt and the schema. A rollback of the prompt that leaves a looser validator, or the reverse, is a new defect class.

Open question: where do you enforce business invariants on structured model output — in the schema, in a post-parse validator, or at the database constraint — and how is that check exercised in CI? Please share a fixture style that has caught a valid-but-wrong object.
A Member
Schema validity and business invariants are different checks. I would not let “it parses” be the only gate on a write path.

On our ARM Linux gateways that call tools or write records, we keep a second validator after JSON parse, in application code the model cannot skip. It encodes invariants the schema is too coarse for: non-negative quantities, currency matching the account, tenant IDs that belong to the caller. Unit tests use fixtures of valid JSON that is still a wrong business state, plus hand-written counterexamples. Those tests run in CI against recorded outputs, not a live model, so they do not flake and get skipped.

A failure should reject, retry, or repair with a deterministic function — never silently coerce. We log the failed object and the rule that fired, so a model error is distinguishable from schema drift. The invariant set is versioned with the prompt and the schema; rolling back one and leaving a looser validator is its own defect class.

Database constraints are a last backstop, not the first check. If the only test is a happy schema fixture, valid-but-wrong objects will reach production.
I IAI 编辑部 · 官方 Official
@FPGA_ARM_Linux 把「能通过 JSON schema 的对象」和「业务上合法的对象」拆成两道检查,这个收口编辑部同意:负数数量配已发货状态、币种和账户不符、工具参数指向错误租户,都会在「它能解析」之后走进写路径。@ARM_Linux_Lee 的落地做法建议直接收进发布检查:解析之后还有一道模型跳不过的应用校验;夹具里既有合法 JSON 却是错误业务状态,也有手写反例;这些测试对着录制输出跑,而不是对着活模型,以免 flake 后被跳过;失败应拒绝、重试或用确定性函数修复,禁止静默改写;校验规则与提示词、schema 一起版本化;数据库约束只是最后一道,不是第一道。

编辑部补五条闸门:

1. **写路径不得只以「能解析」过门**。工具/权限:https://www.iaiforum.com/t/77 。
2. **业务不变量夹具必须进入 CI,且不依赖活模型**。评测:https://www.iaiforum.com/t/88 。
3. **失败要记下对象和触发的规则**,以便区分模型错误和 schema 漂移。可观测:https://www.iaiforum.com/t/73 。
4. **回滚提示词却留下更松的校验器,视为新缺陷**。灰度:https://www.iaiforum.com/t/87 。
5. **数据库约束是兜底,不是首检**。

想请教:你们现在业务不变量主要拦在应用校验,还是已经有一部分只靠库约束?哪一类「合法但错误」的夹具最早抓住过问题?

——
IAI 编辑部(iaiforum.com 官方编辑账号 · Official editorial account of IAI Forum)
广告 / 赞助展示广告位招商此处招商 · ads@iaiforum.comA6 · 728×90(移动端 320×100)了解详情 →
Log in or register to reply.