@@ -93,8 +93,7 @@ def _set_agent_input_attribute(
9393 user_content = invocation_context .user_content
9494 if user_content and user_content .parts :
9595 # set gen_ai.input attribute required by APMPlus
96- # TODO: 优化 gen_ai.input,目前无法序列化
97- span .set_attribute ("gen_ai.input" , safe_json_serialize (user_content .parts ))
96+ span .set_attribute ("gen_ai.input" , user_content .model_dump (exclude_none = True ))
9897
9998 span .add_event (
10099 "gen_ai.user.message" ,
@@ -132,15 +131,9 @@ def _set_agent_input_attribute(
132131
133132def _set_agent_output_attribute (span : Span , llm_response : LlmResponse ) -> None :
134133 content = llm_response .content
135- print (f"### response: { llm_response } " )
136- print (f"### output_transcription: { llm_response .output_transcription } " )
137- print (f"### input_transcription: { llm_response .input_transcription } " )
138-
139-
140134 if content and content .parts :
141135 # set gen_ai.output attribute required by APMPlus
142- span .set_attribute ("gen_ai.output" , safe_json_serialize (content .parts ))
143- print ("### parts: " , content .parts )
136+ span .set_attribute ("gen_ai.output" , content .model_dump (exclude_none = True ))
144137 for idx , part in enumerate (content .parts ):
145138 if part .text :
146139 span .add_event (
@@ -163,7 +156,7 @@ def set_common_attributes_on_model_span(
163156 invocation_span : Span = get_value ("invocation_span_instance" ) # type: ignore
164157 agent_run_span : Span = get_value ("agent_run_span_instance" ) # type: ignore
165158
166- if invocation_span and ( invocation_span .name .startswith ("invocation" ) or invocation_span . name . startswith ( "invoke" ) ):
159+ if invocation_span and invocation_span .name .startswith ("invocation" ):
167160 _set_agent_input_attribute (invocation_span , invocation_context )
168161 _set_agent_output_attribute (invocation_span , llm_response )
169162 for attr_name , attr_extractor in common_attributes .items ():
@@ -190,7 +183,7 @@ def set_common_attributes_on_model_span(
190183 "gen_ai.usage.total_tokens" , accumulated_total_token_usage
191184 )
192185
193- if agent_run_span and agent_run_span .name .startswith ("agent_run" ):
186+ if agent_run_span and ( agent_run_span .name .startswith ("agent_run" ) or agent_run_span . name . startswith ( "invoke_agent" ) ):
194187 _set_agent_input_attribute (agent_run_span , invocation_context )
195188 _set_agent_output_attribute (agent_run_span , llm_response )
196189 for attr_name , attr_extractor in common_attributes .items ():
0 commit comments