Skip to content

Commit 8cda99f

Browse files
committed
fix: optimize code
1 parent 2ffbfd7 commit 8cda99f

File tree

1 file changed

+5
-20
lines changed

1 file changed

+5
-20
lines changed

src/strands/models/bedrock.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -320,28 +320,13 @@ def _get_last_turn_messages(self, messages: Messages) -> Messages:
320320
Returns:
321321
Messages containing only the last turn (user + assistant response if exists).
322322
"""
323-
if not messages:
324-
return []
325-
326-
# Find the last user message
327-
last_user_index = -1
328323
for i in range(len(messages) - 1, -1, -1):
329324
if messages[i]["role"] == "user":
330-
last_user_index = i
331-
break
332-
333-
if last_user_index == -1:
334-
# No user message found, return empty
335-
return []
336-
337-
# Start with the last user message
338-
result_messages: Messages = [messages[last_user_index]]
339-
340-
# Include the assistant's response if it exists (the message after the user message)
341-
if last_user_index < len(messages) - 1 and messages[last_user_index + 1]["role"] == "assistant":
342-
result_messages.append(messages[last_user_index + 1])
343-
344-
return result_messages
325+
# Include assistant response if it immediately follows
326+
if i + 1 < len(messages) and messages[i + 1]["role"] == "assistant":
327+
return [messages[i], messages[i + 1]]
328+
return [messages[i]]
329+
return []
345330

346331
def _format_bedrock_messages(self, messages: Messages) -> list[dict[str, Any]]:
347332
"""Format messages for Bedrock API compatibility.

0 commit comments

Comments
 (0)