MicroFish/backend/app/utils/ontology.py

30 lines
855 B
Python

"""Helpers for validating LLM-generated ontology structures."""
from typing import Any, Dict, Optional
def normalize_ontology_attribute(attribute: Any) -> Optional[Dict[str, Any]]:
"""Return a safe attribute definition, or ``None`` for unusable values."""
if isinstance(attribute, str):
if not attribute.strip():
return None
return {
"name": attribute,
"type": "text",
"description": attribute,
}
if not isinstance(attribute, dict):
return None
name = attribute.get("name")
if not isinstance(name, str) or not name.strip():
return None
normalized = dict(attribute)
description = normalized.get("description")
if not isinstance(description, str) or not description:
normalized["description"] = name
return normalized