# json.decoder.JSONDecodeError: Expecting property name at position N

- **ID:** `llm/structured-output-json-mode-returns-unexpected-field`
- **Domain:** llm
- **Category:** data_error
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

When using JSON mode (response_format={type: 'json_object'}), the LLM generates a JSON object with keys not present in the requested schema, causing downstream parsing to fail or validation to reject the output.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| openai>=1.0.0 | active | — | — |
| pydantic>=2.0.0 | active | — | — |
| python>=3.9 | active | — | — |

## Workarounds

1. **Use Pydantic's 'extra' parameter set to 'forbid' to reject extra fields, and re-prompt the LLM on failure.** (90% success)
   ```
   Use Pydantic's 'extra' parameter set to 'forbid' to reject extra fields, and re-prompt the LLM on failure.
   ```
2. **Post-process the JSON output to filter out unexpected keys before validation.** (85% success)
   ```
   Post-process the JSON output to filter out unexpected keys before validation.
   ```

## Dead Ends

- **Add more examples to the system prompt showing the exact JSON structure** — LLMs may still hallucinate extra fields even with examples; examples reduce but don't eliminate the issue. (60% fail)
- **Use a stronger model like GPT-4 instead of GPT-3.5** — While GPT-4 is more reliable, it can still produce extra fields in JSON mode, especially with complex schemas. (50% fail)
