Skip to content

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

Closed

Conversation

secuwity
Copy link

@secuwity secuwity commented May 20, 2025

This PR addresses several issues in the Agent class:

  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:

    • Implemented sentinel object pattern for callback_handler default value
    • Added _DEFAULT_CALLBACK_HANDLER sentinel object to distinguish between default and explicit None
    • Using the sentinel as the default parameter value for callback_handler
    • Creating a new PrintingCallbackHandler instance when the sentinel is used
    • Preserving the behavior where explicitly passing None uses null_callback_handler
  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

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.

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
@secuwity secuwity requested a review from a team as a code owner May 20, 2025 23:41
Copy link
Member

@awsarron awsarron left a 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())
Copy link
Member

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
Copy link
Member

@awsarron awsarron left a 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:

  1. Open comment about kwargs.copy()
  2. Add unit tests to ensure that we don't regress here in the future

@awsarron awsarron mentioned this pull request Jun 3, 2025
6 tasks
@awsarron
Copy link
Member

awsarron commented Jun 3, 2025

Hi @secuwity, thank you again for raising this PR ❤️.

I'm going to close this one an we'll merge #170 instead which includes the commits here along with the requested revisions.

@awsarron awsarron closed this Jun 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants