Skip to content

Commit 215c852

Browse files
IvanLHcopybara-github
authored andcommitted
fix: Remove validation for empty text parts on Chat, this will support keeping the history in chat when the API yields back such a part.
PiperOrigin-RevId: 852485647
1 parent 930c7ca commit 215c852

3 files changed

Lines changed: 10 additions & 11 deletions

File tree

google/genai/chats.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ def _validate_content(content: Content) -> bool:
3535
for part in content.parts:
3636
if part == Part():
3737
return False
38-
if part.text is not None and part.text == "":
39-
return False
4038
return True
4139

4240

google/genai/tests/chats/test_get_history.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def mock_api_client(vertexai=False):
5555

5656

5757
@pytest.fixture
58-
def mock_generate_content_invalid_content():
58+
def mock_generate_content_with_empty_text_part():
5959
with mock.patch.object(
6060
models.Models, 'generate_content'
6161
) as mock_generate_content:
@@ -84,7 +84,7 @@ def mock_generate_content_empty_content():
8484

8585

8686
@pytest.fixture
87-
def mock_generate_content_stream_invalid_content():
87+
def mock_generate_content_stream_with_empty_text_part():
8888
with mock.patch.object(
8989
models.Models, 'generate_content_stream'
9090
) as mock_generate_content:
@@ -95,7 +95,8 @@ def mock_generate_content_stream_invalid_content():
9595
content=types.Content(
9696
role='model',
9797
parts=[types.Part(text='')],
98-
)
98+
),
99+
finish_reason=types.FinishReason.STOP,
99100
)
100101
]
101102
)
@@ -481,7 +482,7 @@ def test_history_with_invalid_turns():
481482
assert chat.get_history(curated=True) == curated_history
482483

483484

484-
def test_chat_with_invalid_content(mock_generate_content_invalid_content):
485+
def test_chat_with_empty_text_part(mock_generate_content_with_empty_text_part):
485486
models_module = models.Models(mock_api_client)
486487
chats_module = chats.Chats(modules=models_module)
487488
chat = chats_module.create(model='gemini-2.5-flash')
@@ -496,7 +497,7 @@ def test_chat_with_invalid_content(mock_generate_content_invalid_content):
496497
),
497498
]
498499
assert chat.get_history() == expected_comprehensive_history
499-
assert not chat.get_history(curated=True)
500+
assert chat.get_history(curated=True) == expected_comprehensive_history
500501

501502

502503
def test_chat_with_empty_content(mock_generate_content_empty_content):
@@ -517,8 +518,8 @@ def test_chat_with_empty_content(mock_generate_content_empty_content):
517518
assert not chat.get_history(curated=True)
518519

519520

520-
def test_chat_stream_with_invalid_content(
521-
mock_generate_content_stream_invalid_content,
521+
def test_chat_stream_with_empty_text_part(
522+
mock_generate_content_stream_with_empty_text_part,
522523
):
523524
models_module = models.Models(mock_api_client)
524525
chats_module = chats.Chats(modules=models_module)
@@ -536,7 +537,7 @@ def test_chat_stream_with_invalid_content(
536537
),
537538
]
538539
assert chat.get_history() == expected_comprehensive_history
539-
assert not chat.get_history(curated=True)
540+
assert chat.get_history(curated=True) == expected_comprehensive_history
540541

541542

542543
def test_chat_stream_with_empty_content(

google/genai/tests/chats/test_validate_response.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_validate_response_part_with_empty_text():
5353
]
5454
)
5555

56-
assert not _validate_response(response)
56+
assert _validate_response(response)
5757

5858

5959
def test_validate_response_part_with_text():

0 commit comments

Comments
 (0)