Skip to content
Open
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
5 changes: 3 additions & 2 deletions google/genai/chats.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ def _extract_curated_history(
i += 1
if is_valid:
curated_history.extend(current_output)
elif curated_history:
curated_history.pop()
else:
while curated_history and curated_history[-1].role == 'user':
curated_history.pop()
return curated_history


Expand Down
19 changes: 19 additions & 0 deletions google/genai/tests/chats/test_get_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,25 @@ def test_history_with_valid_and_invalid_model_output():
assert chat.get_history(curated=True) == []


def test_history_with_consecutive_user_inputs_and_invalid_model_turn():
history = [
types.Content(
role='user',
parts=[types.Part.from_text(text='user input 1')],
),
types.Content(
role='user',
parts=[types.Part.from_text(text='user input 2')],
),
types.Content(
role='model',
parts=[], # invalid content
),
]

assert chats._extract_curated_history(history) == []


def test_history_end_with_user_input():
history = [
types.Content(
Expand Down