Skip to content

Commit 69d2c56

Browse files
committed
fix aws bedrock streaming bug
1 parent 8f5aba4 commit 69d2c56

File tree

1 file changed

+6
-3
lines changed
  • src/langtrace_python_sdk/instrumentation/aws_bedrock

1 file changed

+6
-3
lines changed

src/langtrace_python_sdk/instrumentation/aws_bedrock/patch.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import json
1818

1919
from wrapt import ObjectProxy
20+
from itertools import tee
2021
from .stream_body_wrapper import BufferedStreamBody
2122
from functools import wraps
2223
from langtrace.trace_attributes import (
@@ -128,7 +129,9 @@ def traced_method(*args, **kwargs):
128129
response = original_method(*args, **kwargs)
129130

130131
if span.is_recording():
131-
set_span_streaming_response(span, response)
132+
stream1, stream2 = tee(response["stream"])
133+
set_span_streaming_response(span, stream1)
134+
response["stream"] = stream2
132135
return response
133136

134137
return traced_method
@@ -442,10 +445,10 @@ def _set_response_attributes(span, kwargs, result):
442445
)
443446

444447

445-
def set_span_streaming_response(span, response):
448+
def set_span_streaming_response(span, response_stream):
446449
streaming_response = ""
447450
role = None
448-
for event in response["stream"]:
451+
for event in response_stream:
449452
if "messageStart" in event:
450453
role = event["messageStart"]["role"]
451454
elif "contentBlockDelta" in event:

0 commit comments

Comments
 (0)