@@ -199,6 +199,7 @@ def rename_types(output_path: Path) -> list[str]:
199199 content = _apply_field_overrides (content )
200200 content = _apply_default_overrides (content )
201201 content = _add_description_comments (content )
202+ content = _ensure_custom_base_model (content )
202203
203204 alias_lines = [f"{ old } = { new } " for old , new in sorted (RENAME_MAP .items ())]
204205 alias_block = BACKCOMPAT_MARKER + "\n " + "\n " .join (alias_lines ) + "\n "
@@ -220,6 +221,37 @@ def rename_types(output_path: Path) -> list[str]:
220221 return warnings
221222
222223
224+ def _ensure_custom_base_model (content : str ) -> str :
225+ if "class BaseModel(_BaseModel):" in content :
226+ return content
227+ lines = content .splitlines ()
228+ for idx , line in enumerate (lines ):
229+ if not line .startswith ("from pydantic import " ):
230+ continue
231+ imports = [part .strip () for part in line [len ("from pydantic import " ) :].split ("," )]
232+ has_alias = any (part == "BaseModel as _BaseModel" for part in imports )
233+ has_config = any (part == "ConfigDict" for part in imports )
234+ new_imports = []
235+ for part in imports :
236+ if part == "BaseModel" :
237+ new_imports .append ("BaseModel as _BaseModel" )
238+ has_alias = True
239+ else :
240+ new_imports .append (part )
241+ if not has_alias :
242+ new_imports .append ("BaseModel as _BaseModel" )
243+ if not has_config :
244+ new_imports .append ("ConfigDict" )
245+ lines [idx ] = "from pydantic import " + ", " .join (new_imports )
246+ insert_idx = idx + 1
247+ lines .insert (insert_idx , "" )
248+ lines .insert (insert_idx + 1 , "class BaseModel(_BaseModel):" )
249+ lines .insert (insert_idx + 2 , " model_config = ConfigDict(populate_by_name=True)" )
250+ lines .insert (insert_idx + 3 , "" )
251+ break
252+ return "\n " .join (lines ) + "\n "
253+
254+
223255def _apply_field_overrides (content : str ) -> str :
224256 for class_name , field_name , new_type , optional in FIELD_TYPE_OVERRIDES :
225257 if optional :
0 commit comments