Skip to content

Commit 98d12be

Browse files
fix(runners): support message modification in workflows via before_run_callback
Fixes a bug in `_run_node_async` where the return value of `before_run_callback` was being swallowed and discarded during workflow execution. Changes introduced: - Captures `corrected_user_message` from the callback response. - Updates `node_input` and `ic.user_content` to pass the modified message into the Workflow graph engine. - Pops the original un-sanitized user event from `ic.session.events` to prevent dirty duplicates in the conversation history before appending the modified event.
1 parent 065f4ae commit 98d12be

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

src/google/adk/runners.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,18 @@ async def _run_node_async(
528528
yield user_event
529529

530530
# Run before_run callbacks
531-
await ic.plugin_manager.run_before_run_callback(invocation_context=ic)
531+
corrected_user_message = await ic.plugin_manager.run_before_run_callback(invocation_context=ic)
532+
if corrected_user_message is not None:
533+
if isinstance(corrected_user_message, types.Content):
534+
node_input = corrected_user_message
535+
ic.user_content = corrected_user_message
536+
if hasattr(ic.session, 'events') and len(ic.session.events) > 0:
537+
ic.session.events.pop()
538+
user_event = await self._append_user_event(
539+
ic, corrected_user_message
540+
)
541+
if yield_user_message and user_event:
542+
yield user_event
532543

533544
# 3. Start root node in background
534545
from .agents.base_agent import BaseAgent

0 commit comments

Comments
 (0)