Fix async callable object tools (#568)

This commit is contained in:
Stephan Lensky 2025-05-23 12:21:53 -04:00 • committed by GitHub
commit f2f4dbdcbd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,5 +1,6 @@
from __future__ import annotations as _annotations
import functools
import inspect
from collections.abc import Callable
from typing import TYPE_CHECKING, Any, get_origin
@ -53,7 +54,7 @@ class Tool(BaseModel):
raise ValueError("You must provide a name for lambda functions")
func_doc = description or fn.__doc__ or ""
is_async = inspect.iscoroutinefunction(fn)
is_async = _is_async_callable(fn)
if context_kwarg is None:
sig = inspect.signature(fn)
@ -98,3 +99,12 @@ class Tool(BaseModel):
)
except Exception as e:
raise ToolError(f"Error executing tool {self.name}: {e}") from e
def _is_async_callable(obj: Any) -> bool:
while isinstance(obj, functools.partial):
obj = obj.func
return inspect.iscoroutinefunction(obj) or (
callable(obj) and inspect.iscoroutinefunction(getattr(obj, "__call__", None))
)