Repository navigation
feat(adk): add generic tool and after-model UI callbacks - #4472
gspencergoog wants to merge 8 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces generic ADK callback hooks for appending and emitting validated A2UI surfaces, specifically adding make_tool_ui_callback for tool execution and make_after_model_ui_callback for completed model responses. It also adds an allowed_a2ui_tool_names allowlist to A2uiPartConverter and A2uiEventConverter to let verified domain tools emit A2UI parts. The feedback suggests enhancing JSON payload handling in part_converter.py by adding type checks for lists, and optimizing dictionary copying in tool_ui_callback.py when the target key is absent.
| json_data = response_dict.get(constants.A2UI_VALIDATED_JSON_KEY) | ||
| if json_data: | ||
| return [create_a2ui_part(message) for message in json_data] |
There was a problem hiding this comment.
When handling JSON payloads that can be either a list of objects or a single object, use type checking (such as isinstance(..., list)) to safely handle both cases and avoid indexing errors.
| json_data = response_dict.get(constants.A2UI_VALIDATED_JSON_KEY) | |
| if json_data: | |
| return [create_a2ui_part(message) for message in json_data] | |
| json_data = response_dict.get(constants.A2UI_VALIDATED_JSON_KEY) | |
| if isinstance(json_data, list): | |
| return [create_a2ui_part(message) for message in json_data] | |
| elif json_data is not None: | |
| return [create_a2ui_part(json_data)] |
References
- When handling JSON payloads that can be either a list of objects or a single object, use type checking (such as
isinstance(..., list)) to safely handle both cases and avoid indexing errors.
| if isinstance(tool_response, MutableMapping): | ||
| had_key = A2UI_VALIDATED_JSON_KEY in tool_response | ||
| if had_key: | ||
| tool_response.pop(A2UI_VALIDATED_JSON_KEY, None) | ||
| return dict(tool_response), had_key | ||
| if isinstance(tool_response, Mapping): | ||
| if A2UI_VALIDATED_JSON_KEY in tool_response: | ||
| sanitized = { | ||
| k: v for k, v in tool_response.items() if k != A2UI_VALIDATED_JSON_KEY | ||
| } | ||
| return sanitized, True | ||
| return dict(tool_response), False |
There was a problem hiding this comment.
Avoid copying the dictionary when A2UI_VALIDATED_JSON_KEY is not present in tool_response. Returning the original mapping directly avoids unnecessary dictionary copying overhead.
| if isinstance(tool_response, MutableMapping): | |
| had_key = A2UI_VALIDATED_JSON_KEY in tool_response | |
| if had_key: | |
| tool_response.pop(A2UI_VALIDATED_JSON_KEY, None) | |
| return dict(tool_response), had_key | |
| if isinstance(tool_response, Mapping): | |
| if A2UI_VALIDATED_JSON_KEY in tool_response: | |
| sanitized = { | |
| k: v for k, v in tool_response.items() if k != A2UI_VALIDATED_JSON_KEY | |
| } | |
| return sanitized, True | |
| return dict(tool_response), False | |
| if isinstance(tool_response, MutableMapping): | |
| had_key = A2UI_VALIDATED_JSON_KEY in tool_response | |
| if had_key: | |
| tool_response.pop(A2UI_VALIDATED_JSON_KEY, None) | |
| return dict(tool_response), True | |
| return tool_response, False | |
| if isinstance(tool_response, Mapping): | |
| if A2UI_VALIDATED_JSON_KEY in tool_response: | |
| sanitized = { | |
| k: v for k, v in tool_response.items() if k != A2UI_VALIDATED_JSON_KEY | |
| } | |
| return sanitized, True | |
| return tool_response, False |
Signed-off-by: Greg Spencer <gspencer@google.com>
Signed-off-by: Greg Spencer <gspencer@google.com>
Signed-off-by: Greg Spencer <gspencer@google.com>
… and commit after part conversion
409031a to
993d061
Compare
Summary
Adds generic, decision-agnostic ADK UI callbacks (
make_tool_ui_callbackina2ui.adkandmake_after_model_ui_callbackina2ui.adk.a2a), public callback type aliases (A2uiProcessorSource,ProducerErrorPolicy,SkipSummarizationPredicate), and an opt-inallowed_a2ui_tool_namesallowlist onA2uiPartConverterandA2uiEventConverter. External extensions and user code can attach validated A2UI surfaces to ADK tool outputs or model responses without couplinga2ui-adkto experimental packages.Reviewer Focus
python/a2ui_adk/src/a2ui/adk/tool_ui_callback.py: UntrustedA2UI_VALIDATED_JSON_KEYstripping before producer execution,WeakKeyDictionaryper-RendererStatesurface ID reservation, serialization before state commit, and startup warning whena2ui_processor=None.python/a2ui_adk/src/a2ui/adk/a2a/after_model_ui_callback.py: Filtering of partial/error/function-call/delimited responses, GenAI part conversion before committing toRendererState, and lazy export undera2ui.adk.python/a2ui_adk/src/a2ui/adk/a2a/part_converter.py: Opt-inallowed_a2ui_tool_namesgating alongsidesend_a2ui_json_to_client.Changes
make_tool_ui_callback,ToolUiContext,ToolUiProducer,AfterToolCallbackFn,A2uiProcessorSource,ProducerErrorPolicy, andSkipSummarizationPredicateinpython/a2ui_adk/src/a2ui/adk/tool_ui_callback.pyand exported them froma2ui.adk.make_after_model_ui_callback,AfterModelUiContext,AfterModelUiProducer, andAfterModelCallbackFninpython/a2ui_adk/src/a2ui/adk/a2a/after_model_ui_callback.pyand lazily exported them froma2ui.adk.allowed_a2ui_tool_namestoA2uiPartConverterandA2uiEventConverterinpython/a2ui_adk/src/a2ui/adk/a2a/part_converter.pyandevent_converter.py.a2ui-adk'sa2ui-agent-sdkdependency floor inpython/a2ui_adk/pyproject.tomland updatedpython/a2ui_adk/CHANGELOG.md.python/a2ui_adk/tests/test_tool_ui_callback.py,python/a2ui_adk/tests/a2a/test_after_model_ui_callback.py,test_part_converter.py,test_event_converter.py, andtest_lazy_a2a_exports.py.Impact & Risks
@experimentaland accepta2ui_processor(matchingSendA2uiToClientToolset,A2uiInstruction, andA2uiEventConverter).a2ui_processorlogs a warning at factory creation time because catalog and topology validation is skipped.allowed_a2ui_tool_namesdefaults toNone, preserving existing behavior forsend_a2ui_json_to_client.Testing
uv run pytest python/a2ui_adk/testsuv run mypy python/a2ui_adk/src/a2ui/adk/uv run ruff format --check python/a2ui_adk