Skip to content

Add docstrings to utility methods in agent.py #3035

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

dhyeyinf
Copy link

What this PR does

This pull request adds missing docstrings to several utility methods in src/crewai/agent.py to improve code readability and developer understanding.

Updated Methods

  • get_delegation_tools(self, agents)
  • get_multimodal_tools(self)
  • get_code_execution_tools(self)
  • get_output_converter(self, llm, text, model, instructions)

These docstrings follow PEP 257 standards and are aimed at helping contributors and users better understand the codebase.

Why this is important

Improved documentation supports open-source maintainability and makes it easier for new contributors to onboard. These utility functions previously lacked docstrings, which could lead to confusion about their purpose or return types.

Additional Notes

No functionality was changed. The file passes ruff and black formatting checks.

Please feel free to suggest any improvements — I’d be happy to make changes if needed. 🙂

@mplachta
Copy link
Contributor

Disclaimer: This review was made by a crew of AI Agents.

Code Review for crewAIInc/crewAI PR #3035 — Adding Missing Docstrings to Utility Methods in agent.py

Summary of Key Findings

This PR enhances the code quality of src/crewai/agent.py by adding detailed docstrings to several utility methods that previously lacked inline documentation. The added docstrings improve readability, maintainability, and clarify the purpose, inputs, and outputs of the following methods:

  • get_delegation_tools
  • get_multimodal_tools
  • get_code_execution_tools
  • get_output_converter

No changes were made to logic or functionality, so the impact is strictly on improving internal documentation and developer experience.

Code Quality Findings and Specific Improvement Suggestions

1. Docstring Adequacy and Style

  • The docstrings provide clear summaries, argument descriptions, and return values, following a consistent style using triple-quoted strings.
  • They align well with existing type hints, enhancing clarity.
  • Suggest adopting a consistent docstring style throughout the module (e.g., Google style as used here) for uniformity and to support automated documentation tools.
  • Include a one-line summary followed by a blank line before elaborating arguments and return types to comply fully with PEP 257 conventions.

2. Parameter and Return Type Descriptions

  • For get_delegation_tools, consider using Sequence[BaseTool] as the return type for greater flexibility and to match the style seen in other methods.
  • Document potential exceptions or failure modes if instantiating AgentTools can fail or raise exceptions.
  • For get_code_execution_tools, replace List[BaseTool] | None in docstrings with the PEP-compliant Optional[List[BaseTool]].
  • Explicitly mention in get_code_execution_tools that None is returned only if the optional dependency crewai_tools is not installed, helping users understand conditional availability.
  • In get_output_converter, describe llm more explicitly as an interface or class expected, and provide the fully qualified name for Converter to reduce ambiguity for future maintainers.

3. Clarity on Dependency Handling

  • get_code_execution_tools gracefully handles the absence of the crewai_tools dependency by returning None. This pattern is good, but documenting this conditional behavior explicitly in the method docstring is recommended to inform users of optional features.
  • Optionally, consider logging a warning (if not done already) when the dependency is missing to aid debugging.

4. Maintainability and Consistency

  • The added docstrings enable easier onboarding and code navigation.
  • Review related files (AgentTools, AddImageTool, Converter, etc.) for similar documentation consistency.
  • Encourage adopting a uniform docstring standard and applying it throughout the codebase to maximize documentation utility.

Historical Context and Related PR Lessons

  • This PR fits into a common pattern of incremental documentation improvements seen in similar repositories and likely in past crewAI PRs focused on code quality.
  • Prior related PRs typically emphasize aligning docstrings with type hints and describing optional or conditional behaviors clearly—particularly useful for optional dependencies.
  • Lessons from related changes suggest prioritizing consistent style, using standard typing forms (Optional, Sequence), and fully qualifying project-specific classes in docstrings.

Implications for Related Files

  • The AgentTools class used in get_delegation_tools should be reviewed to confirm its documentation and interface stability.
  • AddImageTool in get_multimodal_tools is a singleton example of multimodal support; if extended, docs should be updated accordingly.
  • The converter.py or relevant module where Converter is defined should have well-maintained docstrings describing usage, expected behavior, and integration points.
  • Code execution tooling depends on optional package crewai_tools, so any integration or fallback mechanisms related to this package deserve testing and documentation.

Example of Refined Docstring for get_code_execution_tools

def get_code_execution_tools(self) -> Optional[List[BaseTool]]:
    """
    Retrieve tools to enable code execution capabilities for the agent.

    Returns:
        Optional[List[BaseTool]]: A list of code execution tools if the
        'crewai_tools' package is installed; otherwise, returns None.
    """

Conclusion

This PR positively contributes to the maintainability and clarity of the agent.py module by adding missing documentation to key utility methods involved in multi-agent tooling, multimodal support, code execution, and output conversion. While no functional changes were introduced, the added docstrings lay a stronger foundation for future contributors and facilitate smoother integrations with optional dependencies.

Consider adopting the improvement suggestions above to further refine docstrings and ensure consistent documentation across related components.


If there is interest, the project might benefit from a documentation style guide to standardize docstrings and typing annotations for all agents and tools-related modules going forward. This would streamline future docstring additions and reviews.

Thank you for this valuable documentation enhancement contribution!

@joaomdmoura
Copy link
Collaborator

Disclaimer: This review was made by a crew of AI Agents.

Code Review Comments for PR #3035

Overview

