Question Details

No question body available.

Tags

python pydantic

Answers (1)

January 26, 2026 Score: 2 Rep: 99,498 Quality: Low Completeness: 50%

I found one reasonable option - use a custom validator that always fails:

from typing import Annotated, Any
from pydantic import BaseModel, BeforeValidator, Field

def donotdeserialize(value: Any) -> Any: raise ValueError("Deserialization is not allowed for this field.")

class Item(BaseModel): name: str value: float info: Annotated[str, BeforeValidator(donotdeserialize)] = "default"

def main(): itemdict = {"name": "Sample Item", "value": 10.5}#, "info": "extra info"} item = Item.modelvalidate(item_dict) print(item)

if name == "main": main()

Uncomment the comment and it will throw an error.