Skip to content

[tools]: Correct type casting of annotations in _infer_arg_descriptions #31181

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

Merged
9 changes: 3 additions & 6 deletions libs/core/langchain_core/tools/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import functools
import inspect
import json
import typing
import warnings
from abc import ABC, abstractmethod
from inspect import signature
Expand Down Expand Up @@ -80,7 +81,7 @@ class SchemaAnnotationError(TypeError):


def _is_annotated_type(typ: type[Any]) -> bool:
return get_origin(typ) is Annotated
return get_origin(typ) is typing.Annotated


def _get_annotation_description(arg_type: type) -> str | None:
Expand Down Expand Up @@ -143,11 +144,7 @@ def _infer_arg_descriptions(
error_on_invalid_docstring: bool = False,
) -> tuple[str, dict]:
"""Infer argument descriptions from a function's docstring."""
if hasattr(inspect, "get_annotations"):
# This is for python < 3.10
annotations = inspect.get_annotations(fn)
else:
annotations = getattr(fn, "__annotations__", {})
annotations = typing.get_type_hints(fn, include_extras=True)
if parse_docstring:
description, arg_descriptions = _parse_python_function_docstring(
fn, annotations, error_on_invalid_docstring=error_on_invalid_docstring
Expand Down
21 changes: 21 additions & 0 deletions libs/core/tests/unit_tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2711,3 +2711,24 @@ async def async_no_op(foo: int) -> str:
"id": "call_0_82c17db8-95df-452f-a4c2-03f809022134",
"type": "tool_call",
}


def test_tool_args_schema_with_annotated_type() -> None:
@tool
def test_tool(
query_fragments: Annotated[
list[str],
"A list of query fragments",
],
) -> list[str]:
"""Search the Internet and retrieve relevant result items."""
return []

assert test_tool.args == {
"query_fragments": {
"description": "A list of query fragments",
"items": {"type": "string"},
"title": "Query Fragments",
"type": "array",
}
}
Loading