@@ -1981,12 +1981,41 @@ def _cleanup_telemetry(self):
19811981 logging .debug (f"Error cleaning up telemetry: { e } " )
19821982
19831983 def start (self , prompt : str , ** kwargs ):
1984- """Start the agent with a prompt. This is a convenience method that wraps chat()."""
1984+ """Start the agent with a prompt. This is a convenience method that wraps chat().
1985+
1986+ This method is designed to be convenient for simple use cases. By default, it will
1987+ automatically consume the generator when streaming is enabled and return the final
1988+ response, while still displaying the output to the user.
1989+
1990+ For advanced use cases that need the raw generator (e.g., for custom streaming
1991+ handling), use return_generator=True.
1992+
1993+ Args:
1994+ prompt: The prompt to send to the agent
1995+ **kwargs: Additional arguments to pass to the chat method
1996+ - stream: If explicitly set to False, disables streaming
1997+ - return_generator: If True, returns the raw generator for custom handling
1998+
1999+ Returns:
2000+ The final response from the agent (default), or a generator if return_generator=True
2001+ """
19852002 try :
2003+ # Check if user explicitly wants the raw generator for custom handling
2004+ return_generator = kwargs .pop ('return_generator' , False )
2005+
19862006 # Check if streaming is enabled and user wants streaming chunks
19872007 if self .stream and kwargs .get ('stream' , True ):
19882008 result = self ._start_stream (prompt , ** kwargs )
1989- return result
2009+
2010+ if return_generator :
2011+ # Return the raw generator for advanced users
2012+ return result
2013+ else :
2014+ # Auto-consume the generator for convenience while preserving display
2015+ final_response = None
2016+ for chunk in result :
2017+ final_response = chunk # Last chunk is typically the final response
2018+ return final_response
19902019 else :
19912020 result = self .chat (prompt , ** kwargs )
19922021 return result
0 commit comments