Skip to content

Memory leak when converting a list of dicts to a list pydantic-models using TypeAdapter? #8397

@sondrfos

Description

@sondrfos

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Hi,

thanks for all the great work on pydantic, and sorry for this mess of a title. I was unsure how to easily explain this.

We have discovered a memory-leak in our FastAPI-application, and think it is due to a bug in pydantic. Calls to a certain route resulted in the memory-usage of the application rising for each call, and the memory were never released.
We separated the pydantic-operations done inside the route, and tested them outside of the FastAPI-application, and experienced the same leakage.

When rolling back to pydantic 2.4.2 we do not experience the memory-leakage anymore.

A minimal, reproducible example is attached below. Just run the code and watch your memory-usage steadily rise.

Example Code

from pydantic import TypeAdapter, BaseModel


class Bar(BaseModel):
    ...


class Foo(BaseModel):
    children: list["Foo"]
    bar: Bar


def get_foo():
    return TypeAdapter(list[Foo]).validate_python(
        [{
                "children": [],
                "bar": Bar(),
            }
        ]
    )

while True:
    get_foo()

Python, Pydantic & OS Version

             pydantic version: 2.5.2
        pydantic-core version: 2.14.5
          pydantic-core build: profile=release pgo=true
                 install path: /usr/local/lib/python3.11/site-packages/pydantic
               python version: 3.11.7 (main, Dec  5 2023, 18:55:16) [GCC 12.2.0]
                     platform: Linux-5.15.133.1-microsoft-standard-WSL2-x86_64-with-glibc2.36
             related packages: pydantic-settings-2.1.0 pyright-1.1.341 email-validator-2.1.0.post1 fastapi-0.105.0 pydantic-extra-types-2.2.0 typing_extensions-4.9.0

Metadata

Metadata

Assignees

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