From 9e74de9b4fae5ca403fa588d21efa73a4dd5cc42 Mon Sep 17 00:00:00 2001 From: Debanjum Date: Mon, 18 Nov 2024 04:26:27 -0800 Subject: [PATCH] Improve serializing conversation JSON to print messages on console - Handle chatml message.content with non-json serializable data like WebP image binary data used by Gemini models --- src/khoj/processor/conversation/utils.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/khoj/processor/conversation/utils.py b/src/khoj/processor/conversation/utils.py index 6de921b1..73ef0d95 100644 --- a/src/khoj/processor/conversation/utils.py +++ b/src/khoj/processor/conversation/utils.py @@ -740,6 +740,20 @@ Metadata def messages_to_print(messages: list[ChatMessage], max_length: int = 70) -> str: """ - Format, truncate messages to print + Format and truncate messages to print, ensuring JSON serializable content """ - return "\n".join([f"{json.dumps(message.content)[:max_length]}..." for message in messages]) + + def safe_serialize(content: Any) -> str: + try: + # Try JSON serialization + json.dumps(content) + return content + except (TypeError, json.JSONDecodeError): + # Handle non-serializable types + if hasattr(content, "format") and content.format == "WEBP": + return "[WebP Image]" + elif hasattr(content, "__dict__"): + return str(content.__dict__) + return str(content) + + return "\n".join([f"{json.dumps(safe_serialize(message.content))[:max_length]}..." for message in messages])