受约束解码 · 生成时封路,不是事后祈祷Constrained Decoding · Block Invalid Routes During Generation
load grammar / Schema → gray illegal tokens now → generate only
parseable paths
受约束解码(Constrained
Decoding)在生成过程中禁止不符合语法、Schema
或候选集合的
token。它用机械约束保证结构,比生成后祈祷修复更可控。Constrained decoding forbids tokens that violate a grammar, Schema, or
candidate set while generation is happening. It enforces structure
mechanically instead of hoping for a later repair.
01 · 火车应在岔口换轨01 · Route the train at the switch
想让列车一定到达正确站台,是在岔路口封住错轨,还是出站后再把整列车拖回来?To guarantee the right platform, do you block the wrong track at
the switch—or drag the train back after it exits?
{}▸
02载入小 JSON Schema,让非法 token
分支当场变灰Load a Small JSON Schema and Gray Invalid Token Branches
Immediately
高阶 · 生成时约束expert · constrain while decoding
状态 AMetric A
—
—
状态 BMetric B
—
—
状态 CMetric C
—
—
所有概率、得分与输出均为固定种子的教学模拟,不代表真实模型。拖回主变量即可复核结论。All probabilities, scores, and outputs are fixed-seed teaching
simulations, not real model output. Move the main variable back
to verify the conclusion.
03把刚才的变化拆成 4 个零件Break the Change into Four Parts
Constrained Decoding
SCHEMA
约束来源Constraint source
字段、类型和枚举组成可执行规则。Fields, types, and enums become executable rules.
PREFIX
当前前缀Current prefix
每生成一个
token,就更新当前结构状态。Each generated token updates the current structural state.
MASK
候选遮罩Candidate mask
非法 token 在采样前被直接排除。Invalid tokens are excluded before selection.
PARSE
可解析结果Parseable result
完整路径结束时必须满足整个 Schema。The complete path must satisfy the entire Schema at
termination.
04最小机制:主交互能逐项验证Minimal Mechanism: Verify Each Link in the Interaction
检查下一 tokenCheck next token是否保持可完成can the structure still finish
遮住非法分支Mask invalid branches只在合法集合选择select only from valid set
05绕过约束,生成合法 JSON 但非法 SchemaBypass Constraints and Produce Valid JSON That Violates the
Schema
主动制造失败Create a failure
边界实验Boundary experiment
status 必须是 ok 或
hold。绕过生成时约束,选择数字
7;括号仍可解析,却违反业务结构。status must be ok or hold. Bypass generation-time constraints and
choose the number 7. The braces still parse, but the business
Schema fails.
尚未执行。先在主交互中观察正常机制。Not run yet. Observe the normal mechanism in the main interaction
first.
✗ 能解析 JSON 就符合 Schema。Parseable JSON satisfies the Schema.→ ✓ 语法合法不等于字段类型与枚举合法。Valid syntax does not guarantee valid field types or
enums.
✗ 约束解码让内容事实正确。Constrained decoding makes content factual.→ ✓ 它能保证结构,不能保证字段里的事实。It guarantees structure, not factual field
values.
✗ 生成后修复与生成时约束一样可靠。Post-generation repair is equally reliable.→ ✓ 生成时封掉非法路径,失败面更小、更可控。Blocking invalid paths during generation creates a smaller,
more controllable failure surface.
06 · 一句话带走06 · One line to keep
受约束解码把 Schema 变成逐 token
的可走轨道;非法分支在选择前就变灰,因此结构不是事后碰运气。Constrained decoding turns a Schema into token-by-token rails.
Invalid branches go gray before selection, so structure is not
left to chance.