@@ -79,13 +79,6 @@ def _convert_internal_format(self, messages: List[Dict[str, Any]]):
7979 thinking_block = None
8080 for i , msg in enumerate (messages ):
8181 msg .pop ("agent" , None )
82- if "tool_calls" in msg and msg .get ("tool_calls" , []):
83- for tool_call in msg ["tool_calls" ]:
84- tool_call ["function" ] = {}
85- tool_call ["function" ]["name" ] = tool_call .pop ("name" , "" )
86- tool_call ["function" ]["arguments" ] = json .dumps (
87- tool_call .pop ("arguments" , {})
88- )
8982 if msg .get ("role" ) == "assistant" :
9083 if thinking_block :
9184 msg ["reasoning_text" ] = thinking_block .get ("thinking" , "" )
@@ -103,6 +96,14 @@ def _convert_internal_format(self, messages: List[Dict[str, Any]]):
10396 )
10497 msg ["content" ] = []
10598
99+ if "tool_calls" in msg and msg .get ("tool_calls" , []):
100+ for tool_call in msg ["tool_calls" ]:
101+ tool_call ["function" ] = {}
102+ tool_call ["function" ]["name" ] = tool_call .pop ("name" , "" )
103+ tool_call ["function" ]["arguments" ] = json .dumps (
104+ tool_call .pop ("arguments" , {})
105+ )
106+
106107 if msg .get ("role" ) == "tool" :
107108 # Special treatment for GitHub Copilot GPT-4.1 model
108109 # At the the time of writing, GitHub Copilot GPT-4.1 model cannot read tool results with array content
@@ -174,29 +175,32 @@ def _process_stream_chunk(
174175 thinking_content = None # OpenAI doesn't support thinking mode
175176 thinking_signature = None
176177
178+ if (not chunk .choices ) or (len (chunk .choices ) == 0 ):
179+ return (
180+ assistant_response or " " ,
181+ tool_uses ,
182+ input_tokens ,
183+ output_tokens ,
184+ "" ,
185+ (thinking_content , None ) if thinking_content else None ,
186+ )
187+
188+ delta_chunk = chunk .choices [0 ].delta
189+
177190 # Handle thinking content
178191 if (
179- chunk .choices
180- and len (chunk .choices ) > 0
181- and hasattr (chunk .choices [0 ].delta , "reasoning_text" )
182- and chunk .choices [0 ].delta .reasoning_text is not None
192+ hasattr (delta_chunk , "reasoning_text" )
193+ and delta_chunk .reasoning_text is not None
183194 ):
184- thinking_content = chunk . choices [ 0 ]. delta .reasoning_text
195+ thinking_content = delta_chunk .reasoning_text
185196
186197 if (
187- chunk .choices
188- and len (chunk .choices ) > 0
189- and hasattr (chunk .choices [0 ].delta , "reasoning_opaque" )
190- and chunk .choices [0 ].delta .reasoning_opaque is not None
198+ hasattr (delta_chunk , "reasoning_opaque" )
199+ and delta_chunk .reasoning_opaque is not None
191200 ):
192- thinking_signature = chunk . choices [ 0 ]. delta .reasoning_opaque
201+ thinking_signature = delta_chunk .reasoning_opaque
193202 # Handle regular content chunks
194- if (
195- chunk .choices
196- and len (chunk .choices ) > 0
197- and hasattr (chunk .choices [0 ].delta , "content" )
198- and chunk .choices [0 ].delta .content is not None
199- ):
203+ if hasattr (delta_chunk , "content" ) and delta_chunk .content is not None :
200204 chunk_text = chunk .choices [0 ].delta .content
201205 assistant_response += chunk_text
202206
@@ -208,11 +212,7 @@ def _process_stream_chunk(
208212 output_tokens = chunk .usage .completion_tokens
209213
210214 # Handle tool call chunks
211- if (
212- chunk .choices
213- and len (chunk .choices ) > 0
214- and hasattr (chunk .choices [0 ].delta , "tool_calls" )
215- ):
215+ if hasattr (delta_chunk , "tool_calls" ):
216216 delta_tool_calls = chunk .choices [0 ].delta .tool_calls
217217 if delta_tool_calls :
218218 # Process each tool call in the delta
0 commit comments