Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions apps/application/workflow/nodes/ai_chat_node/ai_chat_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ def execute(self):
question.content,
reasoning_content_id,
text_content_id,
is_result=is_result,
)
)
else:
Expand Down Expand Up @@ -467,7 +468,7 @@ def _generate_prompt_question(self, question_str, model, vision, image_list, vid
return HumanMessage(content=[*videos, *images, {"type": "text", "text": question_str}])

async def _stream_response(
self, response, chat_model, message_list, question, reasoning_content_id, text_content_id
self, response, chat_model, message_list, question, reasoning_content_id, text_content_id, is_result=None
):

reasoning_setting = {"reasoning_end": False, "is_reasoning": False}
Expand Down Expand Up @@ -503,11 +504,12 @@ async def _stream_response(
reasoning_content_id, "", Status.SUCCESS, node_info, Position(self.get_node_id())
)
)
self.write(
TextContent(
text_content_id, content_chunk, Status.RUNNING, node_info, Position(self.get_node_id())
if is_result:
self.write(
TextContent(
text_content_id, content_chunk, Status.RUNNING, node_info, Position(self.get_node_id())
)
)
)
if reasoning_content_chunk and model_setting.get("reasoning_content_enable", False):
reasoning_setting["is_reasoning"] = True
self.write(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review Analysis

What the Change Does

The patch adds an is_result parameter to _stream_response, which when falsy, skips writing streaming text chunks to the output (but still writes reasoning content).

Potential Issues

1. Logic seems inverted or unclear

  • The flag name is_result suggests "this is a final result," yet when it's falsy you suppress content. If intermediate/tool-call steps shouldn't stream text, a clearer name like write_text_content=True (a positive flag controlling the action) would be more readable.

2. Silent data loss / inconsistency

  • When is_result is falsy, text chunks are dropped entirely — they're neither written nor stored elsewhere. If downstream consumers expect the full transcript, this creates inconsistent output between contactenated and streamed paths. Consider whether the text should be accumulated and written at the end instead of discarded.
  • Also consider: only the chunk write is guarded. If there's a later "final write" of the full text elsewhere in the function (not shown in the diff), that path may still execute — verify behavior is consistent.

3. Mutable default pattern is fine here, but...

  • is_result=None used as a boolean is acceptable, but is_result=False as default is simpler and more explicit if it's purely a flag.

4. Duplication

  • The repeated Position(self.get_node_id()) calls add noise throughout _stream_response. Several instances appearing in sequence suggests refactoring opportunity (precompute once per call if the node doesn't change mid-stream).

Optimization Suggestions

  • Early branching: Instead of checking if is_result: inside the loop on every chunk, branch once before the streaming loop and use two slightly different write lambdas/helpers. This reduces per-chunk conditionals:
text_writer = self._make_text_writer(text_content_id, node_info) if is_result else None
...
if text_writer:
    self.write(text_writer(content_chunk))
  • Naming: Rename to stream_text: bool = True so call sites read naturally (stream_text=False for silent intermediate stages).

  • Testing: Add tests covering both branches — particularly that reasoning content is still streamed when is_result is false, since that asymmetry is easy to break accidentally.

Summary

Functionally the patch looks safe, but verify: (a) discarding text intentionally vs. needing accumulation, (b) no other writes to text_content_id later bypass the flag, and (c) rename the flag to something positively-phrased for clarity.

Expand Down
Loading