Skip to content

Commit afe4083

Browse files
authored
fix: remove live event buffering in runner (#6151)
1 parent b4fee1c commit afe4083

2 files changed

Lines changed: 254 additions & 317 deletions

File tree

src/google/adk/runners.py

Lines changed: 8 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@
6666
logger = logging.getLogger('google_adk.' + __name__)
6767

6868

69-
def _is_tool_call_or_response(event: Event) -> bool:
70-
return bool(event.get_function_calls() or event.get_function_responses())
71-
72-
7369
def _get_function_responses_from_content(
7470
content: types.Content,
7571
) -> list[types.FunctionResponse]:
@@ -80,21 +76,6 @@ def _get_function_responses_from_content(
8076
]
8177

8278

83-
def _is_transcription(event: Event) -> bool:
84-
return (
85-
event.input_transcription is not None
86-
or event.output_transcription is not None
87-
)
88-
89-
90-
def _has_non_empty_transcription_text(
91-
transcription: types.Transcription,
92-
) -> bool:
93-
return bool(
94-
transcription and transcription.text and transcription.text.strip()
95-
)
96-
97-
9879
def _apply_run_config_custom_metadata(
9980
event: Event, run_config: RunConfig | None
10081
) -> None:
@@ -873,22 +854,6 @@ async def _exec_with_plugin(
873854
yield early_exit_event
874855
else:
875856
# Step 2: Otherwise continue with normal execution
876-
# Note for live/bidi:
877-
# the transcription may arrive later than the action(function call
878-
# event and thus function response event). In this case, the order of
879-
# transcription and function call event will be wrong if we just
880-
# append as it arrives. To address this, we should check if there is
881-
# transcription going on. If there is transcription going on, we
882-
# should hold on appending the function call event until the
883-
# transcription is finished. The transcription in progress can be
884-
# identified by checking if the transcription event is partial. When
885-
# the next transcription event is not partial, it means the previous
886-
# transcription is finished. Then if there is any buffered function
887-
# call event, we should append them after this finished(non-partial)
888-
# transcription event.
889-
buffered_events: list[Event] = []
890-
is_transcribing: bool = False
891-
892857
async with Aclosing(execute_fn(invocation_context)) as agen:
893858
async for event in agen:
894859
_apply_run_config_custom_metadata(
@@ -906,50 +871,14 @@ async def _exec_with_plugin(
906871
)
907872

908873
if is_live_call:
909-
if event.partial and _is_transcription(event):
910-
is_transcribing = True
911-
if is_transcribing and _is_tool_call_or_response(event):
912-
# only buffer function call and function response event which is
913-
# non-partial
914-
buffered_events.append(output_event)
915-
continue
916-
# Note for live/bidi: for audio response, it's considered as
917-
# non-partial event(event.partial=None)
918-
# event.partial=False and event.partial=None are considered as
919-
# non-partial event; event.partial=True is considered as partial
920-
# event.
921-
if event.partial is not True:
922-
if _is_transcription(event) and (
923-
_has_non_empty_transcription_text(event.input_transcription)
924-
or _has_non_empty_transcription_text(
925-
event.output_transcription
926-
)
927-
):
928-
# transcription end signal, append buffered events
929-
is_transcribing = False
930-
logger.debug(
931-
'Appending transcription finished event: %s', event
932-
)
933-
if self._should_append_event(event, is_live_call):
934-
await self.session_service.append_event(
935-
session=invocation_context.session, event=output_event
936-
)
937-
938-
for buffered_event in buffered_events:
939-
logger.debug('Appending buffered event: %s', buffered_event)
940-
await self.session_service.append_event(
941-
session=invocation_context.session, event=buffered_event
942-
)
943-
yield buffered_event # yield buffered events to caller
944-
buffered_events = []
945-
else:
946-
# non-transcription event or empty transcription event, for
947-
# example, event that stores blob reference, should be appended.
948-
if self._should_append_event(event, is_live_call):
949-
logger.debug('Appending non-buffered event: %s', event)
950-
await self.session_service.append_event(
951-
session=invocation_context.session, event=output_event
952-
)
874+
# Skip partial transcriptions for Live
875+
if event.partial is not True and self._should_append_event(
876+
event, is_live_call
877+
):
878+
logger.debug('Appending live event: %s', output_event)
879+
await self.session_service.append_event(
880+
session=invocation_context.session, event=output_event
881+
)
953882
else:
954883
if event.partial is not True:
955884
await self.session_service.append_event(

0 commit comments

Comments
 (0)