From ade1e8a120a4d8a4a5b6bf0b9086e9236088360e Mon Sep 17 00:00:00 2001 From: Yeuoly Date: Thu, 26 Jun 2025 16:49:19 +0800 Subject: [PATCH] refactor: streamline result processing in structured output parser - Converted the result processing logic into a generator function for improved memory efficiency and clarity. - Maintained the existing functionality while enhancing the structure of the code for better readability. --- .../output_parser/structured_output.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/api/core/llm_generator/output_parser/structured_output.py b/api/core/llm_generator/output_parser/structured_output.py index 6dc5ce9fd8..2625aa2d48 100644 --- a/api/core/llm_generator/output_parser/structured_output.py +++ b/api/core/llm_generator/output_parser/structured_output.py @@ -142,13 +142,17 @@ def invoke_llm_with_structured_output( prompt_messages=llm_result.prompt_messages, ) else: - result_text = "" - for event in llm_result: - if isinstance(event, LLMResultChunk): - if isinstance(event.delta.message.content, str): - result_text += event.delta.message.content - yield LLMStructuredOutput(structured_output=_parse_structured_output(result_text)) + def generator(): + result_text = "" + for event in llm_result: + if isinstance(event, LLMResultChunk): + if isinstance(event.delta.message.content, str): + result_text += event.delta.message.content + + yield LLMStructuredOutput(structured_output=_parse_structured_output(result_text)) + + return generator() def _handle_native_json_schema(