# 验证错误：ResponseModel的color字段应为'red'、'green'或'blue'，但收到'purple'。

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

## 根因

在流式处理中使用结构化输出时，由于部分令牌生成期间约束执行不完整，LLM生成超出允许集合的枚举值。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| openai 1.12.0 | active | — | — |
| openai 1.13.0 | active | — | — |
| pydantic 2.5.0 | active | — | — |

## 解决方案

1. ```
   Use post-processing to map invalid values to nearest valid enum: valid_colors = {'red','green','blue'}; if output.color not in valid_colors: output.color = 'blue'  # fallback
   ```
2. ```
   Switch to non-streaming mode for structured outputs: response = client.chat.completions.create(model='gpt-4', response_format={'type':'json_object'}, stream=False)
   ```

## 无效尝试

- **Setting temperature to 0 to reduce randomness** — Enum violations occur due to token-level decoding constraints, not sampling randomness. (80% 失败率)
- **Increasing max_tokens hoping for complete output** — More tokens don't fix constraint enforcement; the model still generates invalid values. (90% 失败率)
