Skip to content

Enable developers to use Annotated types for function tool param description #1731

@shubhlohiya

Description

@shubhlohiya

Currently, the only way to add parameter descriptions for function tools when using the function_tool decorator is using the function docstring. However, I think it would be very developer friendly if parsing descriptions from Annotated types was also supported.

Here is an example of how these can be supported:

from typing import Annotated, Callable, get_origin
import inspect
from pydantic import ValidationError

def sum_two_numbers(
    a: Annotated[int, "First number to add"],
    b: Annotated[int, "Second number to add"]
) -> int:
    return a + b


def get_annotated_description(param_type: type) -> str:
    if get_origin(param_type) is Annotated:
        metadata = getattr(param_type, "__metadata__", ())
        for annotation in metadata:
            if isinstance(annotation, str):
                return annotation
    return None


def get_annotated_descriptions(func: Callable) -> dict:
    sig = inspect.signature(func)
    param_descs = {}
    for name, param in sig.parameters.items():
        param_type = param.annotation
        if param_type is inspect.Parameter.empty:
            raise ValidationError(f"Parameter {name} has no type hint. Type hints are required for all func input parameters.")
        param_descs[name] = get_annotated_description(param_type)
    return param_descs

param_descs = get_annotated_descriptions(sum_two_numbers)
print(param_descs)

Outputs:

{'a': 'First number to add', 'b': 'Second number to add'}

and can directly be used in function_schema.py.


This will allow developers to decorate functions as function tools with name_override, description_override, and annotated types in the function signature to fully specify the function tool schema which is model facing, while allowing them to maintain a docstring that developer-facing.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions