@@ -737,22 +737,40 @@ async def _call_function_tool(
737
737
{
738
738
'type' : 'object' ,
739
739
'properties' : {
740
- ** ({'tool_arguments' : {'type' : 'object' }} if include_content else {}),
740
+ ** (
741
+ {
742
+ 'tool_arguments' : {'type' : 'object' },
743
+ 'tool_response' : {'type' : 'object' },
744
+ }
745
+ if include_content
746
+ else {}
747
+ ),
741
748
'gen_ai.tool.name' : {},
742
749
'gen_ai.tool.call.id' : {},
743
750
},
744
751
}
745
752
),
746
753
}
747
754
748
- with tracer .start_as_current_span ('running tool' , attributes = span_attributes ):
755
+ with tracer .start_as_current_span ('running tool' , attributes = span_attributes ) as span :
749
756
try :
750
757
tool_result = await _call_tool (toolset , tool_call , run_context )
751
758
except ToolRetryError as e :
759
+ part = e .tool_retry
760
+ if include_content and span .is_recording ():
761
+ span .set_attribute ('tool_response' , part .model_response ())
752
762
return (e .tool_retry , [])
753
763
764
+ part = _messages .ToolReturnPart (
765
+ tool_name = tool_call .tool_name ,
766
+ content = tool_result ,
767
+ tool_call_id = tool_call .tool_call_id ,
768
+ )
769
+
770
+ if include_content and span .is_recording ():
771
+ span .set_attribute ('tool_response' , part .model_response_str ())
772
+
754
773
extra_parts : list [_messages .ModelRequestPart ] = []
755
- metadata = None
756
774
757
775
def process_content (content : Any ) -> Any :
758
776
if isinstance (content , _messages .ToolReturn ):
@@ -790,27 +808,20 @@ def process_content(content: Any) -> Any:
790
808
f'Please use `content` instead.'
791
809
)
792
810
793
- metadata = tool_result .metadata
811
+ part .content = tool_result .return_value # type: ignore
812
+ part .metadata = tool_result .metadata
794
813
if tool_result .content :
795
814
extra_parts .append (
796
815
_messages .UserPromptPart (
797
816
content = list (tool_result .content ),
798
817
part_kind = 'user-prompt' ,
799
818
)
800
819
)
801
- tool_result = tool_result .return_value # type: ignore
802
820
elif isinstance (tool_result , list ):
803
821
contents = cast (list [Any ], tool_result )
804
- tool_result = [process_content (content ) for content in contents ]
822
+ part . content = [process_content (content ) for content in contents ]
805
823
else :
806
- tool_result = process_content (tool_result )
807
-
808
- part = _messages .ToolReturnPart (
809
- tool_name = tool_call .tool_name ,
810
- content = tool_result ,
811
- metadata = metadata ,
812
- tool_call_id = tool_call .tool_call_id ,
813
- )
824
+ part .content = process_content (tool_result )
814
825
815
826
return (part , extra_parts )
816
827
0 commit comments