@@ -37,26 +37,34 @@ def normalize_to_pil_images(
3737 return [image ]
3838
3939
40- def truncate_content (
41- content : Any ,
42- max_string_length : int = 100000 ,
43- ) -> Any :
44- """Filter out long strings (i.e. the base64 image data) to keep reports readable."""
45- if isinstance ( content , str ):
46- if len ( content ) > max_string_length :
47- return f"[truncated: { len ( content ) } characters]"
48- return content
49-
40+ def truncate_base64_images ( content : Any ) -> Any :
41+ """Replace base64 image data with a placeholder to keep reports readable.
42+
43+ Walks the message content recursively and replaces the ``data`` field of
44+ any base64 image source block (matching the schema of
45+ ``Base64ImageSourceParam``, i.e. ``{"type": "base64", "data": "...",
46+ "media_type": "image/..."}``) with a placeholder string. All other
47+ content (including regular long strings like prompts or tool outputs)
48+ is left untouched.
49+ """
5050 if isinstance (content , dict ):
51+ content_dict : dict [Any , Any ] = content
52+ media_type = content_dict .get ("media_type" )
53+ if (
54+ isinstance (media_type , str )
55+ and media_type .startswith ("image/" )
56+ and content_dict .get ("type" ) == "base64"
57+ and "data" in content_dict
58+ ):
59+ return {** content_dict , "data" : "[Base64 image data truncated]" }
5160 return {
52- key : truncate_content (value , max_string_length )
53- for key , value in content .items ()
61+ key : truncate_base64_images (value ) for key , value in content_dict .items ()
5462 }
5563
5664 if isinstance (content , list ):
57- return [truncate_content (item , max_string_length ) for item in content ]
65+ content_list : list [Any ] = content
66+ return [truncate_base64_images (item ) for item in content_list ]
5867
59- # For other types (int, float, bool, None), return as-is
6068 return content
6169
6270
@@ -257,7 +265,7 @@ def add_message(
257265 self ._start_time = datetime .now (tz = timezone .utc )
258266
259267 _images = normalize_to_pil_images (image )
260- _content = truncate_content (content )
268+ _content = truncate_base64_images (content )
261269
262270 message = {
263271 "timestamp" : datetime .now (tz = timezone .utc ),
0 commit comments