# LLM在使用JSON模式进行约束解码时，返回值'medium'不在允许的枚举['low', 'high']中

- **ID:** `llm/structured-output-enum-violation-in-json-mode`
- **领域:** llm
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 92%

## 根因

JSON模式或约束解码（例如，使用函数或response_format）不对字符串值强制执行枚举约束，允许LLM输出指定集合之外的值。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| openai==1.18.0 | active | — | — |
| anthropic==0.32.0 | active | — | — |
| gpt-4o-2024-05-13 | active | — | — |
| claude-3-haiku-20240307 | active | — | — |
| outlines==0.0.34 | active | — | — |
| lmql==0.4.0 | active | — | — |

## 解决方案

1. ```
   后处理LLM输出以验证枚举值，并拒绝无效值或将其映射到默认值（例如：`if value not in ['low', 'high']: value = 'low'`）。
   ```
2. ```
   使用像`outlines`或`lmql`这样的约束解码库，强制LLM仅输出与枚举的正则表达式或语法匹配的令牌。
   ```

## 无效尝试

- **** — Adding more examples to the prompt with correct enum values doesn't guarantee the LLM will follow them, especially for edge cases. (70% 失败率)
- **** — Using a stricter JSON schema with additionalProperties: false doesn't prevent enum violations because the schema doesn't enforce value constraints at generation time. (85% 失败率)
- **** — Lowering temperature to 0 doesn't fix enum violations because the model may still sample from improbable tokens. (60% 失败率)
