From 4adf0fd585c57cdc4301ac37c93d7a913c1aae78 Mon Sep 17 00:00:00 2001 From: Mark Backman Date: Mon, 30 Mar 2026 14:24:04 -0400 Subject: [PATCH] Handle incomplete function call arguments from interrupted LLM streams When a user interruption causes the LLM chunk stream to exit early, function call arguments may be incomplete JSON. Wrap json.loads() in try/except JSONDecodeError to skip malformed function calls with a warning instead of crashing. Fixes #2461. --- src/pipecat/services/google/openai/llm.py | 6 +++++- src/pipecat/services/openai/base_llm.py | 6 +++++- src/pipecat/services/sambanova/llm.py | 6 +++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/pipecat/services/google/openai/llm.py b/src/pipecat/services/google/openai/llm.py index da5d1be7a..08a7bcd1b 100644 --- a/src/pipecat/services/google/openai/llm.py +++ b/src/pipecat/services/google/openai/llm.py @@ -199,7 +199,11 @@ class GoogleLLMOpenAIBetaService(OpenAILLMService): # which currently results in an empty function name(''). continue - arguments = json.loads(arguments) + try: + arguments = json.loads(arguments) + except json.JSONDecodeError: + logger.warning(f"{self}: Failed to parse function call arguments: {arguments}") + continue function_calls.append( FunctionCallFromLLM( diff --git a/src/pipecat/services/openai/base_llm.py b/src/pipecat/services/openai/base_llm.py index c06ed3afd..5b444aea1 100644 --- a/src/pipecat/services/openai/base_llm.py +++ b/src/pipecat/services/openai/base_llm.py @@ -567,7 +567,11 @@ class BaseOpenAILLMService(LLMService): for function_name, arguments, tool_id in zip( functions_list, arguments_list, tool_id_list ): - arguments = json.loads(arguments) + try: + arguments = json.loads(arguments) + except json.JSONDecodeError: + logger.warning(f"{self}: Failed to parse function call arguments: {arguments}") + continue function_calls.append( FunctionCallFromLLM( context=context, diff --git a/src/pipecat/services/sambanova/llm.py b/src/pipecat/services/sambanova/llm.py index 20e17b4f2..63f37cb43 100644 --- a/src/pipecat/services/sambanova/llm.py +++ b/src/pipecat/services/sambanova/llm.py @@ -245,7 +245,11 @@ class SambaNovaLLMService(OpenAILLMService): # type: ignore if len(arguments) < 1: continue - arguments = json.loads(arguments) + try: + arguments = json.loads(arguments) + except json.JSONDecodeError: + logger.warning(f"{self}: Failed to parse function call arguments: {arguments}") + continue function_calls.append( FunctionCallFromLLM( context=context,