Description
Describe the bug
When constructing validation errors using ValidationError.from_exception_data
, this fails when the error type is ip_v4_address
.It will raise:
KeyError: "Invalid error type: 'ip_v4_address'
To Reproduce
Run the code below. It uses a custom model validator that manually constructs the validation error using ValidationError.from_exception_data
from pydantic import BaseModel, Field, model_validator, ValidationError
from ipaddress import IPv4Address
from pydantic_core import InitErrorDetails
class OutputStream(BaseModel):
destination_ip: IPv4Address = Field(
...,
)
class StreamingDevice(BaseModel):
output_stream: OutputStream = Field(
...,
)
@model_validator(mode="before")
@classmethod
def validate_model(cls, data: dict) -> dict:
validation_errors: list[InitErrorDetails] = []
if data.get("output_stream") is not None:
try:
OutputStream.model_validate(data["output_stream"])
except ValidationError as e:
validation_errors.extend(e.errors())
if validation_errors:
raise ValidationError.from_exception_data(title=cls.__name__, line_errors=validation_errors)
return data
streaming_device_payload = {"output_stream": {"destination_ip": "123"}}
streaming_device = StreamingDevice.model_validate(streaming_device_payload)
Expected behavior
Pydantic is capable of generating 'ip_v4_address' errors when using model validation without the custom model validator. Therefore, we should be able to manually construct the validation error using the error dictionary that contains the ip_v4_address error type as input into ValidationError.from_exception_data
.
If you were to remove the custom model validator in the example above, Pydantic would successfully throw that error type:
pydantic_core._pydantic_core.ValidationError: 1 validation error for StreamingDevice
output_stream.destination_ip
Input is not a valid IPv4 address [type=ip_v4_address, input_value='123', input_type=str
Haven't tested it, but should ip_v4_address be in this error list here?
https://github.com/pydantic/pydantic-core/blob/main/python/pydantic_core/core_schema.py#L3900
Version:
- OS: Ubuntu 20.04.4 LTS
- Python version: 3.9.16
- pydantic version: 2.3.0