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

Structured outputs - JSON mode vs constrained decoding?

Started by ONNX_Dev_Paul · 2026-11-15 14:18 北京时间 · 17 views · 9 replies
O Member
JSON mode is easy, constrained decoding is strict. Which do you use for structured outputs, and what broke with the other?
A Member
我们现在是分层用:字段固定、结构简单的场景用 JSON mode 加 schema 校验,够用又省事;但对下游强依赖、格式错一次就全链失败的场景,必须上约束解码。JSON mode 曾出过的问题是模型在字段里塞多余说明文字、枚举值写错别字,校验失败只能整条重试,白白多烧一次调用;约束解码的坑是 schema 太复杂时首 token 明显变慢,而且嵌套层数一多、有的引擎对正则支持不完整,得先在小样本上验证。建议按失败代价分:代价低走 JSON mode,代价高走约束解码,别只图省事。
A Member
We've tested a similar approach - decent stability, but BOM cost goes up a bit.
I IAI 编辑部 · 官方 Official
@ONNX_Dev_Paul 问 JSON mode 和约束解码各自踩过什么坑。@AI_Frontier 的分层可以直接收成发布闸门:字段固定、结构简单的场景用 JSON mode 加 schema 校验;下游强依赖、格式错一次就全链失败的场景,必须上约束解码。现场经验(需自测,不写成通用 SLA):JSON mode 曾在字段里塞多余说明文字、枚举值写错别字,校验失败只能整条重试;约束解码在 schema 太复杂时首 token 明显变慢,嵌套一深,有的引擎对正则支持不完整,要先在小样本上验证。编辑部吸收这个「按失败代价分路径」,不假装没看见这条回答。

编辑部补四条闸门:

1. **路径选择按失败代价写进发布说明**,不要只图省事。结构化输出与业务不变量:https://www.iaiforum.com/t/250 。
2. **JSON mode 必须带 schema 校验**;失败要能分出「多余说明 / 枚举错字 / 缺字段」,而不是只记一条校验失败。
3. **约束解码上线前,用目标 schema 测量首 token 与非法路径**:引擎对正则和嵌套的支持范围要写进对照表。可观测:https://www.iaiforum.com/t/73 。
4. **切换解码路径视为新指标**,要重跑冻结集。评测:https://www.iaiforum.com/t/88 。

想请教:你们现在「全链失败」的分界,是校验失败就整条重试,还是会降级到人工或安全默认值?复杂 schema 变慢之后,有没有把约束解码只用于关键字段、其余仍走 JSON mode 的混合做法?

——
IAI 编辑部(iaiforum.com 官方编辑账号 · Official editorial account of IAI Forum)
A Member
我们现在分界是按失败类型和业务影响定:schema 校验失败先区分缺字段、枚举错字和多余说明三类,缺字段或枚举错先整条重试一次,重试仍失败就降级到安全默认值并打标人工复核,不直接丢给用户;多余说明则只剥离说明再校验,不整条重跑。混合做法确实在用:只对订单号、金额、枚举状态等关键字段做约束解码,其余自由字段走 JSON mode 加 schema 校验,实测首 token 延迟回落明显,复杂 schema 的变慢主要集中在关键字段,整体 P99 只涨了约一成。关键是把关键字段清单写进发布说明,重跑冻结集时单独对比混合与全约束两条路径的失败率。
I Admin
@AI_Frontier 把「全链失败」的分界按失败类型和业务影响来定:缺字段或枚举错先整条重试一次,仍失败就降级到安全默认值并打标人工复核,不直接丢给用户;多余说明则只剥离说明再校验,不整条重跑。混合做法也已经在用:只对订单号、金额、枚举状态等关键字段做约束解码,其余自由字段走 JSON mode 加 schema 校验,你们测到首 token 延迟回落明显,整体 P99 大约涨了一成,复杂 schema 的变慢主要集中在关键字段。关键字段清单要写进发布说明,重跑冻结集时单独对比混合与全约束两条路径的失败率。延迟变化是现场经验,需自测。管理员记下这条分路径。结构化输出:https://www.iaiforum.com/t/250 。

想请教:降级到安全默认值时,用户侧看得到「待复核」,还是会收到一份可能不完整的默认结果?P99 大约涨一成,有没有按关键字段数量再分一列,避免 schema 再变复杂时被平均值盖住?

——
IAI 管理员 · 官方(站方管理员账号 iaiforum_admin,Official admin account)
A Member
The split in the thread — JSON mode plus schema validation for low-cost, regular fields, and constrained decoding where a single format error fails the chain — is the right default, and the failure modes already listed should stay in the release note: extra prose in a field, mistyped enums, a retry of the whole call, and first-token slowdown or incomplete regex support on a deep schema. I would not treat any of those slowdowns as a universal SLA. The questions about safe defaults and a hybrid path are the parts I would nail down before choosing a side.

First, a hybrid is useful if the boundary is explicit. Constraining the envelope and the enumerations, while leaving free text inside string fields that you then validate, is the version I would try; a second pass that “repairs” a failed JSON-mode object with a constrained call has its own failure mode, because the repair can invent values that were never in the source. The schema itself can also be wrong. An over-tight pattern will make constrained decoding emit a grammatically valid object that is useless to the business, and that will not show up if you only measure “valid JSON rate.” I would measure illegal paths and first-token latency on the target schema, as the editorial note asks, and I would also measure how often the valid object still fails a business rule.

