fix: Content is marked non empty if its first part contains text or inline_data or file_data or func call/response

PiperOrigin-RevId: 835063599
This commit is contained in:
Google Team Member
2025-11-20 22:19:28 -08:00
committed by Copybara-Service
parent a3e4ad3cd1
commit 631b58336d
2 changed files with 66 additions and 2 deletions
+10 -2
View File
@@ -224,7 +224,8 @@ def _contains_empty_content(event: Event) -> bool:
This can happen to the events that only changed session state.
When both content and transcriptions are empty, the event will be considered
as empty.
as empty. The content is considered empty if none of its parts contain text,
inline data, file data, function call, or function response.
Args:
event: The event to check.
@@ -239,7 +240,14 @@ def _contains_empty_content(event: Event) -> bool:
not event.content
or not event.content.role
or not event.content.parts
or event.content.parts[0].text == ''
or all(
not p.text
and not p.inline_data
and not p.file_data
and not p.function_call
and not p.function_response
for p in [event.content.parts[0]]
)
) and (not event.output_transcription and not event.input_transcription)
@@ -465,6 +465,43 @@ async def test_events_with_empty_content_are_skipped():
author="user",
content=types.UserContent("How are you?"),
),
# Event with content that has only empty text part
Event(
invocation_id="inv6",
author="user",
content=types.Content(parts=[types.Part(text="")], role="model"),
),
# Event with content that has only inline data part
Event(
invocation_id="inv7",
author="user",
content=types.Content(
parts=[
types.Part(
inline_data=types.Blob(
data=b"test", mime_type="image/png"
)
)
],
role="user",
),
),
# Event with content that has only file data part
Event(
invocation_id="inv8",
author="user",
content=types.Content(
parts=[
types.Part(
file_data=types.FileData(
file_uri="gs://test_bucket/test_file.png",
mime_type="image/png",
)
)
],
role="user",
),
),
]
invocation_context.session.events = events
@@ -478,4 +515,23 @@ async def test_events_with_empty_content_are_skipped():
assert llm_request.contents == [
types.UserContent("Hello"),
types.UserContent("How are you?"),
types.Content(
parts=[
types.Part(
inline_data=types.Blob(data=b"test", mime_type="image/png")
)
],
role="user",
),
types.Content(
parts=[
types.Part(
file_data=types.FileData(
file_uri="gs://test_bucket/test_file.png",
mime_type="image/png",
)
)
],
role="user",
),
]