-
Notifications
You must be signed in to change notification settings - Fork 802
Update contact lookup sample #559
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a significant and well-structured refactoring to create a centralized a2ui-agent package for managing A2UI schemas. The changes include a build-time hook to package schema files, a robust schema manager with fallback loading mechanisms, and a validator. The contact lookup sample has been updated to use this new package, which greatly improves maintainability and decouples the agent from hardcoded schemas. The overall approach is excellent. I've identified a few critical and high-severity issues related to exception handling and a NameError that could break the code at runtime, along with some medium-severity suggestions to improve the robustness of the build script and validation logic.
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from ..inference_strategy import InferenceStrategy |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type hint List is used for selected_components on line 25, but it's not imported from the typing module. This will cause a NameError at runtime when this method is called.
| from ..inference_strategy import InferenceStrategy | |
| from typing import List | |
| from ..inference_strategy import InferenceStrategy |
| try: | ||
| repo_root = find_repo_root(os.path.dirname(__file__)) | ||
| except Exception: | ||
| pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This try...except Exception: pass block is problematic. It swallows all exceptions, which can hide bugs, and if an exception does occur, repo_root will be undefined, leading to a NameError on the subsequent if repo_root: check. The find_repo_root function is safe and not expected to raise exceptions, making this block unnecessary and potentially buggy.
repo_root = find_repo_root(os.path.dirname(__file__))| except Exception: | ||
| pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a broad except Exception: pass is a dangerous pattern as it can hide unexpected errors (e.g., TypeError, AttributeError) and make debugging difficult. The intended purpose is likely to handle cases where package resources are not found. It's better to catch a more specific exception. The PackageLoader.load method raises FileNotFoundError in case of failure, so catching that would be safer.
except FileNotFoundError:
# This is an expected failure when the package is not installed,
# so we fall back to other loading mechanisms.
pass| except Exception: | ||
| pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar to a previous point, this broad except Exception: pass can hide unexpected errors. The FileSystemLoader.load method is expected to raise a FileNotFoundError if the asset is not found, which is a valid fallback scenario. Catching this specific exception is safer and makes the code's intent clearer.
except FileNotFoundError:
# This is an expected failure when assets are not in the local src,
# so we fall back to other loading mechanisms.
pass| # Add src to sys.path to import the constant | ||
| src_path = os.path.join(project_root, "src") | ||
| if src_path not in sys.path: | ||
| sys.path.insert(0, src_path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modifying sys.path directly, especially in a build script, is generally discouraged as it can lead to unpredictable behavior and make the build process fragile. A more robust approach would be to avoid this manipulation. Consider refactoring the shared constants (SPEC_VERSION_MAP, etc.) into a separate, simple configuration file (e.g., a JSON file or a simple Python file at the project root) that can be accessed by both the build hook and the runtime code without path manipulation.
| if self._validator: | ||
| self._validator.validate(parsed_json_data) | ||
| else: | ||
| # Fallback if validator failed to init but UI is on (shouldn't happen ideally) | ||
| logger.warning("Validator not initialized, skipping detailed schema validation.") | ||
| # We could choose to fail here, but let's just warn to avoid blocking if just validation setup failed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fallback logic here, which logs a warning and skips validation if self._validator is not initialized, could mask a significant configuration or initialization error. If UI is enabled (self.use_ui is true), the validator should be considered essential. Instead of silently skipping validation, it would be safer to raise a RuntimeError to make the failure explicit and prevent the agent from producing potentially invalid UI responses.
if not self._validator:
raise RuntimeError("UI is enabled but A2UI validator is not initialized.")
self._validator.validate(parsed_json_data)ee5c8e9 to
b6e614b
Compare
- Add a base InferenceStrategy class - Add PackSpecsBuildHook to copy JSON schemas into the package during build time. - Update pyproject.toml to include assets and configure the build hook. - Implement A2uiSchemaManager for robust schema loading, pruning, and prompt generation.
b6e614b to
6bc12ec
Compare
Tested: - [x] The contact_lookup client successfully connected to the contact_lookup agent and rendered the response correctly.
6bc12ec to
6714d4f
Compare
Tested:
Description
Replace this paragraph with a description of what this PR is changing or adding, and why. Consider including before/after screenshots.
List which issues are fixed by this PR. For larger changes, raising an issue first helps reduce redundant work.
Pre-launch Checklist
If you need help, consider asking for advice on the discussion board.