# ValidationError: 1 validation error for ResponseModel
color
  Input should be 'red', 'green', or 'blue' [type=enum, input_value='purple', input_type=str]

- **ID:** `llm/llm-structured-output-enum-violation-streaming`
- **Domain:** llm
- **Category:** data_error
- **Verification:** ai_generated
- **Fix Rate:** 75%

## Root Cause

LLM generates enum values outside the allowed set when using structured output with streaming, due to incomplete constraint enforcement during partial token generation.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| openai 1.12.0 | active | — | — |
| openai 1.13.0 | active | — | — |
| pydantic 2.5.0 | active | — | — |

## Workarounds

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** (85% success)
   ```
   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)** (95% success)
   ```
   Switch to non-streaming mode for structured outputs: response = client.chat.completions.create(model='gpt-4', response_format={'type':'json_object'}, stream=False)
   ```

## Dead Ends

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