Merge pull request #4301 from bnovik0v/fix-4300-missing-tool-lifecycle
Fail missing tool calls cleanly
This commit is contained in:
@@ -737,7 +737,7 @@ class LLMService(UserTurnCompletionLLMServiceMixin, AIService):
|
||||
logger.warning(
|
||||
f"{self} is calling '{function_call.function_name}', but it's not registered."
|
||||
)
|
||||
continue
|
||||
item = self._build_missing_function_call_registry_item(function_call.function_name)
|
||||
|
||||
runner_items.append(
|
||||
FunctionCallRunnerItem(
|
||||
@@ -794,12 +794,21 @@ class LLMService(UserTurnCompletionLLMServiceMixin, AIService):
|
||||
await self._sequential_runner_queue.put(runner_item)
|
||||
|
||||
async def _run_function_call(self, runner_item: FunctionCallRunnerItem):
|
||||
# Re-resolve the registry item at execution time. The function may have
|
||||
# been unregistered between queuing and execution, in which case we
|
||||
# fall back to the missing-function handler so the call still terminates
|
||||
# with a normal tool result.
|
||||
if runner_item.function_name in self._functions.keys():
|
||||
item = self._functions[runner_item.function_name]
|
||||
elif None in self._functions.keys():
|
||||
item = self._functions[None]
|
||||
elif runner_item.registry_item.handler == self._missing_function_call_handler:
|
||||
item = runner_item.registry_item
|
||||
else:
|
||||
return
|
||||
logger.warning(
|
||||
f"{self} is calling '{runner_item.function_name}', but it was just unregistered."
|
||||
)
|
||||
item = self._build_missing_function_call_registry_item(runner_item.function_name)
|
||||
|
||||
logger.debug(
|
||||
f"{self} Calling function [{runner_item.function_name}:{runner_item.tool_call_id}] with arguments {runner_item.arguments}"
|
||||
@@ -908,6 +917,20 @@ class LLMService(UserTurnCompletionLLMServiceMixin, AIService):
|
||||
if timeout_task and not timeout_task.done():
|
||||
await self.cancel_task(timeout_task)
|
||||
|
||||
def _build_missing_function_call_registry_item(
|
||||
self, function_name: str
|
||||
) -> FunctionCallRegistryItem:
|
||||
"""Build a registry item that routes to the missing-function handler."""
|
||||
return FunctionCallRegistryItem(
|
||||
function_name=function_name,
|
||||
handler=self._missing_function_call_handler,
|
||||
cancel_on_interruption=True,
|
||||
)
|
||||
|
||||
async def _missing_function_call_handler(self, params: FunctionCallParams):
|
||||
"""Return a terminal tool result when the LLM calls an unknown function."""
|
||||
await params.result_callback(f"Error: function '{params.function_name}' is not registered.")
|
||||
|
||||
def _has_async_tools(self) -> bool:
|
||||
"""Return True if at least one non-builtin async tool is registered."""
|
||||
return any(
|
||||
|
||||
Reference in New Issue
Block a user