Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(memory): normalize invalid extraction timestamps (#18463)

Loong1e147e0c0b89
diff --git a/api/db/joint_services/memory_message_service.py b/api/db/joint_services/memory_message_service.py
index 46b1bc8075..3ec007c595 100644
--- a/api/db/joint_services/memory_message_service.py
+++ b/api/db/joint_services/memory_message_service.py
@@ -198,8 +198,8 @@ async def extract_by_llm(
return [
{
"content": extracted_content["content"],
- "valid_at": format_iso_8601_to_ymd_hms(extracted_content["valid_at"]),
- "invalid_at": format_iso_8601_to_ymd_hms(extracted_content["invalid_at"]) if extracted_content.get("invalid_at") else "",
+ "valid_at": format_iso_8601_to_ymd_hms(extracted_content.get("valid_at", ""), fallback=conversation_time),
+ "invalid_at": format_iso_8601_to_ymd_hms(extracted_content["invalid_at"], fallback="") if extracted_content.get("invalid_at") else "",
"message_type": message_type,
}
for message_type, extracted_content_list in res_json.items()
diff --git a/common/time_utils.py b/common/time_utils.py
index 5f1753e905..7ee88d803f 100644
--- a/common/time_utils.py
+++ b/common/time_utils.py
@@ -129,12 +129,14 @@ def delta_seconds(date_string: str):
return (datetime.datetime.now() - dt).total_seconds()
-def format_iso_8601_to_ymd_hms(time_str: str) -> str:
+def format_iso_8601_to_ymd_hms(time_str: str, fallback: str | None = None) -> str:
"""
Convert ISO 8601 formatted string to "YYYY-MM-DD HH:MM:SS" format.
Args:
time_str: ISO 8601 date string (e.g. "2024-01-01T12:00:00Z")
+ fallback: Value to return when ``time_str`` cannot be parsed. When omitted,
+ the original input is returned.
Returns:
str: Date string in "YYYY-MM-DD HH:MM:SS" format
@@ -148,6 +150,6 @@ def format_iso_8601_to_ymd_hms(time_str: str) -> str:
try:
dt = parser.isoparse(time_str)
return dt.strftime("%Y-%m-%d %H:%M:%S")
- except Exception as e:
- logging.error(str(e))
- return time_str
+ except (TypeError, ValueError, OverflowError) as e:
+ logging.error("Failed to parse ISO 8601 timestamp %r: %s", time_str, e)
+ return time_str if fallback is None else fallback
diff --git a/test/unit_test/api/db/joint_services/test_memory_message_timestamp_normalization.py b/test/unit_test/api/db/joint_services/test_memory_message_timestamp_normalization.py
new file mode 100644
index 0000000000..052cf89e5f
--- /dev/null
+++ b/test/unit_test/api/db/joint_services/test_memory_message_timestamp_normalization.py
@@ -0,0 +1,56 @@
+#
+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+from contextlib import nullcontext
+from types import SimpleNamespace
+from unittest.mock import AsyncMock
+
+import pytest
+
+from api.db.joint_services import memory_message_service
+
+
+async def test_extract_by_llm_replaces_invalid_timestamps(monkeypatch):
+ llm = SimpleNamespace(async_chat=AsyncMock(return_value="response"))
+ extracted = {
+ "semantic": [
+ {
+ "content": "A timeless fact",
+ "valid_at": "not-a-date",
+ "invalid_at": "also-not-a-date",
+ }
+ ]
+ }
+
+ monkeypatch.setattr(memory_message_service, "current_timestamp", lambda: 1)
+ monkeypatch.setattr(memory_message_service, "timestamp_to_date", lambda _timestamp: "2026-08-18 12:00:00")
+ monkeypatch.setattr(memory_message_service, "resolve_model_config", lambda *_args: SimpleNamespace())
+ monkeypatch.setattr(memory_message_service, "LLMBundle", lambda *_args: nullcontext(llm))
+ monkeypatch.setattr(memory_message_service, "get_json_result_from_llm_response", lambda _response: extracted)
+
+ result = await memory_message_service.extract_by_llm(
+ tenant_id="tenant-1",
+ tenant_llm_id=None,
+ extract_conf={},
+ memory_type=["semantic"],
+ user_input="Remember this",
+ agent_response="I will remember it",
+ llm_id="model-1",
+ )
+
+ assert result[0]["valid_at"] == "2026-08-18 12:00:00"
+ assert result[0]["invalid_at"] == ""
diff --git a/test/unit_test/common/test_time_utils.py b/test/unit_test/common/test_time_utils.py
index ae8f5feeba..b90070bbb2 100644
--- a/test/unit_test/common/test_time_utils.py
+++ b/test/unit_test/common/test_time_utils.py
@@ -711,3 +711,11 @@ class TestFormatIso8601ToYmdHms:
def test_invalid_string_returns_original(self):
"""Unparseable input is returned unchanged."""
assert format_iso_8601_to_ymd_hms("not-a-date") == "not-a-date"
+
+ def test_invalid_string_uses_explicit_fallback(self, caplog):
+ """Callers can prevent invalid timestamps from being written through."""
+ with caplog.at_level("ERROR"):
+ result = format_iso_8601_to_ymd_hms("not-a-date", fallback="2026-08-18 12:00:00")
+
+ assert result == "2026-08-18 12:00:00"
+ assert "not-a-date" in caplog.text