Skip to content

Commit 81e76ad

Browse files
fix(boto3): Skip malformed Bedrock stream chunks and gate stream outputs once
Malformed InvokeModelWithResponseStream chunks are skipped quietly instead of reporting an SDK internal error, like the non-streamed response path. The buffered stream content is already gated on recording outputs, so serialize it directly rather than checking the gate a second time. That makes the buffering gate the only gate, so tests cover it. Co-Authored-By: Sergiy Dybskiy <3760543+sergical@users.noreply.github.com>
1 parent 0dae13b commit 81e76ad

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

‎sentry_sdk/integrations/boto3/_services/bedrock_runtime.py‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -811,8 +811,12 @@ def get_attributes(self) -> "Attributes":
811811
tool_call["arguments"] = content
812812
parts.append(tool_call)
813813

814+
# `record_outputs` already gated the buffered content blocks.
814815
if parts:
815-
attributes.update(_get_output_attributes([_to_output_message(parts)]))
816+
with capture_internal_exceptions():
817+
attributes[SPANDATA.GEN_AI_OUTPUT_MESSAGES] = json.dumps(
818+
[_to_output_message(parts)]
819+
)
816820

817821
return attributes
818822

@@ -883,7 +887,11 @@ def _record_invoke_model_stream_event(
883887
if not payload:
884888
return
885889

886-
chunk = json.loads(payload)
890+
try:
891+
chunk = json.loads(payload)
892+
except ValueError:
893+
return
894+
887895
if not isinstance(chunk, Mapping):
888896
return
889897

‎tests/integrations/boto3/test_bedrock_runtime.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,6 +2237,43 @@ def call():
22372237
)
22382238

22392239

2240+
@pytest.mark.parametrize("trace_mode", TRACE_MODES)
2241+
def test_invoke_model_with_response_stream_skips_malformed_chunks(
2242+
init_bedrock, capture_items, trace_mode
2243+
):
2244+
client = init_bedrock(trace_mode, send_default_pii=True)
2245+
malformed_chunk = ("chunk", {"bytes": base64.b64encode(b"not json").decode()})
2246+
events = [malformed_chunk] + _invoke_model_stream_chunks(
2247+
{
2248+
"outputs": [{"text": "Hi!", "stop_reason": "stop"}],
2249+
"amazon-bedrock-invocationMetrics": {
2250+
"inputTokenCount": 7,
2251+
"outputTokenCount": 3,
2252+
},
2253+
}
2254+
)
2255+
received_chunks = []
2256+
2257+
def call():
2258+
response = client.invoke_model_with_response_stream(
2259+
modelId="mistral.mistral-7b-instruct-v0:2",
2260+
body=json.dumps({"prompt": "Hello!"}),
2261+
)
2262+
received_chunks.extend(event["chunk"]["bytes"] for event in response["body"])
2263+
2264+
with _event_stream_response(client, events):
2265+
spans = _capture_spans(capture_items, trace_mode, call)
2266+
2267+
assert received_chunks[0] == b"not json"
2268+
assert len(received_chunks) == 2
2269+
2270+
attributes = _get_attributes(_get_bedrock_span(spans))
2271+
assert attributes[SPANDATA.GEN_AI_OUTPUT_MESSAGES] == [
2272+
{"role": "assistant", "parts": [{"type": "text", "content": "Hi!"}]}
2273+
]
2274+
assert attributes[SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 3
2275+
2276+
22402277
@pytest.mark.parametrize("trace_mode", TRACE_MODES)
22412278
def test_invoke_model_with_response_stream_error_is_unchanged(
22422279
init_bedrock, capture_items, trace_mode

0 commit comments

Comments
 (0)