From 95fad5d5234f69bc4c850413be1b3cba130b0ce3 Mon Sep 17 00:00:00 2001 From: Billy Bao Date: Wed, 29 Oct 2025 09:44:02 +0800 Subject: [PATCH] Fix: Chat session completion (#10851) ### What problem does this PR solve? Fix: Chat session completion #10791 ### Type of change - [X] Bug Fix (non-breaking change which fixes an issue) --- sdk/python/ragflow_sdk/modules/session.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/sdk/python/ragflow_sdk/modules/session.py b/sdk/python/ragflow_sdk/modules/session.py index 30d7aeea3..9a54eb5e4 100644 --- a/sdk/python/ragflow_sdk/modules/session.py +++ b/sdk/python/ragflow_sdk/modules/session.py @@ -15,7 +15,6 @@ # import json - from .base import Base @@ -51,7 +50,6 @@ class Session(Base): if not line: continue # Skip empty lines line = line.strip() - if line.startswith("data:"): content = line[len("data:"):].strip() if content == "[DONE]": @@ -64,21 +62,26 @@ class Session(Base): except json.JSONDecodeError: continue # Skip lines that are not valid JSON - event = json_data.get("event") - if event == "message": - yield self._structure_answer(json_data) - elif event == "message_end": - return # End of message stream + if ( + (self.__session_type == "agent" and json_data.get("event") == "message_end") + or (self.__session_type == "chat" and json_data.get("data") is True) + ): + return + + yield self._structure_answer(json_data) else: try: json_data = res.json() except ValueError: raise Exception(f"Invalid response {res}") - yield self._structure_answer(json_data["data"]) + yield self._structure_answer(json_data) def _structure_answer(self, json_data): - answer = json_data["data"]["content"] + if self.__session_type == "agent": + answer = json_data["data"]["data"]["content"] + elif self.__session_type == "chat": + answer =json_data["data"]["answer"] reference = json_data["data"].get("reference", {}) temp_dict = { "content": answer,