@@ -236,13 +236,81 @@ async def extract_memories_from_session_thread(
236236 )
237237 return []
238238
239- extraction_result = json .loads (content )
240- memories_data = extraction_result .get ("memories" , [])
239+ # Try to parse JSON with fallback for malformed responses
240+ try :
241+ extraction_result = json .loads (content )
242+ memories_data = extraction_result .get ("memories" , [])
243+ except json .JSONDecodeError :
244+ # Attempt to repair common JSON issues
245+ logger .warning (
246+ f"Initial JSON parsing failed, attempting repair on content: { content [:500 ]} ..."
247+ )
248+
249+ # Try to extract just the memories array if it exists
250+ import re
251+
252+ # Look for memories array in the response
253+ memories_match = re .search (
254+ r'"memories"\s*:\s*\[(.*?)\]' , content , re .DOTALL
255+ )
256+ if memories_match :
257+ try :
258+ # Try to reconstruct a valid JSON object
259+ memories_json = (
260+ '{"memories": [' + memories_match .group (1 ) + "]}"
261+ )
262+ extraction_result = json .loads (memories_json )
263+ memories_data = extraction_result .get ("memories" , [])
264+ logger .info ("Successfully repaired malformed JSON response" )
265+ except json .JSONDecodeError :
266+ logger .error ("JSON repair attempt failed" )
267+ raise
268+ else :
269+ logger .error ("Could not find memories array in malformed response" )
270+ raise
241271 except (json .JSONDecodeError , AttributeError , TypeError ) as e :
242272 logger .error (
243273 f"Failed to parse extraction response: { e } , response: { response } "
244274 )
245- return []
275+
276+ # Log the content for debugging
277+ if hasattr (response , "choices" ) and response .choices :
278+ content = getattr (response .choices [0 ].message , "content" , "No content" )
279+ logger .error (
280+ f"Problematic content (first 1000 chars): { content [:1000 ]} "
281+ )
282+
283+ # For test stability, retry once with a simpler prompt
284+ logger .info ("Attempting retry with simplified extraction" )
285+ try :
286+ simple_response = await client .create_chat_completion (
287+ model = settings .generation_model ,
288+ prompt = f"""Extract key information from this conversation and format as JSON:
289+ { full_conversation }
290+
291+ Return in this exact format:
292+ {{"memories": [{{"type": "episodic", "text": "extracted information", "topics": ["topic1"], "entities": ["entity1"]}}]}}""" ,
293+ response_format = {"type" : "json_object" },
294+ )
295+
296+ if (
297+ hasattr (simple_response , "choices" )
298+ and simple_response .choices
299+ and hasattr (simple_response .choices [0 ].message , "content" )
300+ ):
301+ retry_content = simple_response .choices [0 ].message .content
302+ retry_result = json .loads (retry_content )
303+ memories_data = retry_result .get ("memories" , [])
304+ logger .info (
305+ f"Retry extraction succeeded with { len (memories_data )} memories"
306+ )
307+ else :
308+ logger .error ("Retry extraction failed - no valid response" )
309+ return []
310+
311+ except Exception as retry_error :
312+ logger .error (f"Retry extraction failed: { retry_error } " )
313+ return []
246314
247315 logger .info (
248316 f"Extracted { len (memories_data )} memories from session thread { session_id } "
0 commit comments