This pull request makes significant improvements to the documentation of utility methods in agent.py, specifically for the following methods:

  • get_delegation_tools()
  • get_multimodal_tools()
  • get_code_execution_tools()
  • get_output_converter()

Positive Aspects

  1. Documentation Enhancements: The addition of docstrings follows the Google-style format and provides a clear understanding of the methods' purposes, parameters, and return types.
  2. Type Hints: The preservation of type hints alongside docstrings ensures better code readability and IDE support.
  3. Clarity on Exceptions: The inclusion of exception information in the docstrings enhances understanding of potential issues during method calls.

Areas for Improvement

While the pull request makes commendable improvements, there are some areas that could be enhanced further:

1. get_delegation_tools()

Improvement Suggestions:

  • Enhance the clarity and specificity of the return type and detail any potential exceptions that may occur.

Example:

def get_delegation_tools(self, agents: List[BaseAgent]) -> List[BaseTool]:
    """
    Retrieve delegation tools configured with the provided agents.

    Args:
        agents (List[BaseAgent]): A list of agents to be used for delegation.

    Returns:
        List[BaseTool]: Tools associated with delegation through the given agents.

    Raises:
        ValueError: If agents list is empty or contains invalid agent types.
    """

2. get_multimodal_tools()

Improvement Suggestions:

  • Add details regarding limitations and dependencies associated with the multimodal tools.

Example:

def get_multimodal_tools(self) -> Sequence[BaseTool]:
    """
    Retrieve tools used for handling multimodal inputs like images.

    Returns:
        Sequence[BaseTool]: Currently includes AddImageTool for image handling capabilities.

    Note:
        This method currently only supports image handling through AddImageTool.
    """

3. get_code_execution_tools()

Improvement Suggestions:

  • Ensure that exception handling is thoroughly documented to elucidate potential failures.

Example:

def get_code_execution_tools(self) -> Optional[List[BaseTool]]:
    """
    Retrieve tools for code execution.

    Returns:
        List[BaseTool] | None: List of code execution tools if available,
        otherwise None if crewai_tools is not installed.

    Raises:
        ImportError: When crewai_tools package is not installed.

    Note:
        Requires crewai_tools package to be installed for functionality.
    """

4. get_output_converter()

Improvement Suggestions:

  • Provide more precise typing for parameters and returns and include usage examples if applicable.

Example:

def get_output_converter(
    self,
    llm: BaseLLM,
    text: str,
    model: str,
    instructions: str
) -> Converter:
    """
    Create a Converter object for processing model outputs.

    Args:
        llm (BaseLLM): The language model instance to be used for conversion.

    Returns:
        Converter: Configured Converter object.
    """

Historical Context from Related PRs

Previous discussions in pull requests regarding documentation have highlighted the necessity for:

  • Consistency in adhering to Google-style docstrings.
  • The practice of adding usage examples for complex methods.

In particular, PRs that have focused on documenting return types and error handling consistently underscore the value this brings to code maintainability and comprehension.

General Recommendations

  1. Continue to ensure all methods have specific and clear documentation with relevant examples.
  2. Standardize the inclusion of "Raises" sections to document potential exceptions in methods.
  3. Enhance the overall usability of documentation by including more comprehensive examples in the docstrings.

Summary

The improvements in this PR are a solid step toward establishing clearer documentation standards within the codebase. By addressing the areas for improvement, particularly around exception handling and usage examples, we can further elevate the quality and maintainability of the documentation. I recommend implementing these suggestions in follow-up PRs to uphold and enhance the documentation quality moving forward.

@dhyeyinf
Copy link
Author

Thank you so much for the detailed and constructive reviews! I’m glad the improvements were helpful.

I’ll go ahead and make a follow-up PR implementing the raised suggestions — including refining the return types, adding exception details, and enhancing docstring clarity for conditional dependencies. That way, this PR can stay clean and focused.

Let me know if you’d prefer the changes here instead — I’m happy to update either way.

@dhyeyinf
Copy link
Author

Hi again 👋 Just following up to see if it's okay to proceed with a follow-up PR for the suggested docstring refinements (like exception handling, improved types, etc.).

Happy to make those changes in a separate PR — just wanted to confirm before opening it.

Thanks again for your time and feedback! 🙏

Copy link
Contributor

@lucasgomide lucasgomide left a comment

Choose a reason for hiding this comment

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

@dhyeyinf great changes!

However can you add the type hints also?

@dhyeyinf
Copy link
Author

Thank you so much for the feedback! 🙏
I've now made the requested updates by adding the missing type hints to the three methods.

Please let me know if there’s anything else you'd like me to adjust — happy to make further changes if needed.
Otherwise, if everything looks good, feel free to merge the PR whenever convenient.

@dhyeyinf
Copy link
Author

Thanks again for the review and guidance! 🙌
I've added the missing return None to address the mypy check, and pushed the updated changes.

Let me know if there's anything else you'd like me to improve — happy to iterate further.
Otherwise, it's ready for merge whenever you feel it's good to go. 😊

@dhyeyinf
Copy link
Author

dhyeyinf commented Jul 1, 2025

Hi @lucasgomide 👋 Just following up on this — I’ve addressed the mypy check and all requested changes have been pushed.

Let me know if there's anything else you'd like me to update. Otherwise, if everything looks good, this should be ready for merge. Thanks again! 😊

@lucasgomide
Copy link
Contributor

@dhyeyinf I'm sorry about delay to reply but now you have to resolve some conflicts.

Please tag me when you did it

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.

4 participants