@@ -142,7 +142,7 @@ class StreamContentAccumulator:
142142
143143 def __init__ (self ):
144144 self .base_content = "" # Content before tool calls
145- self .current_content = "" # Current streaming content
145+ self .current_content = [] # Accumulated streaming fragments
146146 self .tool_status_messages = [] # Accumulated tool status messages
147147
148148 def set_base_content (self , content : str ):
@@ -151,7 +151,7 @@ def set_base_content(self, content: str):
151151
152152 def add_streaming_content (self , new_content : str ):
153153 r"""Add new streaming content."""
154- self .current_content += new_content
154+ self .current_content . append ( new_content )
155155
156156 def add_tool_status (self , status_message : str ):
157157 r"""Add a tool status message."""
@@ -160,16 +160,18 @@ def add_tool_status(self, status_message: str):
160160 def get_full_content (self ) -> str :
161161 r"""Get the complete accumulated content."""
162162 tool_messages = "" .join (self .tool_status_messages )
163- return self .base_content + tool_messages + self .current_content
163+ current = "" .join (self .current_content )
164+ return self .base_content + tool_messages + current
164165
165166 def get_content_with_new_status (self , status_message : str ) -> str :
166167 r"""Get content with a new status message appended."""
167168 tool_messages = "" .join ([* self .tool_status_messages , status_message ])
168- return self .base_content + tool_messages + self .current_content
169+ current = "" .join (self .current_content )
170+ return self .base_content + tool_messages + current
169171
170172 def reset_streaming_content (self ):
171173 r"""Reset only the streaming content, keep base and tool status."""
172- self .current_content = ""
174+ self .current_content = []
173175
174176
175177class StreamingChatAgentResponse :
@@ -397,6 +399,10 @@ class ChatAgent(BaseAgent):
397399 step_timeout (Optional[float], optional): Timeout in seconds for the
398400 entire step operation. If None, no timeout is applied.
399401 (default: :obj:`None`)
402+ stream_accumulate (bool, optional): When True, partial streaming
403+ updates return accumulated content (current behavior). When False,
404+ partial updates return only the incremental delta. (default:
405+ :obj:`True`)
400406 """
401407
402408 def __init__ (
@@ -440,6 +446,7 @@ def __init__(
440446 retry_attempts : int = 3 ,
441447 retry_delay : float = 1.0 ,
442448 step_timeout : Optional [float ] = None ,
449+ stream_accumulate : bool = True ,
443450 ) -> None :
444451 if isinstance (model , ModelManager ):
445452 self .model_backend = model
@@ -528,6 +535,7 @@ def __init__(
528535 self .retry_attempts = max (1 , retry_attempts )
529536 self .retry_delay = max (0.0 , retry_delay )
530537 self .step_timeout = step_timeout
538+ self .stream_accumulate = stream_accumulate
531539
532540 def reset (self ):
533541 r"""Resets the :obj:`ChatAgent` to its initial state."""
@@ -3668,15 +3676,18 @@ def _create_streaming_response_with_accumulator(
36683676 ) -> ChatAgentResponse :
36693677 r"""Create a streaming response using content accumulator."""
36703678
3671- # Add new content to accumulator and get full content
3679+ # Add new content; only build full content when needed
36723680 accumulator .add_streaming_content (new_content )
3673- full_content = accumulator .get_full_content ()
3681+ if self .stream_accumulate :
3682+ message_content = accumulator .get_full_content ()
3683+ else :
3684+ message_content = new_content
36743685
36753686 message = BaseMessage (
36763687 role_name = self .role_name ,
36773688 role_type = self .role_type ,
36783689 meta_dict = {},
3679- content = full_content ,
3690+ content = message_content ,
36803691 )
36813692
36823693 return ChatAgentResponse (
@@ -3686,7 +3697,7 @@ def _create_streaming_response_with_accumulator(
36863697 "id" : response_id ,
36873698 "usage" : step_token_usage .copy (),
36883699 "finish_reasons" : ["streaming" ],
3689- "num_tokens" : self ._get_token_count (full_content ),
3700+ "num_tokens" : self ._get_token_count (message_content ),
36903701 "tool_calls" : tool_call_records or [],
36913702 "external_tool_requests" : None ,
36923703 "streaming" : True ,
@@ -3773,6 +3784,7 @@ def clone(self, with_memory: bool = False) -> ChatAgent:
37733784 tool_execution_timeout = self .tool_execution_timeout ,
37743785 pause_event = self .pause_event ,
37753786 prune_tool_calls_from_memory = self .prune_tool_calls_from_memory ,
3787+ stream_accumulate = self .stream_accumulate ,
37763788 )
37773789
37783790 # Copy memory if requested
0 commit comments