Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(api): default missing retriever_resources to empty list (#43036)

leilei3167dbde018550b7
diff --git a/api/models/model.py b/api/models/model.py
index c7637b034d2..a8888cffbb0 100644
--- a/api/models/model.py
+++ b/api/models/model.py
@@ -1744,7 +1744,8 @@ class Message(TypeBase):
@property
def retriever_resources(self) -> Any:
- return self.message_metadata_dict.get("retriever_resources") if self.message_metadata else []
+ resources = self.message_metadata_dict.get("retriever_resources", []) if self.message_metadata else []
+ return [] if resources is None else resources
def message_files_with_session(self, *, session: Session) -> list[MessageFileInfo]:
from factories import file_factory
diff --git a/api/tests/unit_tests/models/test_model.py b/api/tests/unit_tests/models/test_model.py
index 6f8cc296e9a..ec2ebcc1fa5 100644
--- a/api/tests/unit_tests/models/test_model.py
+++ b/api/tests/unit_tests/models/test_model.py
@@ -1255,6 +1255,83 @@ class TestAppModelsCases:
# Assert
assert result == {}
+ def test_retriever_resources_missing_key_returns_empty_list(self):
+ """message_metadata present without retriever_resources must yield [].
+
+ Regression for #41688: web GET /messages failed Pydantic list validation
+ when metadata existed but the key was absent (property returned None).
+ """
+ message = Message(
+ app_id=str(uuid4()),
+ conversation_id=str(uuid4()),
+ query="Test query",
+ message={"role": "user", "content": "Test"},
+ answer="Test answer",
+ message_unit_price=Decimal("0.0001"),
+ answer_unit_price=Decimal("0.0002"),
+ currency="USD",
+ from_source=ConversationFromSource.API,
+ message_metadata=json.dumps({"usage": {"tokens": 1}}),
+ )
+
+ assert message.retriever_resources == []
+
+ def test_retriever_resources_none_metadata_returns_empty_list(self):
+ """No message_metadata should still expose retriever_resources as []."""
+ message = Message(
+ app_id=str(uuid4()),
+ conversation_id=str(uuid4()),
+ query="Test query",
+ message={"role": "user", "content": "Test"},
+ answer="Test answer",
+ message_unit_price=Decimal("0.0001"),
+ answer_unit_price=Decimal("0.0002"),
+ currency="USD",
+ from_source=ConversationFromSource.API,
+ message_metadata=None,
+ )
+
+ assert message.retriever_resources == []
+
+ def test_retriever_resources_present_passthrough(self):
+ """Existing retriever_resources list should be returned unchanged."""
+ resources = [{"dataset_id": "ds-1", "document_id": "doc-1"}]
+ message = Message(
+ app_id=str(uuid4()),
+ conversation_id=str(uuid4()),
+ query="Test query",
+ message={"role": "user", "content": "Test"},
+ answer="Test answer",
+ message_unit_price=Decimal("0.0001"),
+ answer_unit_price=Decimal("0.0002"),
+ currency="USD",
+ from_source=ConversationFromSource.API,
+ message_metadata=json.dumps({"retriever_resources": resources}),
+ )
+
+ assert message.retriever_resources == resources
+
+ def test_retriever_resources_explicit_null_returns_empty_list(self):
+ """Explicit retriever_resources: null must yield [], not None.
+
+ WebMessageListItem.retriever_resources is list[RetrieverResource].
+ dict.get(..., []) does not replace an explicit JSON null.
+ """
+ message = Message(
+ app_id=str(uuid4()),
+ conversation_id=str(uuid4()),
+ query="Test query",
+ message={"role": "user", "content": "Test"},
+ answer="Test answer",
+ message_unit_price=Decimal("0.0001"),
+ answer_unit_price=Decimal("0.0002"),
+ currency="USD",
+ from_source=ConversationFromSource.API,
+ message_metadata=json.dumps({"retriever_resources": None}),
+ )
+
+ assert message.retriever_resources == []
+
@pytest.mark.parametrize("sqlite_session", [(Message,)], indirect=True)
def test_message_to_dict_serialization(self, sqlite_session: Session):
"""Test message to_dict method."""