python
data_error
ai_generated
true
pydantic_core._pydantic_core.ValidationError: 1 validation error for RootModel[list[Item]] Input should be a valid list [type=list_type, input_value={'a': 1}, input_type=dict]
ID: python/pydantic-rootmodel-list
80%Fix Rate
88%Confidence
0Evidence
2024-02-14First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 2.x | active | — | — | — |
Root Cause
A RootModel[list[...]] received a dict. In v2, RootModel wraps the root value directly, so the input must be the list itself, not {'root': [...]}.
generic中文
RootModel[list[...]] 接收到了一个 dict。在 v2 中 RootModel 直接包装根值,因此输入必须是列表本身,而不是 {'root': [...]}。
Workarounds
-
95% success
from pydantic import RootModel Items = RootModel[list[Item]] Items.model_validate([{'a': 1}]) -
90% success
from pydantic import TypeAdapter ta = TypeAdapter(list[Item]) ta.validate_python([{'a': 1}])
Dead Ends
Common approaches that don't work:
-
70% fail
v2 removed the implicit 'root' key; the model now expects the bare value.
-
75% fail
__root__ is a v1 feature and raises PydanticUserError under v2.
-
50% fail
Loses Pydantic validation and serialization hooks.