Spaces:
Sleeping
Sleeping
| from __future__ import annotations | |
| import json | |
| from typing import Any, TypeVar | |
| from pydantic import BaseModel | |
| ModelT = TypeVar("ModelT", bound=BaseModel) | |
| def model_to_dict(model: BaseModel) -> dict[str, Any]: | |
| if hasattr(model, "model_dump"): | |
| return model.model_dump(mode="json") # type: ignore[attr-defined] | |
| return json.loads(model.json()) | |
| def model_validate(model_cls: type[ModelT], data: Any) -> ModelT: | |
| if hasattr(model_cls, "model_validate"): | |
| return model_cls.model_validate(data) # type: ignore[attr-defined] | |
| return model_cls.parse_obj(data) | |