|
42 | 42 | model_validator,
|
43 | 43 | root_validator,
|
44 | 44 | validator,
|
45 |
| - ConfigDict |
| 45 | + ConfigDict, |
| 46 | + field_serializer |
46 | 47 | )
|
47 | 48 |
|
48 | 49 | logger = logging.getLogger(__name__)
|
@@ -96,36 +97,11 @@ class BaseModelWithConfigDict(BaseModel):
|
96 | 97 | from_attributes=True,
|
97 | 98 | alias_generator=to_camel_case,
|
98 | 99 | populate_by_name=True,
|
99 |
| - json_encoders={datetime: encode_datetime}, |
100 | 100 | use_enum_values=True,
|
101 | 101 | extra="ignore",
|
102 | 102 | json_schema_extra={"nullable": True},
|
103 | 103 | )
|
104 | 104 |
|
105 |
| - # class Config: |
106 |
| - # """ |
107 |
| - # A configuration class that provides default behaviors for how to handle serialization, |
108 |
| - # alias generation, enum values, and extra fields when working with models. |
109 |
| - |
110 |
| - # Attributes: |
111 |
| - # from_attributes (bool): Flag to indicate if attributes should be taken from model fields. |
112 |
| - # alias_generator (callable): Function used to generate aliases for field names (e.g., converting to camelCase). |
113 |
| - # populate_by_name (bool): Flag to specify whether to populate fields by name during initialization. |
114 |
| - # json_encoders (dict): Custom JSON encoders for specific types, such as datetime encoding. |
115 |
| - # use_enum_values (bool): Flag to determine if enum values should be serialized or the enum type itself. |
116 |
| - # extra (str): Defines behavior for extra fields in models. The "ignore" option means extra fields are ignored. |
117 |
| - # json_schema_extra (dict): Additional schema information, e.g., specifying that fields can be nullable. |
118 |
| - |
119 |
| - # """ |
120 |
| - |
121 |
| - # from_attributes = True |
122 |
| - # alias_generator = to_camel_case |
123 |
| - # populate_by_name = True |
124 |
| - # json_encoders = {datetime: encode_datetime} |
125 |
| - # use_enum_values = True |
126 |
| - # extra = "ignore" |
127 |
| - # json_schema_extra = {"nullable": True} |
128 |
| - |
129 | 105 | def to_dict(self, use_alias: bool = False) -> Dict[str, Any]:
|
130 | 106 | """
|
131 | 107 | Converts the model instance into a dictionary representation.
|
@@ -548,6 +524,11 @@ class ResourceNotification(BaseModelWithConfigDict):
|
548 | 524 | content: ResourceContent
|
549 | 525 | timestamp: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
|
550 | 526 |
|
| 527 | + @field_serializer("timestamp") |
| 528 | + def serialize_timestamp(self, dt: datetime) -> str: |
| 529 | + # now returns ISO string with Z |
| 530 | + return dt.astimezone(timezone.utc).isoformat().replace("+00:00", "Z") |
| 531 | + |
551 | 532 |
|
552 | 533 | # --- Prompt Schemas ---
|
553 | 534 |
|
@@ -1027,6 +1008,11 @@ class EventMessage(BaseModelWithConfigDict):
|
1027 | 1008 | data: Dict[str, Any] = Field(..., description="Event payload")
|
1028 | 1009 | timestamp: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
|
1029 | 1010 |
|
| 1011 | + @field_serializer("timestamp") |
| 1012 | + def serialize_timestamp(self, dt: datetime) -> str: |
| 1013 | + # now returns ISO string with Z |
| 1014 | + return dt.astimezone(timezone.utc).isoformat().replace("+00:00", "Z") |
| 1015 | + |
1030 | 1016 |
|
1031 | 1017 | class AdminToolCreate(BaseModelWithConfigDict):
|
1032 | 1018 | """Schema for creating tools via admin UI.
|
|
0 commit comments