Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/langtrace_python_sdk/instrumentation/aws_bedrock/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,13 +524,15 @@ def __init__(
stream_done_callback=None,
):
super().__init__(response)

self._stream_done_callback = stream_done_callback
self._accumulating_body = {"generation": ""}
self.last_chunk = None

def __iter__(self):
for event in self.__wrapped__:
# Process the event
self._process_event(event)
# Yield the original event immediately
yield event

def _process_event(self, event):
Expand All @@ -545,7 +547,11 @@ def _process_event(self, event):
self._stream_done_callback(decoded_chunk)
return
if "generation" in decoded_chunk:
self._accumulating_body["generation"] += decoded_chunk.get("generation")
generation = decoded_chunk.get("generation")
if self.last_chunk == generation:
return
self.last_chunk = generation
self._accumulating_body["generation"] += generation

if type == "message_start":
self._accumulating_body = decoded_chunk.get("message")
Expand All @@ -554,9 +560,11 @@ def _process_event(self, event):
decoded_chunk.get("content_block")
)
elif type == "content_block_delta":
self._accumulating_body["content"][-1]["text"] += decoded_chunk.get(
"delta"
).get("text")
text = decoded_chunk.get("delta").get("text")
if self.last_chunk == text:
return
self.last_chunk = text
self._accumulating_body["content"][-1]["text"] += text

elif self.has_finished(type, decoded_chunk):
self._accumulating_body["invocation_metrics"] = decoded_chunk.get(
Expand Down
2 changes: 1 addition & 1 deletion src/langtrace_python_sdk/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "3.8.19"
__version__ = "3.8.20"
4 changes: 3 additions & 1 deletion src/tests/aws_bedrock/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
@pytest.fixture(autouse=True)
def environment():
if not os.getenv("AWS_ACCESS_KEY_ID"):
os.environ["AWS_ACCESS_KEY_ID"] = "test_api_key"
os.environ["AWS_ACCESS_KEY_ID"] = "test_aws_access_key_id"
if not os.getenv("AWS_SECRET_ACCESS_KEY"):
os.environ["AWS_SECRET_ACCESS_KEY"] = "test_aws_secret_access_key"


@pytest.fixture
Expand Down