@@ -315,7 +315,7 @@ def start_model_invoke_span(
315
315
"gen_ai.system" : "strands-agents" ,
316
316
"agent.name" : agent_name ,
317
317
"gen_ai.agent.name" : agent_name ,
318
- "gen_ai.prompt" : json . dumps (messages , cls = JSONEncoder ),
318
+ "gen_ai.prompt" : serialize (messages ),
319
319
}
320
320
321
321
if model_id :
@@ -338,7 +338,7 @@ def end_model_invoke_span(
338
338
error: Optional exception if the model call failed.
339
339
"""
340
340
attributes : Dict [str , AttributeValue ] = {
341
- "gen_ai.completion" : json . dumps (message ["content" ], cls = JSONEncoder ),
341
+ "gen_ai.completion" : serialize (message ["content" ]),
342
342
"gen_ai.usage.prompt_tokens" : usage ["inputTokens" ],
343
343
"gen_ai.usage.completion_tokens" : usage ["outputTokens" ],
344
344
"gen_ai.usage.total_tokens" : usage ["totalTokens" ],
@@ -360,10 +360,10 @@ def start_tool_call_span(
360
360
The created span, or None if tracing is not enabled.
361
361
"""
362
362
attributes : Dict [str , AttributeValue ] = {
363
- "gen_ai.prompt" : json . dumps (tool , cls = JSONEncoder ),
363
+ "gen_ai.prompt" : serialize (tool ),
364
364
"tool.name" : tool ["name" ],
365
365
"tool.id" : tool ["toolUseId" ],
366
- "tool.parameters" : json . dumps (tool ["input" ], cls = JSONEncoder ),
366
+ "tool.parameters" : serialize (tool ["input" ]),
367
367
}
368
368
369
369
# Add additional kwargs as attributes
@@ -387,7 +387,7 @@ def end_tool_call_span(
387
387
status = tool_result .get ("status" )
388
388
status_str = str (status ) if status is not None else ""
389
389
390
- tool_result_content_json = json . dumps (tool_result .get ("content" ), cls = JSONEncoder )
390
+ tool_result_content_json = serialize (tool_result .get ("content" ))
391
391
attributes .update (
392
392
{
393
393
"tool.result" : tool_result_content_json ,
@@ -420,7 +420,7 @@ def start_event_loop_cycle_span(
420
420
parent_span = parent_span if parent_span else event_loop_kwargs .get ("event_loop_parent_span" )
421
421
422
422
attributes : Dict [str , AttributeValue ] = {
423
- "gen_ai.prompt" : json . dumps (messages , cls = JSONEncoder ),
423
+ "gen_ai.prompt" : serialize (messages ),
424
424
"event_loop.cycle_id" : event_loop_cycle_id ,
425
425
}
426
426
@@ -449,11 +449,11 @@ def end_event_loop_cycle_span(
449
449
error: Optional exception if the cycle failed.
450
450
"""
451
451
attributes : Dict [str , AttributeValue ] = {
452
- "gen_ai.completion" : json . dumps (message ["content" ], cls = JSONEncoder ),
452
+ "gen_ai.completion" : serialize (message ["content" ]),
453
453
}
454
454
455
455
if tool_result_message :
456
- attributes ["tool.result" ] = json . dumps (tool_result_message ["content" ], cls = JSONEncoder )
456
+ attributes ["tool.result" ] = serialize (tool_result_message ["content" ])
457
457
458
458
self ._end_span (span , attributes , error )
459
459
@@ -490,7 +490,7 @@ def start_agent_span(
490
490
attributes ["gen_ai.request.model" ] = model_id
491
491
492
492
if tools :
493
- tools_json = json . dumps (tools , cls = JSONEncoder )
493
+ tools_json = serialize (tools )
494
494
attributes ["agent.tools" ] = tools_json
495
495
attributes ["gen_ai.agent.tools" ] = tools_json
496
496
@@ -571,3 +571,15 @@ def get_tracer(
571
571
)
572
572
573
573
return _tracer_instance
574
+
575
+
576
+ def serialize (obj : Any ) -> str :
577
+ """Serialize an object to JSON with consistent settings.
578
+
579
+ Args:
580
+ obj: The object to serialize
581
+
582
+ Returns:
583
+ JSON string representation of the object
584
+ """
585
+ return json .dumps (obj , ensure_ascii = False , cls = JSONEncoder )
0 commit comments