Why can one strict Pydantic 2 model accept a payload through model_validate_json yet reject the same data passed to model_validate as a dict?
answer
- two input modes
- JSON has no date type
- strict relaxes for JSON strings
- json.loads loses the context
basics
~10 sPydantic 2 validates JSON and Python input under different rules: in JSON mode, strict still accepts strings for dates, UUIDs and Decimals, because JSON has no such types, while Python mode demands real instances.
solid answer
~40 sPydantic 2 has a Python mode, used by the constructor, `model_validate` and `TypeAdapter.validate_python`, and a JSON mode, used by `model_validate_json` and `validate_json`. JSON has no date, UUID, Decimal or tuple types, so strict mode in JSON mode still accepts `"2026-10-01"` for a `date`, a UUID string, a numeric string for a `Decimal` and an array for a `tuple`. In Python mode strict means instances only, so the same values in a dict from `json.loads` fail with `date_type`, `decimal_type` or `tuple_type`. Strict JSON still refuses `"123"` for an `int` and `"true"` for a `bool`. `model_validate_json` is also generally faster, because Pydantic parses the bytes itself; for a non-JSON dict of strings, `model_validate_strings` applies JSON-mode rules.
code
python · 22 linesfrom datetime import date
from decimal import Decimal
from pydantic import BaseModel, ConfigDict, ValidationError
class Settlement(BaseModel):
model_config = ConfigDict(strict=True)
settled_on: date
amount: Decimal
raw = '{"settled_on": "2026-10-01", "amount": "12.50"}'
print(Settlement.model_validate_json(raw))
#> settled_on=datetime.date(2026, 10, 1) amount=Decimal('12.50')
try:
Settlement.model_validate({'settled_on': '2026-10-01', 'amount': '12.50'})
except ValidationError as exc:
print([e['type'] for e in exc.errors()])
#> ['date_type', 'decimal_type']go deeper
Recall that model_validate takes Python objects and model_validate_json takes a JSON string or bytes, and that both raise ValidationError.
Explain that JSON lacks date, UUID and Decimal types, so strict JSON accepts strings for them while strict Python input needs instances.
Diagnose the json.loads-then-model_validate bug on strict models and fix it with model_validate_json or model_validate_strings, noting the speed gain.
Set the house rule for ingestion paths: JSON bodies go straight to model_validate_json, so strictness behaves the same in every service.
## Two input modes Pydantic 2 validates input in one of two **modes**, and the method you call picks the mode: | Mode | Methods | Input | |---|---|---| | Python | the constructor, `model_validate`, `TypeAdapter.validate_python` | Python objects: dicts, instances, `bytes`, sets | | JSON | `model_validate_json`, `TypeAdapter.validate_json` | a JSON `str` or `bytes` document | | Strings | `model_validate_strings`, `TypeAdapter.validate_strings` | a dict of string keys and values, validated with JSON-mode rules | The documentation warns that, depending on the types and configuration involved, Python and JSON modes may validate differently, and strictness is where the difference bites. ## Strict mode in each mode Strict mode means "the input must already be the declared type". In Python mode that is literal: a `date` field wants a `datetime.date` instance. JSON, though, has no date, UUID, Decimal or tuple types, so a strict rule of "instances only" would make those types impossible to send in JSON. Pydantic therefore relaxes strictness for JSON input where JSON has no native representation: | Field type | Strict, Python mode | Strict, JSON mode | |---|---|---| | `date`, `datetime` | instances only | ISO strings accepted | | `UUID` | `UUID` instances only | strings accepted | | `Decimal` | `Decimal` instances only | numbers and numeric strings accepted | | `tuple` | `tuple` only | arrays accepted | | `Enum` | members only | values accepted | | `int` | `int` only, no `bool` | JSON integers only, `"123"` refused | | `bool` | `True`/`False` only | `true`/`false` only, `"true"` refused | The last two rows matter as much as the first five: strict JSON is not lax JSON. A numeric string for an `int` is a JSON type mismatch that JSON could have avoided, so it stays refused. ## The json.loads trap A common bug follows directly. A service reads a request body, calls `json.loads`, and passes the dict to `model_validate` on a strict settlement model. The JSON document `{"settled_on": "2026-10-01", "amount": "12.50"}` would have passed `model_validate_json`, but after `json.loads` the values are plain Python strings, the validator runs in Python mode, and the result is `date_type` and `decimal_type` errors. Nothing about the data changed; the mode did. The fixes are: 1. Pass the raw bytes or string to `model_validate_json` instead of parsing first. 2. If the data arrives as a dict of strings from a non-JSON source, use `model_validate_strings`, which applies JSON-mode rules. 3. Relax the specific fields with `Field(strict=False)` if they genuinely receive Python strings. ## Spotting the mode in an error The error type gives the mode away. A `date_type` or `decimal_type` error whose input is a perfectly valid ISO date or numeric string means the value was refused for its type, not for its content, which is what Python-mode strictness does. A `*_parsing` error, such as `datetime_parsing`, means the string itself was malformed. Strings mode has its own edge: `model_validate_strings` with `strict=True` refuses `'2024-04-01'` for a `datetime` field with `datetime_parsing`, because a strict datetime string needs a time separator, while lax mode accepts the date-only form. ## Lax mode differs too Even without strict mode, the rows differ: `bytes`, `bytearray`, `set`, `frozenset`, `deque` and `Decimal` instances can only arrive from Python, so conversions such as `bytes` to `str` or `set` to `list` exist only in Python mode. In JSON mode the input types are JSON's: strings, numbers, booleans, null, arrays and objects. ## Why model_validate_json is usually the better call - **Speed.** Pydantic parses the JSON inside its Rust core, with the `jiter` parser since 2.5, and validates in the same pass; the documentation calls this generally faster than parsing to a dict first. - **Fidelity.** The validator sees JSON types, so the strict-mode JSON rules apply as designed. - **One step.** You hand Pydantic the raw `str` or `bytes`, so no intermediate dict exists for other code to re-type or mutate before validation. ## Choosing Use `model_validate_json` whenever the input is a JSON document, `model_validate` when you already hold Python objects such as instances, dates or `Decimal` values, and `model_validate_strings` for dicts of strings. When an interviewer asks why a strict model "works in one test and fails in another", the mode is the first thing to check.
- In Pydantic 2, how do you get JSON-mode validation for a dict of strings read from a CSV row?Call `model_validate_strings`, or `TypeAdapter.validate_strings`. It takes a possibly nested dict of string keys and values and validates it in JSON mode, so date, Decimal and number strings are converted as JSON input would be. Dumping the row to JSON first and calling `model_validate_json` is the other documented route.
- Is model_validate_json in Pydantic 2 just json.loads followed by model_validate?No. Pydantic parses the document itself inside its core and validates in JSON mode, which is generally faster and applies the JSON rules, such as strict date strings being accepted. `json.loads` followed by `model_validate` validates in Python mode, where strict mode wants instances, so results can differ.
- model_validate_json is only a convenience wrapper around json.loads and model_validate.
- Strict mode means exactly the same thing for JSON and Python input.
- Strict JSON mode accepts "123" for an int because JSON numbers are strings.
- A strict date field can never accept a string under any input mode.
- Parsing with json.loads first is faster because Pydantic's parser is slower.