Closed SeanIsYoung closed 1 week ago
from langchain_core.tools import tool
from langchain_core.utils.function_calling import convert_to_openai_tool
@tool(parse_docstring=True)
def add(a: int, b: int) -> int:
"""Add two integers.
Args:
a: First integer.
b: Second integer.
Returns:
a + b
"""
return a + b
@tool(parse_docstring=True)
def mulitply(a: int, b: int) -> int:
"""Multiply two integers.
Args:
a: First integer.
b: Second integer.
Returns:
a * b
"""
return a * b
print(convert_to_openai_tool(add))
I can't reproduce your bug.
output:
{'type': 'function', 'function': {'name': 'add', 'description': 'Add two integers.', 'parameters': {'type': 'object', 'properties': {'a': {'description': 'First integer.', 'type': 'integer'}, 'b': {'description': 'Second integer.', 'type': 'integer'}}, 'required': ['a', 'b']}}}
I can't reproduce your bug.
Not sure what it was doing. I've just tried running it again and for some reason it works now.
Sorry about that.
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
Traceback (most recent call last): File "/home/youngs/test/test.py", line 16, in
@tool(parse_docstring=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/youngs/anaconda3/envs/rag-environment/lib/python3.11/site-packages/langchain_core/tools/convert.py", line 220, in _partial
return _make_with_name(func.name)(func)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/youngs/anaconda3/envs/rag-environment/lib/python3.11/site-packages/langchain_core/tools/convert.py", line 176, in _make_tool
return StructuredTool.from_function(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/youngs/anaconda3/envs/rag-environment/lib/python3.11/site-packages/langchain_core/tools/structured.py", line 163, in from_function
args_schema = create_schema_from_function(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/youngs/anaconda3/envs/rag-environment/lib/python3.11/site-packages/langchain_core/tools/base.py", line 212, in create_schema_from_function
description, arg_descriptions = _infer_arg_descriptions(
^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/youngs/anaconda3/envs/rag-environment/lib/python3.11/site-packages/langchain_core/tools/base.py", line 152, in _infer_arg_descriptions
_validate_docstring_args_against_annotations(arg_descriptions, annotations)
File "/home/youngs/anaconda3/envs/rag-environment/lib/python3.11/site-packages/langchain_core/tools/base.py", line 127, in _validate_docstring_args_against_annotations
raise ValueError(
ValueError: Arg Returns in docstring not found in function signature.
Description
I'd have thought this would have run successfully. It appears to be erroring out because of the "Returns:" line in the docstring. And when I remove it, it works fine.
This is an accepted part of the google docstring and so I would have thought it would have worked.
I'm not sure if this behaviour is intentional or if it's an actual bug. But I would have thought if you didn't want to include it in the scheme, at the very least it could just be ignored, rather than casuing an error.
On further testing it appears that the same issue is present with the "Raises" keyword.
System Info
System Information
Package Information
Optional packages not installed
Other Dependencies