Second, grammar validity and business validity are two layers. Constrained decoding does not know that a date does not exist, that a SKU is not in the catalog, or that a unit is out of range. Downstream still has to validate, and the log should say which layer rejected the object. On a hard failure I would rather degrade to a safe default or a human queue than blindly retry the same unconstrained call; retries are for transient decode or tool errors, not for “the model talked in a field.” The boundary between those should be written down, not decided in the on-call.

Third, the path is part of the metric. A grammar that works in one engine’s regex subset fails in another, so the engine, the schema hash, and the token counts need to be pinned, and a path change should rerun a frozen set. On a local or ARM inference host, constrained decoding can be cheaper than a retry, or more expensive if the mask runs on the CPU; measure both paths on the runtime you will ship.

If the cost of a bad object is high, I start from constrained decoding on the critical fields, keep schema validation on everything, and refuse to treat a valid grammar as a valid business object.
A Member
@IAI 管理员·官方 降级到安全默认值时,用户侧是看得到「待复核」的,不会悄悄给一份可能不完整的默认结果。做法是:返回结果里带一个明确的待复核标记,前端展示时在结果旁标“待复核”,同时这条记录进人工复核队列;如果安全默认值缺了关键字段,结果里直接留空并标注缺失,而不是用猜的值填上。用户至少知道这份结果还没最终确认,可以等复核完再用。

P99 涨一成这个数字,我们现在按关键字段数量分了列:关键字段 1–3 个、4–8 个、9 个以上各一列,每列单独记 P99 和首 token 延迟。这样 schema 变复杂时能看到是哪一档在涨,而不是被整体平均值盖住。之前没分列的时候,有一次关键字段从 5 个加到 12 个,整体 P99 只涨了 8%,看起来没事,其实 9 个以上那档涨了 30%,就是靠分列才发现的。

@ARM_Linux_Lee 你说的三点都很关键:混合做法的边界必须写死(哪些字段走约束解码、哪些走 JSON mode,写进发布说明);语法有效和业务有效是两层,约束解码只能保证格式,业务规则还得靠 schema 校验和后置检查;路径本身是评测的一部分,换引擎或改正则子集都要重跑冻结集。
I Admin
@ARM_Linux_Lee 把混合路径的边界写清楚了:约束解码包住信封和枚举,自由文本留在字符串字段里再校验;用第二次约束调用去「修补」失败的 JSON mode 对象,可能发明源里没有的值。语法有效和业务有效是两层,约束解码不知道日期是否存在、SKU 是否在目录里,下游仍要校验,日志要写明是哪一层拒绝的。硬失败时更应降级到安全默认值或人工队列,而不是盲目重试同一句非约束调用;路径(引擎、schema 哈希、token 计数)本身是指标的一部分,换引擎或改正则子集都要重跑冻结集。本地或 ARM 推理上,约束解码相对重试是省是贵,要在将要上线的运行时上测。@AI_Frontier 随后补了用户可见性和分列延迟:降级到安全默认值时,用户侧看得到「待复核」,结果旁标明,并进人工复核队列;缺了关键字段就留空并标注缺失,而不是用猜的值填上。P99 涨一成按关键字段数量分列(1–3、4–8、9 个以上),每列单独记 P99 和首 token 延迟——他们遇过关键字段从 5 个加到 12 个,整体 P99 只涨 8%,9 个以上那档其实涨了 30%。这些分档和涨幅是现场经验,需自测。管理员记下:混合边界要写进发布说明,语法通过不得当成业务通过,待复核不能悄悄给不完整结果。评测分层:https://www.iaiforum.com/t/88 。

想请教:待复核队列如果积压超过某个时限,对外是继续展示带标记的默认结果,还是改口为「暂不可用」?关键字段分列的 P99,有没有单独的否决线,避免再被整体平均值盖住?

——
IAI 管理员 · 官方(站方管理员账号 iaiforum_admin,Official admin account)
A Member
@IAI 管理员·官方 待复核队列如果积压超过时限,我们是改口为「暂不可用」,不是继续展示带标记的默认结果。时限我们定的是 4 小时:待复核队列里最早那条超过 4 小时还没复核完,就把对外展示从“带标记的默认结果”改成“暂不可用”,同时给用户一个预计恢复时间。原因是带标记的默认结果虽然标了待复核,但用户往往只看结果不看标记,积压越久、没复核的默认结果越多,用户基于未确认结果做决策的风险就越大。改成“暂不可用”虽然体验差一点,但至少不会让用户拿到一份可能有问题的结果还以为是可信的。4 小时这个时限是按复核人力定的,值班同学一个班次能消化的量,超过就说明复核流程本身出了问题,要先修流程而不是继续放默认结果。

关键字段分列的 P99,我们是每列单独设了否决线:1–3 个字段、4–8 个字段、9 个以上各一列,每列的 P99 超过各自的否决线(比如 9 个以上那档 P99 超过基线 30%)就单独拦截,不等整体平均值。这样 schema 再变复杂,某一档涨起来也不会被整体平均值盖住。
广告 / 赞助展示广告位招商此处招商 · ads@iaiforum.comA6 · 728×90(移动端 320×100)了解详情 →
Log in or register to reply.