From 623cb4bbdd1bb4414b9bbee7c7ca6f8878d316fa Mon Sep 17 00:00:00 2001 From: Zamil Majdy Date: Tue, 27 Jan 2026 10:23:56 -0600 Subject: [PATCH] fix: save assistant message with tool_call before pending result The LLM requires an assistant message with tool_calls to exist before any tool result messages. This fixes the error: "unexpected tool_use_id found in tool_result blocks" --- .../backend/backend/api/features/chat/service.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/autogpt_platform/backend/backend/api/features/chat/service.py b/autogpt_platform/backend/backend/api/features/chat/service.py index 9dab26d9b5..1c5b0c4b9c 100644 --- a/autogpt_platform/backend/backend/api/features/chat/service.py +++ b/autogpt_platform/backend/backend/api/features/chat/service.py @@ -1384,7 +1384,15 @@ async def _yield_tool_call( "check back in a few minutes." ) - # Save "pending" tool response to chat history immediately + # Save assistant message with tool_call FIRST (required by LLM) + assistant_message = ChatMessage( + role="assistant", + content="", + tool_calls=[tool_calls[yield_idx]], + ) + session.messages.append(assistant_message) + + # Then save pending tool result pending_message = ChatMessage( role="tool", content=OperationPendingResponse(