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"
This commit is contained in:
Zamil Majdy
2026-01-27 10:23:56 -06:00
parent b2e4820fcf
commit 623cb4bbdd

View File

@@ -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(