Skip to content

Commit 0e71985

Browse files
vishwa180copybara-github
authored andcommitted
fix: Safer fix for UI widget merging in ADK
Revert global list extension in deep_merge_dicts and instead explicitly handle list extension only for render_ui_widgets in merge_parallel_function_response_events. Co-authored-by: Vishwa Murugan <vishwamurugan@google.com> PiperOrigin-RevId: 891147765
1 parent 4030c0d commit 0e71985

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,12 +1151,10 @@ def __build_response_event(
11511151

11521152

11531153
def deep_merge_dicts(d1: dict, d2: dict) -> dict:
1154-
"""Recursively merges d2 into d1, extending lists where keys collide."""
1154+
"""Recursively merges d2 into d1."""
11551155
for key, value in d2.items():
11561156
if key in d1 and isinstance(d1[key], dict) and isinstance(value, dict):
11571157
d1[key] = deep_merge_dicts(d1[key], value)
1158-
elif key in d1 and isinstance(d1[key], list) and isinstance(value, list):
1159-
d1[key].extend(value)
11601158
else:
11611159
d1[key] = value
11621160
return d1
@@ -1181,14 +1179,25 @@ def merge_parallel_function_response_events(
11811179

11821180
# Merge actions from all events
11831181
merged_actions_data: dict[str, Any] = {}
1182+
aggregated_ui_widgets = []
11841183
for event in function_response_events:
11851184
if event.actions:
1185+
actions_dict = event.actions.model_dump(exclude_none=True, by_alias=True)
1186+
ui_widgets = actions_dict.pop(
1187+
'renderUiWidgets', None
1188+
) or actions_dict.pop('render_ui_widgets', None)
1189+
if ui_widgets:
1190+
aggregated_ui_widgets.extend(ui_widgets)
1191+
11861192
# Use `by_alias=True` because it converts the model to a dictionary while respecting field aliases, ensuring that the enum fields are correctly handled without creating a duplicate.
11871193
merged_actions_data = deep_merge_dicts(
11881194
merged_actions_data,
1189-
event.actions.model_dump(exclude_none=True, by_alias=True),
1195+
actions_dict,
11901196
)
11911197

1198+
if aggregated_ui_widgets:
1199+
merged_actions_data['renderUiWidgets'] = aggregated_ui_widgets
1200+
11921201
merged_actions = EventActions.model_validate(merged_actions_data)
11931202

11941203
# Create the new merged event

0 commit comments

Comments
 (0)