@@ -723,24 +723,22 @@ async def _map_messages(
723
723
else :
724
724
assert_never (part )
725
725
elif isinstance (message , ModelResponse ):
726
- thinking_parts : list [ThinkingPart ] = []
727
726
for item in message .parts :
728
727
if isinstance (item , TextPart ):
729
728
openai_messages .append (responses .EasyInputMessageParam (role = 'assistant' , content = item .content ))
730
729
elif isinstance (item , ToolCallPart ):
731
730
openai_messages .append (self ._map_tool_call (item ))
732
731
elif isinstance (item , ThinkingPart ):
733
- thinking_parts .append (item )
732
+ # Reasoning needs to precede the tool call, it shouldn't get added last
733
+ openai_messages .append (
734
+ responses .ResponseReasoningItemParam (
735
+ id = item .signature or '' ,
736
+ summary = [Summary (text = item .content , type = 'summary_text' )],
737
+ type = 'reasoning' ,
738
+ )
739
+ )
734
740
else :
735
741
assert_never (item )
736
- if thinking_parts :
737
- openai_messages .append (
738
- responses .ResponseReasoningItemParam (
739
- id = thinking_parts [0 ].signature or '' ,
740
- summary = [Summary (text = item .content , type = 'summary_text' ) for item in thinking_parts ],
741
- type = 'reasoning' ,
742
- )
743
- )
744
742
else :
745
743
assert_never (message )
746
744
instructions = self ._get_instructions (messages ) or NOT_GIVEN
@@ -749,6 +747,7 @@ async def _map_messages(
749
747
@staticmethod
750
748
def _map_tool_call (t : ToolCallPart ) -> responses .ResponseFunctionToolCallParam :
751
749
return responses .ResponseFunctionToolCallParam (
750
+ id = t .id or '' ,
752
751
arguments = t .args_as_json_str (),
753
752
call_id = _guard_tool_call_id (t = t ),
754
753
name = t .tool_name ,
@@ -892,6 +891,15 @@ async def _get_event_iterator(self) -> AsyncIterator[ModelResponseStreamEvent]:
892
891
elif isinstance (chunk , responses .ResponseContentPartDoneEvent ):
893
892
pass # there's nothing we need to do here
894
893
894
+ elif isinstance (chunk , responses .ResponseReasoningSummaryPartAddedEvent ):
895
+ pass # there's nothing we need to do here
896
+
897
+ elif isinstance (chunk , responses .ResponseReasoningSummaryPartDoneEvent ):
898
+ pass # there's nothing we need to do here
899
+
900
+ elif isinstance (chunk , responses .ResponseReasoningSummaryTextDoneEvent ):
901
+ pass # there's nothing we need to do here
902
+
895
903
elif isinstance (chunk , responses .ResponseCreatedEvent ):
896
904
pass # there's nothing we need to do here
897
905
@@ -923,11 +931,15 @@ async def _get_event_iterator(self) -> AsyncIterator[ModelResponseStreamEvent]:
923
931
vendor_part_id = chunk .item .id ,
924
932
tool_name = chunk .item .name ,
925
933
args = chunk .item .arguments ,
926
- tool_call_id = chunk .item .id ,
934
+ tool_call_id = chunk .item .call_id ,
927
935
)
928
936
elif isinstance (chunk .item , responses .ResponseReasoningItem ):
929
937
content = chunk .item .summary [0 ].text if chunk .item .summary else ''
930
- yield self ._parts_manager .handle_thinking_delta (vendor_part_id = chunk .item .id , content = content )
938
+ yield self ._parts_manager .handle_thinking_delta (
939
+ vendor_part_id = chunk .item .id ,
940
+ content = content ,
941
+ signature = chunk .item .id ,
942
+ )
931
943
elif isinstance (chunk .item , responses .ResponseOutputMessage ):
932
944
pass
933
945
else :
@@ -940,6 +952,13 @@ async def _get_event_iterator(self) -> AsyncIterator[ModelResponseStreamEvent]:
940
952
elif isinstance (chunk , responses .ResponseTextDeltaEvent ):
941
953
yield self ._parts_manager .handle_text_delta (vendor_part_id = chunk .content_index , content = chunk .delta )
942
954
955
+ elif isinstance (chunk , responses .ResponseReasoningSummaryTextDeltaEvent ):
956
+ yield self ._parts_manager .handle_thinking_delta (
957
+ vendor_part_id = chunk .item_id ,
958
+ content = chunk .delta ,
959
+ signature = chunk .item_id ,
960
+ )
961
+
943
962
elif isinstance (chunk , responses .ResponseTextDoneEvent ):
944
963
pass # there's nothing we need to do here
945
964
0 commit comments