Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(memory): treat bare empty-list LLM response as no-operations instead of crashing extraction (#2487) (#2490)

Evob3ca0ef47ef6
diff --git a/openviking/session/memory/schema_model_generator.py b/openviking/session/memory/schema_model_generator.py
index e02fad582..b69c53a86 100644
--- a/openviking/session/memory/schema_model_generator.py
+++ b/openviking/session/memory/schema_model_generator.py
@@ -341,6 +341,9 @@ class SchemaModelGenerator:
StructuredMemoryOperations.is_empty = is_empty
StructuredMemoryOperations.to_legacy_operations = to_legacy_operations
StructuredMemoryOperations._memory_type_fields = memory_type_fields # type: ignore
+ # Opt this model into treating a bare `[]` LLM response as an empty-ops result
+ # (every field is default_factory=list); see parse_json_with_stability Layer 3.
+ StructuredMemoryOperations._allow_empty_list_response = True # type: ignore
self._operations_model = StructuredMemoryOperations
return self._operations_model
diff --git a/openviking/session/memory/utils/json_parser.py b/openviking/session/memory/utils/json_parser.py
index 6643aab95..a5ac93408 100644
--- a/openviking/session/memory/utils/json_parser.py
+++ b/openviking/session/memory/utils/json_parser.py
@@ -429,6 +429,15 @@ def parse_json_with_stability(
if isinstance(parsed_data, list) and len(parsed_data) > 0:
parsed_data = parsed_data[0]
tracer.info("Extracted first item from list response")
+ elif isinstance(parsed_data, list) and len(parsed_data) == 0 and getattr(
+ model_class, "_allow_empty_list_response", False
+ ):
+ # The operations model opts in (via _allow_empty_list_response) to treating a
+ # bare `[]` as a valid "no operations" outcome: every field is default_factory=list,
+ # so map [] to {} and let it validate to an empty-ops object instead of raising
+ # downstream. Every other model keeps the fail-loud "Expected dict" path below.
+ parsed_data = {}
+ tracer.info("Empty list response treated as empty object (no operations)")
if not isinstance(parsed_data, dict):
return None, f"Expected dict after parsing, got {parsed_data}"
diff --git a/tests/session/memory/test_json_stability.py b/tests/session/memory/test_json_stability.py
index bec614699..f31b36aa5 100644
--- a/tests/session/memory/test_json_stability.py
+++ b/tests/session/memory/test_json_stability.py
@@ -180,6 +180,35 @@ class TestParseJsonWithStability:
assert error is None
assert data.reasonning == "test"
+ def test_handles_empty_list_for_operations_model(self):
+ """A bare [] from the operations model (opted in via _allow_empty_list_response)
+ is a valid 'no operations' outcome: mapped to an empty object, not an error."""
+
+ class OperationsLike(BaseModel):
+ reasonning: str = ""
+ tags: List[str] = Field(default_factory=list)
+
+ OperationsLike._allow_empty_list_response = True
+
+ data, error = parse_json_with_stability('[]', model_class=OperationsLike)
+ assert error is None
+ assert data.tags == []
+
+ def test_empty_list_fails_loud_without_opt_in(self):
+ """A bare [] for any model that has NOT opted in stays fail-loud, even if it
+ happens to expose an is_empty() convenience method."""
+
+ class HasIsEmptyButNotOptedIn(BaseModel):
+ tags: List[str] = Field(default_factory=list)
+
+ def is_empty(self) -> bool:
+ return not self.tags
+
+ for model in (self.TestModel, HasIsEmptyButNotOptedIn):
+ data, error = parse_json_with_stability('[]', model_class=model)
+ assert data is None
+ assert error is not None
+
def test_filters_extra_fields(self):
"""Test extra fields are filtered when expected_fields is provided."""
content = '{"reasonning": "test", "extra_field": "should be filtered", "count": 42}'