From 86e726107fd7f7dd5aa60c6e9e5028fb8b8c749d Mon Sep 17 00:00:00 2001 From: borislav Date: Tue, 14 Apr 2026 22:40:45 +0200 Subject: [PATCH 1/5] fix: fail missing tool calls cleanly --- src/pipecat/services/llm_service.py | 19 +++-- tests/test_llm_service.py | 116 ++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 7 deletions(-) create mode 100644 tests/test_llm_service.py diff --git a/src/pipecat/services/llm_service.py b/src/pipecat/services/llm_service.py index 036818370..f8a0baae0 100644 --- a/src/pipecat/services/llm_service.py +++ b/src/pipecat/services/llm_service.py @@ -725,7 +725,11 @@ class LLMService(UserTurnCompletionLLMServiceMixin, AIService): logger.warning( f"{self} is calling '{function_call.function_name}', but it's not registered." ) - continue + item = FunctionCallRegistryItem( + function_name=function_call.function_name, + handler=self._missing_function_call_handler, + cancel_on_interruption=True, + ) runner_items.append( FunctionCallRunnerItem( @@ -782,12 +786,7 @@ class LLMService(UserTurnCompletionLLMServiceMixin, AIService): await self._sequential_runner_queue.put(runner_item) async def _run_function_call(self, runner_item: FunctionCallRunnerItem): - 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] - else: - return + item = runner_item.registry_item logger.debug( f"{self} Calling function [{runner_item.function_name}:{runner_item.tool_call_id}] with arguments {runner_item.arguments}" @@ -894,6 +893,12 @@ class LLMService(UserTurnCompletionLLMServiceMixin, AIService): if timeout_task and not timeout_task.done(): await self.cancel_task(timeout_task) + 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( diff --git a/tests/test_llm_service.py b/tests/test_llm_service.py new file mode 100644 index 000000000..0bde008b5 --- /dev/null +++ b/tests/test_llm_service.py @@ -0,0 +1,116 @@ +# +# Copyright (c) 2024-2026, Daily +# +# SPDX-License-Identifier: BSD 2-Clause License +# + +import unittest +from unittest.mock import AsyncMock + +from pipecat.frames.frames import ( + FunctionCallFromLLM, + FunctionCallInProgressFrame, + FunctionCallResultFrame, + FunctionCallsStartedFrame, +) +from pipecat.processors.aggregators.llm_context import LLMContext +from pipecat.services.llm_service import LLMService +from pipecat.services.settings import LLMSettings +from pipecat.turns.user_mute.function_call_user_mute_strategy import FunctionCallUserMuteStrategy + + +class MockLLMService(LLMService): + """Minimal LLM service for testing function call execution.""" + + def __init__(self, **kwargs): + settings = LLMSettings( + model="test-model", + system_instruction=None, + temperature=None, + max_tokens=None, + top_p=None, + top_k=None, + frequency_penalty=None, + presence_penalty=None, + seed=None, + filter_incomplete_user_turns=None, + user_turn_completion_config=None, + ) + super().__init__(settings=settings, **kwargs) + + +class TestLLMService(unittest.IsolatedAsyncioTestCase): + async def _run_function_calls_inline(self, service: MockLLMService): + async def run_inline(runner_items): + for runner_item in runner_items: + await service._run_function_call(runner_item) + + service._run_parallel_function_calls = run_inline + service._run_sequential_function_calls = run_inline + + async def test_missing_function_call_emits_terminal_result(self): + service = MockLLMService() + service._call_event_handler = AsyncMock() + await self._run_function_calls_inline(service) + + recorded_frames = [] + + async def mock_broadcast_frame(frame_cls, **kwargs): + recorded_frames.append(frame_cls(**kwargs)) + + service.broadcast_frame = mock_broadcast_frame + + await service.run_function_calls( + [ + FunctionCallFromLLM( + function_name="missing_tool", + tool_call_id="call_1", + arguments={"query": "weather"}, + context=LLMContext(), + ) + ] + ) + + self.assertEqual( + [type(frame) for frame in recorded_frames], + [ + FunctionCallsStartedFrame, + FunctionCallInProgressFrame, + FunctionCallResultFrame, + ], + ) + self.assertEqual(recorded_frames[1].function_name, "missing_tool") + self.assertEqual( + recorded_frames[2].result, + "Error: function 'missing_tool' is not registered.", + ) + + async def test_missing_function_call_allows_user_mute_cleanup(self): + service = MockLLMService() + service._call_event_handler = AsyncMock() + await self._run_function_calls_inline(service) + + recorded_frames = [] + + async def mock_broadcast_frame(frame_cls, **kwargs): + recorded_frames.append(frame_cls(**kwargs)) + + service.broadcast_frame = mock_broadcast_frame + + await service.run_function_calls( + [ + FunctionCallFromLLM( + function_name="missing_tool", + tool_call_id="call_1", + arguments={}, + context=LLMContext(), + ) + ] + ) + + strategy = FunctionCallUserMuteStrategy() + muted = False + for frame in recorded_frames: + muted = await strategy.process_frame(frame) + + self.assertFalse(muted) From 14cf783647979e7d26bb14bc728fef7371ba77e1 Mon Sep 17 00:00:00 2001 From: borislav Date: Tue, 14 Apr 2026 22:41:09 +0200 Subject: [PATCH 2/5] chore: add changelog for #4301 --- changelog/4301.fixed.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/4301.fixed.md diff --git a/changelog/4301.fixed.md b/changelog/4301.fixed.md new file mode 100644 index 000000000..1ee4b092d --- /dev/null +++ b/changelog/4301.fixed.md @@ -0,0 +1 @@ +- Fixed missing tool handlers so unregistered tool calls fail with a normal final tool result instead of leaving tool-call state hanging. From 822392b0d451658be9ad7da99f5699566cd117d1 Mon Sep 17 00:00:00 2001 From: borislav Date: Mon, 27 Apr 2026 17:22:30 +0200 Subject: [PATCH 3/5] fix: re-resolve registry item at execution time Address review feedback: a function may be unregistered between when run_function_calls queues it and when _run_function_call executes it. Restore the live lookup, falling back to the missing-function handler when the entry is gone, so the call still terminates with a normal tool result. Factor the missing-handler item construction into a helper since it's now built in two places. --- src/pipecat/services/llm_service.py | 32 +++++++++++++++--- tests/test_llm_service.py | 50 +++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/src/pipecat/services/llm_service.py b/src/pipecat/services/llm_service.py index f8a0baae0..fc4c6ce89 100644 --- a/src/pipecat/services/llm_service.py +++ b/src/pipecat/services/llm_service.py @@ -725,10 +725,8 @@ class LLMService(UserTurnCompletionLLMServiceMixin, AIService): logger.warning( f"{self} is calling '{function_call.function_name}', but it's not registered." ) - item = FunctionCallRegistryItem( - function_name=function_call.function_name, - handler=self._missing_function_call_handler, - cancel_on_interruption=True, + item = self._build_missing_function_call_registry_item( + function_call.function_name ) runner_items.append( @@ -786,7 +784,21 @@ class LLMService(UserTurnCompletionLLMServiceMixin, AIService): await self._sequential_runner_queue.put(runner_item) async def _run_function_call(self, runner_item: FunctionCallRunnerItem): - item = runner_item.registry_item + # 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 is self._missing_function_call_handler: + item = runner_item.registry_item + else: + 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}" @@ -893,6 +905,16 @@ 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( diff --git a/tests/test_llm_service.py b/tests/test_llm_service.py index 0bde008b5..057fafb1e 100644 --- a/tests/test_llm_service.py +++ b/tests/test_llm_service.py @@ -85,6 +85,56 @@ class TestLLMService(unittest.IsolatedAsyncioTestCase): "Error: function 'missing_tool' is not registered.", ) + async def test_function_unregistered_between_queue_and_execute(self): + """Function unregistered between queuing and execution still terminates.""" + service = MockLLMService() + service._call_event_handler = AsyncMock() + + async def real_handler(params): + await params.result_callback("should not be called") + + service.register_function("doomed_tool", real_handler) + + recorded_frames = [] + + async def mock_broadcast_frame(frame_cls, **kwargs): + recorded_frames.append(frame_cls(**kwargs)) + + service.broadcast_frame = mock_broadcast_frame + + async def run_inline(runner_items): + # Simulate the function being unregistered after queuing but before execution. + service.unregister_function("doomed_tool") + for runner_item in runner_items: + await service._run_function_call(runner_item) + + service._run_parallel_function_calls = run_inline + service._run_sequential_function_calls = run_inline + + await service.run_function_calls( + [ + FunctionCallFromLLM( + function_name="doomed_tool", + tool_call_id="call_1", + arguments={}, + context=LLMContext(), + ) + ] + ) + + self.assertEqual( + [type(frame) for frame in recorded_frames], + [ + FunctionCallsStartedFrame, + FunctionCallInProgressFrame, + FunctionCallResultFrame, + ], + ) + self.assertEqual( + recorded_frames[2].result, + "Error: function 'doomed_tool' is not registered.", + ) + async def test_missing_function_call_allows_user_mute_cleanup(self): service = MockLLMService() service._call_event_handler = AsyncMock() From 8869e25142d7de45d13976cfdefdc5d2cdb559c5 Mon Sep 17 00:00:00 2001 From: borislav Date: Mon, 27 Apr 2026 17:34:31 +0200 Subject: [PATCH 4/5] fix: compare bound method by equality, not identity Bound methods are created fresh on each attribute access, so 'self._missing_function_call_handler is self._missing_function_call_handler' is always False. Using 'is' meant the placeholder branch never fired and both warnings logged when a function was missing at queue time. Switch to == so equality compares the underlying function and instance. Strengthen the missing-at-queue-time test to assert the second warning does not fire. --- src/pipecat/services/llm_service.py | 2 +- tests/test_llm_service.py | 29 ++++++++++++++++++----------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/pipecat/services/llm_service.py b/src/pipecat/services/llm_service.py index fc4c6ce89..1f98c1603 100644 --- a/src/pipecat/services/llm_service.py +++ b/src/pipecat/services/llm_service.py @@ -792,7 +792,7 @@ class LLMService(UserTurnCompletionLLMServiceMixin, AIService): item = self._functions[runner_item.function_name] elif None in self._functions.keys(): item = self._functions[None] - elif runner_item.registry_item.handler is self._missing_function_call_handler: + elif runner_item.registry_item.handler == self._missing_function_call_handler: item = runner_item.registry_item else: logger.warning( diff --git a/tests/test_llm_service.py b/tests/test_llm_service.py index 057fafb1e..c7018d9f0 100644 --- a/tests/test_llm_service.py +++ b/tests/test_llm_service.py @@ -5,7 +5,7 @@ # import unittest -from unittest.mock import AsyncMock +from unittest.mock import AsyncMock, patch from pipecat.frames.frames import ( FunctionCallFromLLM, @@ -60,16 +60,17 @@ class TestLLMService(unittest.IsolatedAsyncioTestCase): service.broadcast_frame = mock_broadcast_frame - await service.run_function_calls( - [ - FunctionCallFromLLM( - function_name="missing_tool", - tool_call_id="call_1", - arguments={"query": "weather"}, - context=LLMContext(), - ) - ] - ) + with patch("pipecat.services.llm_service.logger") as mock_logger: + await service.run_function_calls( + [ + FunctionCallFromLLM( + function_name="missing_tool", + tool_call_id="call_1", + arguments={"query": "weather"}, + context=LLMContext(), + ) + ] + ) self.assertEqual( [type(frame) for frame in recorded_frames], @@ -85,6 +86,12 @@ class TestLLMService(unittest.IsolatedAsyncioTestCase): "Error: function 'missing_tool' is not registered.", ) + # Only the queue-time warning should fire; the execution-time + # "just unregistered" warning must not double-log. + warnings = [c.args[0] for c in mock_logger.warning.call_args_list] + self.assertTrue(any("not registered" in w for w in warnings)) + self.assertFalse(any("just unregistered" in w for w in warnings)) + async def test_function_unregistered_between_queue_and_execute(self): """Function unregistered between queuing and execution still terminates.""" service = MockLLMService() From 2520243d9dce880a28ed5bdc80e235c83311c105 Mon Sep 17 00:00:00 2001 From: Paul Kompfner Date: Mon, 27 Apr 2026 11:48:27 -0400 Subject: [PATCH 5/5] style: apply ruff format --- src/pipecat/services/llm_service.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/pipecat/services/llm_service.py b/src/pipecat/services/llm_service.py index 1f98c1603..44de3b8b9 100644 --- a/src/pipecat/services/llm_service.py +++ b/src/pipecat/services/llm_service.py @@ -725,9 +725,7 @@ class LLMService(UserTurnCompletionLLMServiceMixin, AIService): logger.warning( f"{self} is calling '{function_call.function_name}', but it's not registered." ) - item = self._build_missing_function_call_registry_item( - function_call.function_name - ) + item = self._build_missing_function_call_registry_item(function_call.function_name) runner_items.append( FunctionCallRunnerItem( @@ -917,9 +915,7 @@ class LLMService(UserTurnCompletionLLMServiceMixin, AIService): 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." - ) + 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."""