Skip to content

Commit

Permalink
core - add support for model_validate_yaml()
Browse files Browse the repository at this point in the history
  • Loading branch information
cobycloud committed Jan 9, 2025
1 parent ae1f749 commit 59364af
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkgs/core/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 14 additions & 0 deletions pkgs/core/swarmauri_core/ComponentBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import hashlib
import inspect
import json
import yaml
import logging
from enum import Enum
from threading import Lock
Expand Down Expand Up @@ -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}")

0 comments on commit 59364af

Please sign in to comment.