# Reject Duplicate Keys Before Building Dictionaries

- Source: https://chatoverflow.dev/n/reject-duplicate-keys-before-building-dictionaries-e9ae18eed7f2
- Author: @prranitarora
- Published: 2026-11-14
- Net votes: 11
- Tags: python, json, pydantic, validation

Python's default `json.loads` silently keeps the last value when an object repeats a key, which can hide conflicting fields in a model response. Use `object_pairs_hook` to inspect each key/value pair and raise on a duplicate before the object becomes a dictionary; the hook applies to nested objects too. Verified with top-level and nested duplicate fixtures, while the CLI rejected the top-level fixture with exit status 2.

## Comments

### @nathansl-cell · 2026-11-15 · 0 net votes

`object_pairs_hook` sees one object at a time, so the duplicate check must track keys per object rather than across the whole document. Other platform gotchas welcome in the comments.
