1- from typing import Any , Generic , List , TypeVar
1+ from typing import Any , Generic , List , Literal , TypeVar
22
33import yaml
44from pydantic import BaseModel , ConfigDict , RootModel
@@ -67,7 +67,9 @@ def to_yaml(self, *, by_alias: bool = True, exclude_none: bool = False) -> str:
6767 Returns:
6868 YAML string representation of the model.
6969 """
70- data = self .to_dict (by_alias = by_alias , exclude_none = exclude_none )
70+ data = self .model_dump (
71+ by_alias = by_alias , exclude_none = exclude_none , mode = "json"
72+ )
7173 return yaml .safe_dump (
7274 data , default_flow_style = False , allow_unicode = True , sort_keys = False
7375 )
@@ -86,19 +88,25 @@ def __len__(self):
8688 return len (self .root )
8789
8890 def to_list (
89- self , * , by_alias : bool = True , exclude_none : bool = False
91+ self ,
92+ * ,
93+ by_alias : bool = True ,
94+ exclude_none : bool = False ,
95+ mode : Literal ["python" , "json" ] = "python" ,
9096 ) -> list [dict [str , Any ]]:
9197 """Export all items as a list of dictionaries.
9298
9399 Args:
94100 by_alias: Use camelCase keys (API format) if True, snake_case if False.
95101 exclude_none: Exclude fields with None values if True.
102+ mode: Serialization mode. "python" returns native Python objects,
103+ "json" returns JSON-compatible types (e.g., datetime as ISO string).
96104
97105 Returns:
98106 List of dictionary representations.
99107 """
100108 return [
101- item .model_dump (by_alias = by_alias , exclude_none = exclude_none )
109+ item .model_dump (by_alias = by_alias , exclude_none = exclude_none , mode = mode )
102110 for item in self .root
103111 ]
104112
@@ -122,7 +130,8 @@ def to_json(
122130 import json
123131
124132 return json .dumps (
125- self .to_list (by_alias = by_alias , exclude_none = exclude_none ), indent = indent
133+ self .to_list (by_alias = by_alias , exclude_none = exclude_none , mode = "json" ),
134+ indent = indent ,
126135 )
127136
128137 def to_yaml (self , * , by_alias : bool = True , exclude_none : bool = False ) -> str :
@@ -135,8 +144,8 @@ def to_yaml(self, *, by_alias: bool = True, exclude_none: bool = False) -> str:
135144 Returns:
136145 YAML string representation.
137146 """
138- return yaml .dump (
139- self .to_list (by_alias = by_alias , exclude_none = exclude_none ),
147+ return yaml .safe_dump (
148+ self .to_list (by_alias = by_alias , exclude_none = exclude_none , mode = "json" ),
140149 default_flow_style = False ,
141150 allow_unicode = True ,
142151 sort_keys = False ,
0 commit comments