Skip to content

PydanticSerializationError missing validation error field name #1483

@ddennerline3

Description

@ddennerline3

I am getting quite a few of these Expected 'int' but got 'float' - serialized value may not be as expected warnings in the log files. Is there a way to callout the specific field name?

I looked at Rust's exception and error message, and I don't see any "add_variable_context" flag.

If not easily possible, would it be possible to add the field name that is triggering the warning to PydanticSerializationError please.

The object that is triggering the exception has large number of sub-objects and fields, so going through all the fields would be very time consuming.

# Internal imports

# External imports
from pydantic import BaseModel
from pydantic_core import PydanticSerializationError


# Own imports

class TestModelDump(BaseModel):
    int_value: int | None = None
    float_value: float | None = None


def test_mismatch():
    valu = TestModelDump(int_value=1, float_value=1.0)
    result = valu.model_dump_json()
    print(result)

    valu = TestModelDump(int_value=1.0, float_value=1.0)
    result = valu.model_dump_json(warnings="error")
    print(result)

    valu = TestModelDump(int_value=1.0, float_value=1)
    valu.int_value = 1.0
    valu.float_value = 1
    try:
        result = valu.model_dump_json(warnings="error")
    except PydanticSerializationError as e:
        print(e)
    else:
        print(result)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions