@@ -292,6 +292,32 @@ def _sanitize_chat_row(self, chat_item):
292292
293293 return changed
294294
295+ def _normalize_assistant_content_from_output (self , chat : dict , prior_chat : dict ) -> dict :
296+ """Rewrite assistant 'content' from 'output' when 'output' has changed.
297+
298+ Skipping unchanged messages preserves user-typed text on messages
299+ with empty or absent 'output'.
300+ """
301+ from open_webui .utils .middleware import serialize_output
302+
303+ history = (chat or {}).get ('history' ) or {}
304+ messages = history .get ('messages' )
305+ if not isinstance (messages , dict ):
306+ return chat
307+
308+ prior_messages = ((prior_chat or {}).get ('history' ) or {}).get ('messages' ) or {}
309+
310+ for message_id , message in messages .items ():
311+ if not (isinstance (message , dict ) and message .get ('role' ) == 'assistant' ):
312+ continue
313+ new_output = message .get ('output' )
314+ if not isinstance (new_output , list ):
315+ continue
316+ old_output = (prior_messages .get (message_id ) or {}).get ('output' )
317+ if new_output != old_output :
318+ message ['content' ] = serialize_output (new_output )
319+ return chat
320+
295321 async def insert_new_chat (
296322 self , id : str , user_id : str , form_data : ChatForm , db : Optional [AsyncSession ] = None
297323 ) -> Optional [ChatModel ]:
@@ -395,6 +421,7 @@ async def update_chat_by_id(self, id: str, chat: dict, db: Optional[AsyncSession
395421 (chat_item .chat or {}).get ('history' , {}).get ('messages' , {}) or {}
396422 )
397423
424+ chat = self ._normalize_assistant_content_from_output (chat , chat_item .chat )
398425 chat_item .chat = self ._clean_null_bytes (chat )
399426 chat_item .title = self ._clean_null_bytes (chat ['title' ]) if 'title' in chat else 'New Chat'
400427
0 commit comments