fix: prevent duplicate tool messages for long-running operations

Skip saving operation_started and operation_in_progress responses to
tool_response_messages since the pending message is already saved to
the session. This prevents duplicate tool_result blocks which caused
LLM errors ("each tool_use must have a single result").
This commit is contained in:
Zamil Majdy
2026-01-27 10:19:01 -06:00
parent 8627366b1c
commit b2e4820fcf

View File

@@ -427,13 +427,19 @@ async def stream_chat_completion(
if isinstance(chunk.output, str)
else orjson.dumps(chunk.output).decode("utf-8")
)
tool_response_messages.append(
ChatMessage(
role="tool",
content=result_content,
tool_call_id=chunk.toolCallId,
)
# Skip saving long-running operation responses - pending message already saved
is_long_running_response = any(
op_type in result_content
for op_type in ('"operation_started"', '"operation_in_progress"')
)
if not is_long_running_response:
tool_response_messages.append(
ChatMessage(
role="tool",
content=result_content,
tool_call_id=chunk.toolCallId,
)
)
has_done_tool_call = True
# Track if any tool execution failed
if not chunk.success: