Skip to content

Conversation

sumitaryal
Copy link
Contributor

Summary

This PR introduces new methods in the Agent class to enable the dynamic addition of various components (tools, handoffs, input guardrails, and output guardrails) in a scalable and type-safe manner.

Changes

  • _add_item: A generic helper function to perform type checking and avoid duplicate additions.
  • _ensure_iterable: A static method to ensure inputs are processed as iterables (handling single values and collections uniformly).
  • add_tools: Allows adding one or multiple tools, using valid tool types from the union.
  • add_handoffs: Supports the addition of one or multiple handoffs.
  • add_input_guardrails: Enables adding one or more input guardrails.
  • add_output_guardrails: Supports adding one or multiple output guardrails.

Motivation

By splitting the addition functionality into small, reusable methods, we improve the maintainability and extensibility of the Agent class. These changes also enforce type safety and prevent duplicates, ensuring robust runtime behavior.

Testing

  • Verified that each method adds the correct items without duplicates.
  • Confirmed type-checking is enforced correctly for each collection.

Introduced a generic helper method `_add_item` that handles type checking and duplicate prevention when adding items to list attributes.
Added `_ensure_iterable` to ensure that inputs are always processed as iterables (except for strings and bytes), allowing the API to accept both single items and collections.
Implemented `add_tools` to add one or multiple tools to the agent's tools list. This method leverages `_ensure_iterable` and `_add_item`. The valid tool types are determined via `get_args(Tool)`.
Added `add_handoffs` to allow adding one or more handoffs (Agent or Handoff instances) to the agent's handoffs list.
Added `add_input_guardrails` to support the addition of one or multiple input guardrails to the agent's input guardrails list.
Introduced `add_output_guardrails` to enable adding one or multiple output guardrails to the agent's output guardrails list.
Refactored the docstring in the _add_item method to conform to the max line length requirement. The docstring is now split across multiple lines, resolving the E501 error.
Prior to this change, _ensure_iterable returned an untyped iterable, triggering a mypy error ("Returning Any from function declared to return Iterable[Any]"). This commit adds an explicit cast to ensure the return value is recognized as Iterable[Any] when the input is iterable. The function's runtime behavior remains unchanged.
Copy link
Collaborator

@rm-openai rm-openai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer people just add directly by modifying the agent.

agent.tools.append(...)
agent.guardrails.extend(...)

etc

@rm-openai rm-openai closed this Mar 12, 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