How to correct __optional_fields = [
"resource_metadata"
] # Any field in here will be skipped by the partial() method
code
__db_specific_fields = [
"created_at",
"updated_at",
] # Fields here are not used by equality check
# TODO[pydantic]: The following keys were removed: `json_encoders`.
# Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-config for more information.
model_config = ConfigDict(
extra="forbid",
json_encoders={
datetime: lambda v: int(v.timestamp()),
ResourceMetadata: lambda v: v.asdict(),
},
arbitrary_types_allowed=True,
validate_assignment=True,
) E pydantic.warnings.PydanticDeprecatedSince20: `json_encoders` is deprecated. See https://docs.pydantic.dev/2.9/concepts/serialization/#custom-serializers for alternatives. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.9/migration/
Assistant
The deprecation warning you're encountering is due to changes in Pydantic version 2.0 and above. The json_encoders configuration has been replaced with serializers. To fix the warning and update your code to be compatible with Pydantic v2, you should replace the json_encoders key in your model_config with serializers.
Here's how you can correct your code:
python
from datetime import datetime
from pydantic import BaseModel
from pydantic.config import ConfigDict
class YourModel(BaseModel):
# Your model fields and other configurations
__optional_fields = [
"resource_metadata"
] # Any field in here will be skipped by the partial() method
__db_specific_fields = [
"created_at",
"updated_at",
] # Fields here are not used by equality check
# Update: Replace `json_encoders` with `serializers` in the ConfigDict
model_config = ConfigDict(
extra="forbid",
serializers={
datetime: lambda v: int(v.timestamp()),
ResourceMetadata: lambda v: v.asdict(),
},
arbitrary_types_allowed=True,
validate_assignment=True,
)
Explanation:
Replace json_encoders with serializers: In Pydantic v2, the json_encoders configuration has been deprecated. The new configuration key is serializers. This key serves the same purpose but aligns with the new serialization mechanisms introduced in Pydantic v2.
Update model_config: Ensure that your model_config is updated as shown above, with serializers replacing json_encoders.
No Need for Decorators in This Case: Since you're providing type-based serialization for datetime and ResourceMetadata, using the serializers key in model_config is sufficient. There's no need to add @serializer or @field_serializer decorators unless you have more complex serialization logic or need field-specific serialization.
Additional Notes:
Pydantic v2 Serialization: Pydantic v2 has introduced new ways to handle serialization to make the process more consistent and flexible. By updating the configuration to use serializers, you ensure that your custom serialization logic for specific types is respected when models are serialized.
Reference Documentation: For more information on the changes and how to handle custom serialization in Pydantic v2, you can refer to the Pydantic v2 Migration Guide and the Serialization Concepts in the official documentation.
By making this change, the deprecation warning should be resolved, and your code will be compliant with the new version of Pydantic.