@@ -4289,12 +4289,22 @@ def _stream_response(
42894289 return
42904290
42914291 # Handle streaming response
4292- if isinstance (response , Stream ) or inspect .isgenerator (response ):
4292+ # Check for Stream, generator, or third-party wrappers
4293+ if (
4294+ isinstance (response , Stream )
4295+ or inspect .isgenerator (response )
4296+ or (
4297+ hasattr (response , '__iter__' )
4298+ and hasattr (response , '__enter__' )
4299+ and not hasattr (response , 'get_final_completion' )
4300+ and not isinstance (response , ChatCompletion )
4301+ )
4302+ ):
42934303 (
42944304 stream_completed ,
42954305 tool_calls_complete ,
42964306 ) = yield from self ._process_stream_chunks_with_accumulator (
4297- response ,
4307+ response , # type: ignore[arg-type]
42984308 content_accumulator ,
42994309 accumulated_tool_calls ,
43004310 tool_call_records ,
@@ -4333,11 +4343,9 @@ def _stream_response(
43334343 # Stream completed without tool calls
43344344 accumulated_tool_calls .clear ()
43354345 break
4336- elif hasattr (response , '__enter__' ) and hasattr (
4337- response , '__exit__'
4338- ):
4346+ elif hasattr (response , 'get_final_completion' ):
43394347 # Handle structured output stream (ChatCompletionStreamManager)
4340- with response as stream :
4348+ with response as stream : # type: ignore[union-attr]
43414349 parsed_object = None
43424350
43434351 for event in stream :
@@ -4426,7 +4434,9 @@ def _stream_response(
44264434 return
44274435 else :
44284436 # Handle non-streaming response (fallback)
4429- model_response = self ._handle_batch_response (response )
4437+ model_response = self ._handle_batch_response (
4438+ response # type: ignore[arg-type]
4439+ )
44304440 yield self ._convert_to_chatagent_response (
44314441 model_response ,
44324442 tool_call_records ,
@@ -5098,11 +5108,16 @@ async def _astream_response(
50985108 return
50995109
51005110 # Handle streaming response
5101- # Note: Also check for async generators since some model backends
5102- # (e.g., GeminiModel) wrap AsyncStream in async generators for
5103- # additional processing
5104- if isinstance (response , AsyncStream ) or inspect .isasyncgen (
5105- response
5111+ # Check for AsyncStream, async generator, or third-party wrappers
5112+ if (
5113+ isinstance (response , AsyncStream )
5114+ or inspect .isasyncgen (response )
5115+ or (
5116+ hasattr (response , '__aiter__' )
5117+ and hasattr (response , '__aenter__' )
5118+ and not hasattr (response , 'get_final_completion' )
5119+ and not isinstance (response , ChatCompletion )
5120+ )
51065121 ):
51075122 stream_completed = False
51085123 tool_calls_complete = False
@@ -5111,7 +5126,7 @@ async def _astream_response(
51115126 async for (
51125127 item
51135128 ) in self ._aprocess_stream_chunks_with_accumulator (
5114- response ,
5129+ response , # type: ignore[arg-type]
51155130 content_accumulator ,
51165131 accumulated_tool_calls ,
51175132 tool_call_records ,
@@ -5158,12 +5173,10 @@ async def _astream_response(
51585173 # Stream completed without tool calls
51595174 accumulated_tool_calls .clear ()
51605175 break
5161- elif hasattr (response , '__aenter__' ) and hasattr (
5162- response , '__aexit__'
5163- ):
5176+ elif hasattr (response , 'get_final_completion' ):
51645177 # Handle structured output stream
51655178 # (AsyncChatCompletionStreamManager)
5166- async with response as stream :
5179+ async with response as stream : # type: ignore[union-attr]
51675180 parsed_object = None
51685181
51695182 async for event in stream :
@@ -5254,7 +5267,9 @@ async def _astream_response(
52545267 return
52555268 else :
52565269 # Handle non-streaming response (fallback)
5257- model_response = self ._handle_batch_response (response )
5270+ model_response = self ._handle_batch_response (
5271+ response # type: ignore[arg-type]
5272+ )
52585273 yield self ._convert_to_chatagent_response (
52595274 model_response ,
52605275 tool_call_records ,
0 commit comments