(dec_func: Union[Callable, Runnable])
| 892 | |
| 893 | def _make_with_name(tool_name: str) -> Callable: |
| 894 | def _make_tool(dec_func: Union[Callable, Runnable]) -> BaseTool: |
| 895 | if isinstance(dec_func, Runnable): |
| 896 | runnable = dec_func |
| 897 | |
| 898 | if runnable.input_schema.schema().get("type") != "object": |
| 899 | raise ValueError("Runnable must have an object schema.") |
| 900 | |
| 901 | async def ainvoke_wrapper( |
| 902 | callbacks: Optional[Callbacks] = None, **kwargs: Any |
| 903 | ) -> Any: |
| 904 | return await runnable.ainvoke(kwargs, {"callbacks": callbacks}) |
| 905 | |
| 906 | def invoke_wrapper( |
| 907 | callbacks: Optional[Callbacks] = None, **kwargs: Any |
| 908 | ) -> Any: |
| 909 | return runnable.invoke(kwargs, {"callbacks": callbacks}) |
| 910 | |
| 911 | coroutine = ainvoke_wrapper |
| 912 | func = invoke_wrapper |
| 913 | schema: Optional[Type[BaseModel]] = runnable.input_schema |
| 914 | description = repr(runnable) |
| 915 | elif inspect.iscoroutinefunction(dec_func): |
| 916 | coroutine = dec_func |
| 917 | func = None |
| 918 | schema = args_schema |
| 919 | description = None |
| 920 | else: |
| 921 | coroutine = None |
| 922 | func = dec_func |
| 923 | schema = args_schema |
| 924 | description = None |
| 925 | |
| 926 | if infer_schema or args_schema is not None: |
| 927 | return StructuredTool.from_function( |
| 928 | func, |
| 929 | coroutine, |
| 930 | name=tool_name, |
| 931 | description=description, |
| 932 | return_direct=return_direct, |
| 933 | args_schema=schema, |
| 934 | infer_schema=infer_schema, |
| 935 | ) |
| 936 | # If someone doesn't want a schema applied, we must treat it as |
| 937 | # a simple string->string function |
| 938 | if func.__doc__ is None: |
| 939 | raise ValueError( |
| 940 | "Function must have a docstring if " |
| 941 | "description not provided and infer_schema is False." |
| 942 | ) |
| 943 | return Tool( |
| 944 | name=tool_name, |
| 945 | func=func, |
| 946 | description=f"{tool_name} tool", |
| 947 | return_direct=return_direct, |
| 948 | coroutine=coroutine, |
| 949 | ) |
| 950 | |
| 951 | return _make_tool |
nothing calls this directly
no test coverage detected