From d2ae69135685f6180560cd89c101969f2657fe73 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Thu, 13 Jun 2024 08:01:38 -0700 Subject: [PATCH] chore: Update schema.py and service.py to handle file serialization and deserialization (#2159) Refactor the MessageModel class in schema.py to include methods for validating and serializing the 'files' field. This ensures that the 'files' field can be properly handled when it is a string or a list. In service.py, update the query in the MonitorService class to include the 'files' field when retrieving messages from the database. This ensures that the 'files' field is included in the query results. --- src/backend/base/langflow/services/monitor/schema.py | 10 +++++++++- src/backend/base/langflow/services/monitor/service.py | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/backend/base/langflow/services/monitor/schema.py b/src/backend/base/langflow/services/monitor/schema.py index a409f8337..22ccb2bb4 100644 --- a/src/backend/base/langflow/services/monitor/schema.py +++ b/src/backend/base/langflow/services/monitor/schema.py @@ -91,9 +91,17 @@ class MessageModel(DefaultModel): files: list[str] = [] @field_validator("files", mode="before") + @classmethod def validate_files(cls, v): if isinstance(v, str): - return json.loads(v) + v = json.loads(v) + return v + + @field_serializer("files") + @classmethod + def serialize_files(cls, v): + if isinstance(v, list): + return json.dumps(v) return v @classmethod diff --git a/src/backend/base/langflow/services/monitor/service.py b/src/backend/base/langflow/services/monitor/service.py index 92c642e20..c7d898d11 100644 --- a/src/backend/base/langflow/services/monitor/service.py +++ b/src/backend/base/langflow/services/monitor/service.py @@ -140,7 +140,7 @@ class MonitorService(Service): order: Optional[str] = "DESC", limit: Optional[int] = None, ): - query = "SELECT index, flow_id, sender_name, sender, session_id, text, timestamp FROM messages" + query = "SELECT index, flow_id, sender_name, sender, session_id, text, files, timestamp FROM messages" conditions = [] if sender: conditions.append(f"sender = '{sender}'")