@@ -334,9 +334,10 @@ def patched_extract_tool_calls(
334334 tool_calls .append (tool_call )
335335 return tool_calls
336336
337- # TODO: The following code is to patch a bedrock bug that was fixed in
337+ # TODO: The following code is to patch bedrock bugs that were fixed in
338338 # opentelemetry-instrumentation-botocore==0.60b0 in:
339339 # https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3875
340+ # https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3990
340341 # Remove this code once we've bumped opentelemetry-instrumentation-botocore dependency to 0.60b0
341342 def patched_process_anthropic_claude_chunk (self , chunk ):
342343 # pylint: disable=too-many-return-statements,too-many-branches
@@ -412,12 +413,30 @@ def patched_process_anthropic_claude_chunk(self, chunk):
412413 self ._stream_done_callback (self ._response )
413414 return
414415
416+ def patched_from_converse (cls , response : dict [str , Any ], capture_content : bool ) -> bedrock_utils ._Choice :
417+ # be defensive about malformed responses, refer to #3958 for more context
418+ output = response .get ("output" , {})
419+ orig_message = output .get ("message" , {})
420+ if role := orig_message .get ("role" ):
421+ message = {"role" : role }
422+ else :
423+ # amazon.titan does not serialize the role
424+ message = {}
425+
426+ if tool_calls := bedrock_utils .extract_tool_calls (orig_message , capture_content ):
427+ message ["tool_calls" ] = tool_calls
428+ elif capture_content :
429+ message ["content" ] = orig_message ["content" ]
430+
431+ return cls (message , response ["stopReason" ], index = 0 )
432+
415433 bedrock_utils .ConverseStreamWrapper .__init__ = patched_init
416434 bedrock_utils .ConverseStreamWrapper ._process_event = patched_process_event
417435 bedrock_utils .InvokeModelWithResponseStreamWrapper ._process_anthropic_claude_chunk = (
418436 patched_process_anthropic_claude_chunk
419437 )
420438 bedrock_utils .extract_tool_calls = patched_extract_tool_calls
439+ bedrock_utils ._Choice .from_converse = classmethod (patched_from_converse )
421440
422441 # END The OpenTelemetry Authors code
423442
0 commit comments