Skip to content

Support ReST-style docstrings when loading tools from function #7

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
bilgeyucel opened this issue Mar 14, 2025 · 1 comment
Open

Comments

@bilgeyucel
Copy link
Owner

Is your feature request related to a problem? Please describe.
Currently we will need to use Annotated parameters for functions to be smoothly parsed as tools. This is not the standard documentation practice in Python however, and would often need manual labor for tool creators to convert standard python functions to tools. And this manual labor is usually copying from the ReST docstring and pasting to the Annotated description.

Describe the solution you'd like
Make create_tool_from_function support ReST-styled docstrings by default. See deepset-ai#9004

Describe alternatives you've considered

Additional context
This was a small upgrade when I was working on my personal project, but I feel like this can be standardized as a common feature. So here's the PR.

@bilgeyucel
Copy link
Owner Author

ReST-style Docstring Support Analysis

I've analyzed the feature request for supporting ReST-style docstrings in the create_tool_from_function utility. This is definitely a valuable enhancement that would make tool creation more developer-friendly.

Current Implementation

Currently, create_tool_from_function only supports parameter descriptions through typing.Annotated:

def get_weather(
    city: Annotated[str, "the city for which to get the weather"] = "Munich",
    unit: Annotated[Literal["Celsius", "Fahrenheit"], "the unit for the temperature"] = "Celsius"):
    '''A simple function to get the current weather for a location.'''
    # Function implementation

Proposed Enhancement

The enhancement would allow developers to use standard ReST-style docstrings for parameter descriptions:

def get_weather(city: str = "Munich", unit: Literal["Celsius", "Fahrenheit"] = "Celsius"):
    '''A simple function to get the current weather for a location.
    
    :param city: the city for which to get the weather
    :param unit: the unit for the temperature
    '''
    # Function implementation

Implementation Approach

To implement this feature, we would need to:

  1. Parse the function's docstring to extract parameter descriptions
  2. Add a docstring parser that specifically handles ReST-style parameter documentation (:param param_name: description)
  3. Update create_tool_from_function to:
    • Extract parameter descriptions from docstrings when available
    • Fall back to Annotated metadata when docstring descriptions aren't available
    • Populate the parameter descriptions in the generated JSON schema

This would provide a more natural developer experience for Python developers who are already using ReST-style docstrings as their standard documentation approach.

Backward Compatibility

The implementation should maintain backward compatibility by:

  • First checking for Annotated metadata (existing approach)
  • Then falling back to docstring parameter descriptions if available
  • Ensuring existing tools created with Annotated types continue to work

This enhancement would reduce the "manual labor" mentioned in the issue description where developers need to copy descriptions from docstrings into Annotated type hints.

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

No branches or pull requests

1 participant