Given a possibly nested Pydantic instance, return a flattened version of it, as a dict where nested traversal paths are translated to keys a__b__c. Args: instance (BaseModel): The Pydantic instance to flatten. prefix (str, optional): The prefix to use for the top-level
(
instance: BaseModel,
prefix: str = "",
force_str: bool = False,
)
| 205 | |
| 206 | |
| 207 | def flatten_pydantic_instance( |
| 208 | instance: BaseModel, |
| 209 | prefix: str = "", |
| 210 | force_str: bool = False, |
| 211 | ) -> Dict[str, Any]: |
| 212 | """ |
| 213 | Given a possibly nested Pydantic instance, return a flattened version of it, |
| 214 | as a dict where nested traversal paths are translated to keys a__b__c. |
| 215 | |
| 216 | Args: |
| 217 | instance (BaseModel): The Pydantic instance to flatten. |
| 218 | prefix (str, optional): The prefix to use for the top-level fields. |
| 219 | force_str (bool, optional): Whether to force all values to be strings. |
| 220 | |
| 221 | Returns: |
| 222 | Dict[str, Any]: The flattened dict. |
| 223 | |
| 224 | """ |
| 225 | flat_data: Dict[str, Any] = {} |
| 226 | for name, value in instance.model_dump().items(): |
| 227 | # Assuming nested pydantic model will be a dict here |
| 228 | if isinstance(value, dict): |
| 229 | # Get field info from model_fields |
| 230 | field_info = instance.model_fields[name] |
| 231 | # Try to get the nested model type from field annotation |
| 232 | field_type = ( |
| 233 | field_info.annotation if hasattr(field_info, "annotation") else None |
| 234 | ) |
| 235 | if ( |
| 236 | field_type |
| 237 | and isinstance(field_type, type) |
| 238 | and issubclass(field_type, BaseModel) |
| 239 | ): |
| 240 | nested_flat_data = flatten_pydantic_instance( |
| 241 | field_type(**value), |
| 242 | prefix=f"{prefix}{name}__", |
| 243 | force_str=force_str, |
| 244 | ) |
| 245 | else: |
| 246 | # Skip non-Pydantic nested fields for safety |
| 247 | continue |
| 248 | flat_data.update(nested_flat_data) |
| 249 | else: |
| 250 | flat_data[f"{prefix}{name}"] = str(value) if force_str else value |
| 251 | return flat_data |
| 252 | |
| 253 | |
| 254 | def extract_fields(doc: BaseModel, fields: List[str]) -> Dict[str, Any]: |
nothing calls this directly
no test coverage detected
searching dependent graphs…