@@ -73,6 +73,25 @@ async def on_user_message_callback(
7373
7474 return None
7575
76+
77+ def replace_bytes_with_empty (data ):
78+ """
79+ Recursively traverse the data structure and replace all bytes types with empty strings.
80+ Supports handling any nested structure of lists and dictionaries.
81+ """
82+ if isinstance (data , dict ):
83+ # Handle dictionary: Recursively process each value
84+ return {k : replace_bytes_with_empty (v ) for k , v in data .items ()}
85+ elif isinstance (data , list ):
86+ # Handle list: Recursively process each element
87+ return [replace_bytes_with_empty (item ) for item in data ]
88+ elif isinstance (data , bytes ):
89+ # When encountering the bytes type, replace it with an empty string
90+ return "<image data>"
91+ else :
92+ # Keep other types unchanged
93+ return data
94+
7695
7796class BaseTracer (ABC ):
7897 def __init__ (self , name : str ):
@@ -164,8 +183,11 @@ def tracer_hook_after_model(
164183 role = getattr (user_content , "role" , None )
165184
166185 if user_content and getattr (user_content , "parts" , None ):
186+ # content = user_content.model_dump_json(exclude_none=True)
167187 content = user_content .model_dump (exclude_none = True ).get ("parts" , None )
168- content = json .dumps (content ) if content else None
188+ if content :
189+ content = replace_bytes_with_empty (content )
190+ content = json .dumps (content , ensure_ascii = False ) if content else None
169191
170192 if role and content :
171193 attributes ["gen_ai.prompt.0.role" ] = role
0 commit comments