Validate structured utility outputs
This commit is contained in:
+13
-15
@@ -1,11 +1,12 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from duck_core.structured_output import load_json_object, validate_json_object
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -124,26 +125,23 @@ class MemoryPolicy:
|
||||
|
||||
def _parse_response(self, content: str, summary: str, task_id: str) -> MemoryDecision:
|
||||
try:
|
||||
data = json.loads(content)
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
data = validate_json_object(
|
||||
load_json_object(content, "memory policy"),
|
||||
self._RESPONSE_SCHEMA,
|
||||
"memory policy",
|
||||
)
|
||||
except ValueError as exc:
|
||||
logger.warning("MemoryPolicy: invalid JSON for %s: %s", task_id, content[:200])
|
||||
return MemoryDecision(
|
||||
should_store=False,
|
||||
memory_type="event",
|
||||
summary=summary,
|
||||
importance=0.0,
|
||||
metadata={"task_id": task_id, "source": "llm_policy_fallback"},
|
||||
)
|
||||
|
||||
required = ("should_store", "memory_type", "summary", "importance", "scope")
|
||||
if not all(key in data for key in required):
|
||||
logger.warning("MemoryPolicy: missing fields for %s: %s", task_id, list(data.keys()))
|
||||
return MemoryDecision(
|
||||
should_store=False,
|
||||
memory_type="event",
|
||||
summary=summary,
|
||||
importance=0.0,
|
||||
metadata={"task_id": task_id, "source": "llm_policy_fallback"},
|
||||
metadata={
|
||||
"task_id": task_id,
|
||||
"source": "llm_policy_fallback",
|
||||
"error": str(exc),
|
||||
},
|
||||
)
|
||||
|
||||
return MemoryDecision(
|
||||
|
||||
Reference in New Issue
Block a user