langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
92.39k stars 14.77k forks source link

[Bug] Arg Returns in docstring not found in function signature. #26093

Closed SeanIsYoung closed 1 week ago

SeanIsYoung commented 1 week ago

Checked other resources

Example Code

from langchain_core.tools import 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 

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

OS: Linux / WSL OS Version: #1 SMP Fri Mar 29 23:14:13 UTC 2024 Python Version: 3.11.9 (main, Apr 19 2024, 16:48:06) [GCC 11.2.0]

Package Information

langchain_core: 0.2.38 langchain: 0.2.16 langchain_community: 0.2.16 langsmith: 0.1.105 langchain_huggingface: 0.0.3 langchain_milvus: 0.1.4 langchain_openai: 0.1.23 langchain_text_splitters: 0.2.2 langchain_unstructured: 0.1.2

Optional packages not installed

langgraph langserve

Other Dependencies

aiohttp: 3.10.5 async-timeout: Installed. No version info available. dataclasses-json: 0.6.7 httpx: 0.27.2 huggingface-hub: 0.24.6 jsonpatch: 1.33 numpy: 1.26.4 openai: 1.42.0 orjson: 3.10.7 packaging: 24.1 pydantic: 2.8.2 pymilvus: 2.4.5 PyYAML: 6.0.2 requests: 2.32.3 scipy: 1.14.1 sentence-transformers: 3.0.1 SQLAlchemy: 2.0.32 tenacity: 8.5.0 tiktoken: 0.7.0 tokenizers: 0.19.1 transformers: 4.44.2 typing-extensions: 4.12.2 unstructured-client: 0.24.1 unstructured[all-docs]: Installed. No version info available.

gbaian10 commented 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']}}}
SeanIsYoung commented 1 week ago

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.