From 59364af9328a0db70263c6c8fceb8574779a3f09 Mon Sep 17 00:00:00 2001 From: cobycloud <25079070+cobycloud@users.noreply.github.com> Date: Thu, 9 Jan 2025 04:05:17 -0600 Subject: [PATCH] core - add support for model_validate_yaml() --- pkgs/core/pyproject.toml | 1 + pkgs/core/swarmauri_core/ComponentBase.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/pkgs/core/pyproject.toml b/pkgs/core/pyproject.toml index d6f2e95e..63edba59 100644 --- a/pkgs/core/pyproject.toml +++ b/pkgs/core/pyproject.toml @@ -16,6 +16,7 @@ classifiers = [ [tool.poetry.dependencies] python = ">=3.10,<3.13" pydantic = "^2.0" +pyyaml = "^6.0.2" #numpy = "==1.26.4" #requests = "^2.0" diff --git a/pkgs/core/swarmauri_core/ComponentBase.py b/pkgs/core/swarmauri_core/ComponentBase.py index ce6ffaab..33dbaedd 100644 --- a/pkgs/core/swarmauri_core/ComponentBase.py +++ b/pkgs/core/swarmauri_core/ComponentBase.py @@ -3,6 +3,7 @@ import hashlib import inspect import json +import yaml import logging from enum import Enum from threading import Lock @@ -490,3 +491,16 @@ def recreate_models(cls): except Exception as e: logger.error(f"Error while rebuilding model '{model_class.__name__}': {e}") logger.info("All models have been successfully recreated.") + + @classmethod + def model_validate_yaml(cls, yaml_data: str): + try: + # Parse YAML into a Python dictionary + yaml_content = yaml.safe_load(yaml_data) + + # Convert the dictionary to JSON and validate using Pydantic + return cls.model_validate_json(json.dumps(yaml_content)) + except yaml.YAMLError as e: + raise ValueError(f"Invalid YAML data: {e}") + except ValidationError as e: + raise ValueError(f"Validation failed: {e}") \ No newline at end of file