Skip to content

Commit 429f624

Browse files
committed
add defensive check for from_converse response
1 parent 77170ea commit 429f624

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

instrumentation/opentelemetry-instrumentation-botocore/src/opentelemetry/instrumentation/botocore/extensions/bedrock_utils.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,8 @@ def __init__(
528528
def from_converse(
529529
cls, response: dict[str, Any], capture_content: bool
530530
) -> _Choice:
531-
orig_message = response["output"]["message"]
531+
output = response.get("output", {})
532+
orig_message = output.get("message", {})
532533
if role := orig_message.get("role"):
533534
message = {"role": role}
534535
else:

instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_bedrock.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from botocore.response import StreamingBody
2727

2828
from opentelemetry.instrumentation.botocore.extensions.bedrock_utils import (
29+
ConverseStreamWrapper,
2930
InvokeModelWithResponseStreamWrapper,
3031
)
3132
from opentelemetry.semconv._incubating.attributes.error_attributes import (
@@ -3049,7 +3050,21 @@ def stream_error_callback(exc, ended):
30493050
assert isinstance(tool_block["input"], dict)
30503051
else:
30513052
assert "input" not in tool_block
3052-
3053+
def test_converse_stream_with_malformed_response():
3054+
"""Test that converse stream handles malformed response missing output key."""
3055+
def stream_done_callback(response, ended):
3056+
pass
3057+
3058+
wrapper = ConverseStreamWrapper(
3059+
stream=mock.MagicMock(),
3060+
stream_done_callback=stream_done_callback,
3061+
model_id="amazon.nova-micro-v1:0",
3062+
)
3063+
3064+
malformed_response = {"stopReason": "end_turn"}
3065+
result = wrapper._complete_stream(malformed_response)
3066+
assert result is None
3067+
30533068

30543069
def amazon_nova_messages():
30553070
return [

0 commit comments

Comments
 (0)