Skip to content

Commit 7889bd8

Browse files
wukathcopybara-github
authored andcommitted
fix: Filter out video events with inline data from being stored in session
Co-authored-by: Kathy Wu <wukathy@google.com> PiperOrigin-RevId: 911614336
1 parent 153c7f7 commit 7889bd8

3 files changed

Lines changed: 43 additions & 12 deletions

File tree

src/google/adk/flows/llm_flows/contents.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -795,8 +795,8 @@ def _is_request_input_event(event: Event) -> bool:
795795
return _is_function_call_event(event, REQUEST_INPUT_FUNCTION_CALL_NAME)
796796

797797

798-
def _is_live_model_audio_event_with_inline_data(event: Event) -> bool:
799-
"""Check if the event is a live/bidi audio event with inline data.
798+
def _is_live_model_media_event_with_inline_data(event: Event) -> bool:
799+
"""Check if the event is a live/bidi media event (audio, video, image) with inline data.
800800
801801
There are two possible cases and we only care about the second case:
802802
content=Content(
@@ -826,12 +826,14 @@ def _is_live_model_audio_event_with_inline_data(event: Event) -> bool:
826826
if not event.content or not event.content.parts:
827827
return False
828828
for part in event.content.parts:
829-
if (
830-
part.inline_data
831-
and part.inline_data.mime_type
832-
and part.inline_data.mime_type.startswith('audio/')
833-
):
834-
return True
829+
if part.inline_data and part.inline_data.mime_type:
830+
mime = part.inline_data.mime_type.lower()
831+
if (
832+
mime.startswith('audio/')
833+
or mime.startswith('video/')
834+
or mime.startswith('image/')
835+
):
836+
return True
835837
return False
836838

837839

src/google/adk/runners.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -786,17 +786,17 @@ async def _compute_artifact_delta_for_rewind(
786786

787787
def _should_append_event(self, event: Event, is_live_call: bool) -> bool:
788788
"""Checks if an event should be appended to the session."""
789-
# Don't append audio response from model in live mode to session.
789+
# Don't append media (audio/video/image) response from model in live mode to session.
790790
# The data is appended to artifacts with a reference in file_data in the
791-
# event.
791+
# event if save_live_blob is True.
792792
# We should append non-partial events only.For example, non-finished(partial)
793793
# transcription events should not be appended.
794794
# Function call and function response events should be appended.
795795
# Other control events should be appended.
796-
if is_live_call and contents._is_live_model_audio_event_with_inline_data(
796+
if is_live_call and contents._is_live_model_media_event_with_inline_data(
797797
event
798798
):
799-
# We don't append live model audio events with inline data to avoid
799+
# We don't append live model media events with inline data to avoid
800800
# storing large blobs in the session. However, events with file_data
801801
# (references to artifacts) should be appended.
802802
return False

tests/unittests/test_runners.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,35 @@ def test_should_append_event_other_event(self):
11881188
)
11891189
assert self.runner._should_append_event(event, is_live_call=True) is True
11901190

1191+
def test_should_not_append_event_live_model_video(self):
1192+
event = Event(
1193+
invocation_id="inv1",
1194+
author="model",
1195+
content=types.Content(
1196+
parts=[
1197+
types.Part(
1198+
inline_data=types.Blob(data=b"123", mime_type="video/mp4")
1199+
)
1200+
]
1201+
),
1202+
)
1203+
assert self.runner._should_append_event(event, is_live_call=True) is False
1204+
1205+
def test_should_append_event_non_live_model_video(self):
1206+
event = Event(
1207+
invocation_id="inv1",
1208+
author="model",
1209+
content=types.Content(
1210+
parts=[
1211+
types.Part(
1212+
inline_data=types.Blob(data=b"123", mime_type="video/mp4")
1213+
)
1214+
]
1215+
),
1216+
)
1217+
assert self.runner._should_append_event(event, is_live_call=False) is True
1218+
1219+
11911220

11921221
@pytest.fixture
11931222
def user_agent_module(tmp_path, monkeypatch):

0 commit comments

Comments
 (0)