Skip to content

Key order not conserved in OrderedDicts #12273

@sdascoli

Description

@sdascoli

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

It looks like OrderedDict fields are broken, as can be seen in the example below:

from collections import OrderedDict
import pydantic
class Model(pydantic.BaseModel):
    foo: OrderedDict
foo = OrderedDict({"a": 1, "b": 2})
foo.move_to_end("a")
model1 = Model(foo=foo) # this doesn't work
model2 = Model(foo=dict(foo)) # this works
print(foo.keys(), model1.foo.keys(), model2.foo.keys())

Output:
odict_keys(['b', 'a']) odict_keys(['a', 'b']) odict_keys(['b', 'a'])

Example Code

Python, Pydantic & OS Version

pydantic version: 2.11.6
        pydantic-core version: 2.33.2
          pydantic-core build: profile=release pgo=false
               python version: 3.12.11 | packaged by conda-forge | (main, Jun  4 2025, 14:45:31) [GCC 13.3.0]
                     platform: Linux-5.15.0-1061-nvidia-x86_64-with-glibc2.35
             related packages: typing_extensions-4.14.0 fastapi-0.115.12 mypy-1.16.0
                       commit: unknown

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V2Bug related to Pydantic V2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions