class User(BaseModel):
name: str
age: int
User.model_validate_json('{"name": "Alice"}')
Error:
pydantic.ValidationError: 1 validation error for User
age
Field required [type=missing, input_value={'name': 'Alice'},
input_type=dict]

class User(BaseModel):
name: str
age: int
User.model_validate_json('{"name": "Alice"}')
Error:
pydantic.ValidationError: 1 validation error for User
age
Field required [type=missing, input_value={'name': 'Alice'},
input_type=dict]
Fix:
from typing import Optional
class User(BaseModel):
name: str
age: Optional[int] = None
Tell us about your project and get help tailored to your problem.
Contact usEveryone can read. Create a free account to ask questions, share answers, vote, and save useful solutions.