-
Notifications
You must be signed in to change notification settings - Fork 210
Fix type annotations and parameter handling in Agent class #68
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
Conversation
1. Type Annotation Improvements: - Changed kwargs: Any to kwargs: Dict[str, Any] in _run_loop method - Changed dict[str, Any] to Dict[str, Any] in _execute_event_loop_cycle - Improved all Callable type annotations to Callable[..., Any] 2. Default Parameter Value Fix: - Changed callback_handler default from PrintingCallbackHandler() to None - This avoids the anti-pattern of using a mutable default parameter value 3. Parameter Mutation Protection: - Added .copy() to kwargs in both __call__ and stream_async methods - This prevents modifications to the kwargs dictionary from affecting the original
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.
Thank you for your contribution @secuwity!
A couple of small comments and then we'll get these changes merged 🚀
@@ -340,7 +340,7 @@ def __call__(self, prompt: str, **kwargs: Any) -> AgentResult: | |||
|
|||
try: | |||
# Run the event loop and get the result | |||
result = self._run_loop(prompt, kwargs) | |||
result = self._run_loop(prompt, kwargs.copy()) |
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.
It can be useful to pass in a value and have the agent modify it during its execution which is achieved by not copying kwargs
.
Could we move this change out of this PR for now, along with the kwargs
copy on line 403?
This addresses PR feedback by: 1. Adding a sentinel object _DEFAULT_CALLBACK_HANDLER to distinguish between default and explicit None 2. Using the sentinel as the default parameter value for callback_handler 3. Creating a new PrintingCallbackHandler instance when the sentinel is used 4. Preserving the behavior where explicitly passing None uses null_callback_handler 5. Updating the docstring to reflect these changes
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.
Thanks for the updates @secuwity.
A couple of outstanding things and then we can merge this:
- Open comment about
kwargs.copy()
- Add unit tests to ensure that we don't regress here in the future
This PR addresses several issues in the Agent class:
Type Annotation Improvements:
Default Parameter Value Fix:
Parameter Mutation Protection:
These changes improve the code's type safety, prevent potential bugs related to mutable default parameters, and ensure that input parameters are not unexpectedly modified during execution.