Skip to content

Generators raise ValidationError for list fields in v2.4 #7613

@PoByBolek

Description

@PoByBolek

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Pydantic 2.4 seems to require a maximum length for list fields that are filled by generator objects. This wasn't there in Pydantic 2.3.

Traceback (most recent call last):
  File "/home/bug/pydantic-bug/bug.py", line 18, in <module>
    Bug.model_validate({'values': count(20)})
  File "/home/bug/pydantic-bug/venv/lib/python3.10/site-packages/pydantic/main.py", line 503, in model_validate
    return cls.__pydantic_validator__.validate_python(
pydantic_core._pydantic_core.ValidationError: 1 validation error for Bug
values
  List should have at most 10 items after validation, not 11 [type=too_long, input_value=<generator object count at 0x7f0bb48963b0>, input_type=generator]
    For further information visit https://errors.pydantic.dev/2.4/v/too_long

Example Code

from pydantic import BaseModel


class Bug(BaseModel):
    values: list[int]


def count(N):
    yield from range(N)


Bug.model_validate({'values': list(count(20))})
# works

Bug.model_validate({'values': range(20)})
# works

Bug.model_validate({'values': count(20)})
# ValidationError: List should have at most 10 items after validation, not 11 [type=too_long, input_value=<generator object count at 0x7f0bb48963b0>, input_type=generator]

Python, Pydantic & OS Version

             pydantic version: 2.4.0
        pydantic-core version: 2.10.0
          pydantic-core build: profile=release pgo=true
                 install path: /home/bug/pydantic-bug/venv/lib/python3.10/site-packages/pydantic
               python version: 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0]
                     platform: Linux-5.10.102.1-microsoft-standard-WSL2-x86_64-with-glibc2.35
             related packages: typing_extensions-4.8.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