Skip to content

Conversation

@Lingbo-Huang
Copy link

Related issue

Fixes #508

Problem

Currently, the AgentBuilder._build() method uses pop() operations which modify the original config dict, potentially causing:

  1. Side effects: If the caller retains a reference to the config, they'll find it unexpectedly modified
  2. Reusability issues: The same config object cannot be used multiple times
  3. Unclear intent: The purpose of pop() is not clear enough

Use dictionary comprehension to create a new dict, filtering out fields that need to be passed explicitly:

# Filter out special fields that will be passed explicitly
# to avoid modifying the original config and parameter conflicts
config_for_init = {
    k: v for k, v in agent_config.items()
    if k not in ["sub_agents", "tools"]
}

agent = agent_cls(**config_for_init, sub_agents=sub_agents, tools=tools)

Changes:

Remove agent_config.pop("sub_agents")
Remove agent_config.pop("tools")
Add dictionary filtering to create config_for_init
Add comments explaining the change

Benefits

No side effects: doesn't modify original config
Reusable: config can be used multiple times
Clear intent: code purpose is explicit
Best practice: follows immutability principle

- Replace pop() operations with dictionary comprehension
- Create filtered config dict to avoid side effects
- Improve code clarity and maintainability

The previous implementation used pop() which modified the original
config dict passed to _build(). This could cause issues if the caller
retained a reference to the config or wanted to reuse it.

The new implementation uses dictionary comprehension to create a new
dict excluding 'sub_agents' and 'tools', which are passed explicitly
as parameters to avoid conflicts.
@Lingbo-Huang
Copy link
Author

@cuericlee Hello, this is my first contribute in github. Thank you!

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.

Improve AgentBuilder: Avoid modifying original config dict

1 participant