@@ -537,6 +537,15 @@ def __call__(self, *args: Any, **kwargs: Any) -> Any:
537537 self .invocation_exception_count += 1
538538 raise
539539
540+ async def _invoke_function (self , call_kwargs : Mapping [str , Any ]) -> Any :
541+ """Run sync tools off the event loop during async invocation."""
542+ func = self .func .func if isinstance (self .func , FunctionTool ) else self .func
543+ if inspect .iscoroutinefunction (func ):
544+ return await self .__call__ (** call_kwargs )
545+
546+ res = await asyncio .to_thread (self .__call__ , ** call_kwargs )
547+ return await res if inspect .isawaitable (res ) else res
548+
540549 @overload
541550 async def invoke (
542551 self ,
@@ -679,8 +688,7 @@ async def invoke(
679688 if not OBSERVABILITY_SETTINGS .ENABLED : # type: ignore[name-defined]
680689 logger .info (f"Function name: { self .name } " )
681690 logger .debug (f"Function arguments: { observable_kwargs } " )
682- res = self .__call__ (** call_kwargs )
683- result = await res if inspect .isawaitable (res ) else res
691+ result = await self ._invoke_function (call_kwargs )
684692 if skip_parsing :
685693 logger .info (f"Function { self .name } succeeded." )
686694 logger .debug (f"Function result: { type (result ).__name__ } " )
@@ -730,8 +738,7 @@ async def invoke(
730738 start_time_stamp = perf_counter ()
731739 end_time_stamp : float | None = None
732740 try :
733- res = self .__call__ (** call_kwargs )
734- result = await res if inspect .isawaitable (res ) else res
741+ result = await self ._invoke_function (call_kwargs )
735742 end_time_stamp = perf_counter ()
736743 except Exception as exception :
737744 end_time_stamp = perf_counter ()
0 commit comments