From c54216706502bd095872c76a0fb93982057e7d6e Mon Sep 17 00:00:00 2001 From: filipi87 Date: Fri, 10 Apr 2026 15:06:39 -0300 Subject: [PATCH] Refactored on_function_calls_cancelled to use FunctionCallFromLLM. --- ...function-calling-anthropic-async-stream.py | 4 ++-- .../function-calling-anthropic-async.py | 4 ++-- .../function-calling-google-async-stream.py | 4 ++-- .../function-calling-google-async.py | 4 ++-- .../function-calling-openai-async-stream.py | 4 ++-- .../function-calling-openai-async.py | 4 ++-- ...n-calling-openai-responses-async-stream.py | 4 ++-- ...function-calling-openai-responses-async.py | 4 ++-- src/pipecat/services/llm_service.py | 24 +++++++++++++++---- 9 files changed, 35 insertions(+), 21 deletions(-) diff --git a/examples/function-calling/function-calling-anthropic-async-stream.py b/examples/function-calling/function-calling-anthropic-async-stream.py index 8dc62481a..52069af68 100644 --- a/examples/function-calling/function-calling-anthropic-async-stream.py +++ b/examples/function-calling/function-calling-anthropic-async-stream.py @@ -141,8 +141,8 @@ async def run_bot(transport: BaseTransport, runner_args: RunnerArguments): ) @llm.event_handler("on_function_calls_cancelled") - async def on_function_calls_cancelled(service, cancelled): - for item in cancelled: + async def on_function_calls_cancelled(service, function_calls): + for item in function_calls: logger.info(f"Function call cancelled: {item.function_name} [{item.tool_call_id}]") location_function = FunctionSchema( diff --git a/examples/function-calling/function-calling-anthropic-async.py b/examples/function-calling/function-calling-anthropic-async.py index 62baaaa1b..2c5fb9402 100644 --- a/examples/function-calling/function-calling-anthropic-async.py +++ b/examples/function-calling/function-calling-anthropic-async.py @@ -94,8 +94,8 @@ async def run_bot(transport: BaseTransport, runner_args: RunnerArguments): llm.register_function("get_restaurant_recommendation", fetch_restaurant_recommendation) @llm.event_handler("on_function_calls_cancelled") - async def on_function_calls_cancelled(service, cancelled): - for item in cancelled: + async def on_function_calls_cancelled(service, function_calls): + for item in function_calls: logger.info(f"Function call cancelled: {item.function_name} [{item.tool_call_id}]") weather_function = FunctionSchema( diff --git a/examples/function-calling/function-calling-google-async-stream.py b/examples/function-calling/function-calling-google-async-stream.py index 63c83318e..503880ca6 100644 --- a/examples/function-calling/function-calling-google-async-stream.py +++ b/examples/function-calling/function-calling-google-async-stream.py @@ -145,8 +145,8 @@ async def run_bot(transport: BaseTransport, runner_args: RunnerArguments): await tts.queue_frame(TTSSpeakFrame("Sure, tracking your location now.")) @llm.event_handler("on_function_calls_cancelled") - async def on_function_calls_cancelled(service, cancelled): - for item in cancelled: + async def on_function_calls_cancelled(service, function_calls): + for item in function_calls: logger.info(f"Function call cancelled: {item.function_name} [{item.tool_call_id}]") location_function = FunctionSchema( diff --git a/examples/function-calling/function-calling-google-async.py b/examples/function-calling/function-calling-google-async.py index 89bae24d4..dbc86d664 100644 --- a/examples/function-calling/function-calling-google-async.py +++ b/examples/function-calling/function-calling-google-async.py @@ -142,8 +142,8 @@ indicate you should use the get_image tool are: await tts.queue_frame(TTSSpeakFrame("Let me check on that.")) @llm.event_handler("on_function_calls_cancelled") - async def on_function_calls_cancelled(service, cancelled): - for item in cancelled: + async def on_function_calls_cancelled(service, function_calls): + for item in function_calls: logger.info(f"Function call cancelled: {item.function_name} [{item.tool_call_id}]") weather_function = FunctionSchema( diff --git a/examples/function-calling/function-calling-openai-async-stream.py b/examples/function-calling/function-calling-openai-async-stream.py index b91a0eac1..a60a5198f 100644 --- a/examples/function-calling/function-calling-openai-async-stream.py +++ b/examples/function-calling/function-calling-openai-async-stream.py @@ -145,8 +145,8 @@ async def run_bot(transport: BaseTransport, runner_args: RunnerArguments): await tts.queue_frame(TTSSpeakFrame("Sure, tracking your location now.")) @llm.event_handler("on_function_calls_cancelled") - async def on_function_calls_cancelled(service, cancelled): - for item in cancelled: + async def on_function_calls_cancelled(service, function_calls): + for item in function_calls: logger.info(f"Function call cancelled: {item.function_name} [{item.tool_call_id}]") location_function = FunctionSchema( diff --git a/examples/function-calling/function-calling-openai-async.py b/examples/function-calling/function-calling-openai-async.py index 79a4ccad0..31c932d5f 100644 --- a/examples/function-calling/function-calling-openai-async.py +++ b/examples/function-calling/function-calling-openai-async.py @@ -108,8 +108,8 @@ async def run_bot(transport: BaseTransport, runner_args: RunnerArguments): await tts.queue_frame(TTSSpeakFrame("Let me check on that.")) @llm.event_handler("on_function_calls_cancelled") - async def on_function_calls_cancelled(service, cancelled): - for item in cancelled: + async def on_function_calls_cancelled(service, function_calls): + for item in function_calls: logger.info(f"Function call cancelled: {item.function_name} [{item.tool_call_id}]") weather_function = FunctionSchema( diff --git a/examples/function-calling/function-calling-openai-responses-async-stream.py b/examples/function-calling/function-calling-openai-responses-async-stream.py index 093e0d47b..745c18ae1 100644 --- a/examples/function-calling/function-calling-openai-responses-async-stream.py +++ b/examples/function-calling/function-calling-openai-responses-async-stream.py @@ -145,8 +145,8 @@ async def run_bot(transport: BaseTransport, runner_args: RunnerArguments): await tts.queue_frame(TTSSpeakFrame("Sure, tracking your location now.")) @llm.event_handler("on_function_calls_cancelled") - async def on_function_calls_cancelled(service, cancelled): - for item in cancelled: + async def on_function_calls_cancelled(service, function_calls): + for item in function_calls: logger.info(f"Function call cancelled: {item.function_name} [{item.tool_call_id}]") location_function = FunctionSchema( diff --git a/examples/function-calling/function-calling-openai-responses-async.py b/examples/function-calling/function-calling-openai-responses-async.py index 8e6fb3fa9..368f41fe1 100644 --- a/examples/function-calling/function-calling-openai-responses-async.py +++ b/examples/function-calling/function-calling-openai-responses-async.py @@ -106,8 +106,8 @@ async def run_bot(transport: BaseTransport, runner_args: RunnerArguments): await tts.queue_frame(TTSSpeakFrame("Let me check on that.", append_to_context=False)) @llm.event_handler("on_function_calls_cancelled") - async def on_function_calls_cancelled(service, cancelled): - for item in cancelled: + async def on_function_calls_cancelled(service, function_calls): + for item in function_calls: logger.info(f"Function call cancelled: {item.function_name} [{item.tool_call_id}]") weather_function = FunctionSchema( diff --git a/src/pipecat/services/llm_service.py b/src/pipecat/services/llm_service.py index b1ca29346..036818370 100644 --- a/src/pipecat/services/llm_service.py +++ b/src/pipecat/services/llm_service.py @@ -195,12 +195,12 @@ class LLMService(UserTurnCompletionLLMServiceMixin, AIService): logger.warning("LLM completion timed out") @task.event_handler("on_function_calls_started") - async def on_function_calls_started(service, function_calls): + async def on_function_calls_started(service, function_calls: List[FunctionCallFromLLM]): logger.info(f"Starting {len(function_calls)} function calls") @task.event_handler("on_function_calls_cancelled") - async def on_function_calls_cancelled(service, cancelled): - logger.info(f"Cancelled {len(cancelled)} async function calls") + async def on_function_calls_cancelled(service, function_calls: List[FunctionCallFromLLM]): + logger.info(f"Cancelled {len(function_calls)} function calls") """ _settings: LLMSettings @@ -987,7 +987,14 @@ class LLMService(UserTurnCompletionLLMServiceMixin, AIService): FunctionCallCancelFrame, function_name=name, tool_call_id=tool_call_id ) - cancelled_items.append(runner_item) + cancelled_items.append( + FunctionCallFromLLM( + function_name=runner_item.function_name, + tool_call_id=runner_item.tool_call_id, + arguments=runner_item.arguments, + context=runner_item.context, + ) + ) logger.debug(f"{self} Async function call [{name}:{tool_call_id}] cancelled") for task in cancelled_tasks: @@ -1018,7 +1025,14 @@ class LLMService(UserTurnCompletionLLMServiceMixin, AIService): FunctionCallCancelFrame, function_name=name, tool_call_id=tool_call_id ) - cancelled_items.append(runner_item) + cancelled_items.append( + FunctionCallFromLLM( + function_name=runner_item.function_name, + tool_call_id=runner_item.tool_call_id, + arguments=runner_item.arguments, + context=runner_item.context, + ) + ) logger.debug(f"{self} Function call [{name}:{tool_call_id}] has been cancelled") # Remove all cancelled tasks from our set.