@@ -113,7 +113,7 @@ def default(self, o: Any) -> Any:
113113 return super ().default (o )
114114
115115
116- def _convert_any_value_to_string (value : Any , debug_location : str ) -> str :
116+ def _convert_any_value_to_string (value : Any ) -> str :
117117 if isinstance (value , bool ):
118118 return "true" if value else "false"
119119 if isinstance (value , bytes ):
@@ -122,12 +122,14 @@ def _convert_any_value_to_string(value: Any, debug_location: str) -> str:
122122 return str (value )
123123 if isinstance (value , (list , tuple , Mapping )):
124124 return json .dumps (value , separators = ("," , ":" ), cls = _GenAiJsonEncoder )
125- logging .warning (
126- "Unexpected type %s found in %s, this field will not be added to the LogEntry." ,
127- type (value ),
128- debug_location ,
129- )
130- return ""
125+ try :
126+ return str (value )
127+ except Exception as exc : # pylint: disable=broad-except
128+ logging .exception (
129+ "Error mapping AnyValue to string, this field will not be added to the LogEntry: %s" ,
130+ exc ,
131+ )
132+ return ""
131133
132134
133135# Be careful not to mutate original body. Make copies of anything that needs to change.
@@ -189,7 +191,7 @@ def _set_payload_in_log_entry(log_entry: LogEntry, body: AnyValue):
189191 log_entry .text_payload = base64 .b64encode (body ).decode ()
190192 elif body is not None :
191193 log_entry .text_payload = _convert_any_value_to_string (
192- body , "LogRecord.body"
194+ body
193195 )
194196
195197
@@ -279,7 +281,7 @@ def export(self, batch: Sequence[LogData]):
279281 ]
280282 log_entry .labels = {
281283 k : _convert_any_value_to_string (
282- v , "LogRecord.Attribute {}" . format ( k )
284+ v
283285 )
284286 for k , v in attributes .items ()
285287 }
0 commit comments