Skip to content

Commit 7007b79

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 fb85f5b commit 7007b79

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
@@ -794,8 +794,12 @@ def get_attributes(self) -> "Attributes":
794794
tool_call["arguments"] = content
795795
parts.append(tool_call)
796796

797+
# `record_outputs` already gated the buffered content blocks.
797798
if parts:
798-
attributes.update(_get_output_attributes([_to_output_message(parts)]))
799+
with capture_internal_exceptions():
800+
attributes[SPANDATA.GEN_AI_OUTPUT_MESSAGES] = json.dumps(
801+
[_to_output_message(parts)]
802+
)
799803

800804
return attributes
801805

@@ -866,7 +870,11 @@ def _record_invoke_model_stream_event(
866870
if not payload:
867871
return
868872

869-
chunk = json.loads(payload)
873+
try:
874+
chunk = json.loads(payload)
875+
except ValueError:
876+
return
877+
870878
if not isinstance(chunk, Mapping):
871879
return
872880

‎tests/integrations/boto3/test_bedrock_runtime.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,6 +2020,43 @@ def call():
20202020
)
20212021

20222022

2023+
@pytest.mark.parametrize("trace_mode", TRACE_MODES)
2024+
def test_invoke_model_with_response_stream_skips_malformed_chunks(
2025+
init_bedrock, capture_items, trace_mode
2026+
):
2027+
client = init_bedrock(trace_mode, send_default_pii=True)
2028+
malformed_chunk = ("chunk", {"bytes": base64.b64encode(b"not json").decode()})
2029+
events = [malformed_chunk] + _invoke_model_stream_chunks(
2030+
{
2031+
"outputs": [{"text": "Hi!", "stop_reason": "stop"}],
2032+
"amazon-bedrock-invocationMetrics": {
2033+
"inputTokenCount": 7,
2034+
"outputTokenCount": 3,
2035+
},
2036+
}
2037+
)
2038+
received_chunks = []
2039+
2040+
def call():
2041+
response = client.invoke_model_with_response_stream(
2042+
modelId="mistral.mistral-7b-instruct-v0:2",
2043+
body=json.dumps({"prompt": "Hello!"}),
2044+
)
2045+
received_chunks.extend(event["chunk"]["bytes"] for event in response["body"])
2046+
2047+
with _event_stream_response(client, events):
2048+
spans = _capture_spans(capture_items, trace_mode, call)
2049+
2050+
assert received_chunks[0] == b"not json"
2051+
assert len(received_chunks) == 2
2052+
2053+
attributes = _get_attributes(_get_bedrock_span(spans))
2054+
assert attributes[SPANDATA.GEN_AI_OUTPUT_MESSAGES] == [
2055+
{"role": "assistant", "parts": [{"type": "text", "content": "Hi!"}]}
2056+
]
2057+
assert attributes[SPANDATA.GEN_AI_USAGE_OUTPUT_TOKENS] == 3
2058+
2059+
20232060
@pytest.mark.parametrize("trace_mode", TRACE_MODES)
20242061
def test_invoke_model_with_response_stream_error_is_unchanged(
20252062
init_bedrock, capture_items, trace_mode

0 commit comments

Comments
 (0)