[Python] How to fix - UserWarning: Pydantic serializer warnings in Pydantic V2?

Discussão em 'Python' iniciado por Stack, Julho 18, 2025.

  1. Stack

    Stack Membro Participativo

    Using datamodel-codegen command with JSON data as input type and generating Pydantic schema as output, during this process I was seeing warnings.

    What is the meaning of these warnings and how to fix them? What kind of issues it can create(or why this is a warning)?

    UserWarning:

    Expected `Union[list[definition-ref], definition-ref, bool]` but got `JsonSchemaObject` - serialized value may not be as expected
    Expected `Union[definition-ref, bool]` but got `JsonSchemaObject` - serialized value may not be as expected
    Expected `Union[definition-ref, bool]` but got `JsonSchemaObject` - serialized value may not be as expected
    Expected `Union[definition-ref, bool]` but got `JsonSchemaObject` - serialized value may not be as expected
    return self.__pydantic_serializer__.to_python(


    To reproduce:

    pets.json

    {
    "pets": [
    {
    "name": "dog",
    "age": 2
    },
    {
    "name": "cat",
    "age": 1
    },
    {
    "name": "snake",
    "age": 3,
    "nickname": "python"
    }
    ],
    "status": 200
    }


    Command:

    datamodel-codegen --input pets.json --input-file-type json --output model.py


    Versions:

    python - 3.11.4
    pydantic==2.1.1
    datamodel-code-generator==0.21.4
    genson==1.2.2

    Continue reading...

Compartilhe esta Página