diff --git a/private_gpt/components/engines/chat/async_chat_engine.py b/private_gpt/components/engines/chat/async_chat_engine.py index 969859d7..2b71bb12 100644 --- a/private_gpt/components/engines/chat/async_chat_engine.py +++ b/private_gpt/components/engines/chat/async_chat_engine.py @@ -1168,13 +1168,16 @@ class AsyncChatEngine: existing = assistant_message.additional_kwargs.get("tool_calls") if not isinstance(existing, list): existing = [] - seen_ids = {tc.tool_id for tc in existing if tc.tool_id} - merged = list(existing) + by_id = {} + for tc in existing: + if tc.tool_id: + by_id[tc.tool_id] = tc for tc in value: - if tc.tool_id and tc.tool_id not in seen_ids: - merged.append(tc) - seen_ids.add(tc.tool_id) - assistant_message.additional_kwargs["tool_calls"] = merged + if tc.tool_id: + by_id[tc.tool_id] = tc + assistant_message.additional_kwargs["tool_calls"] = list( + by_id.values() + ) continue assistant_message.additional_kwargs[key] = value diff --git a/private_gpt/components/engines/chat/chat_engine.py b/private_gpt/components/engines/chat/chat_engine.py index eb4e2376..c1c27237 100644 --- a/private_gpt/components/engines/chat/chat_engine.py +++ b/private_gpt/components/engines/chat/chat_engine.py @@ -600,13 +600,16 @@ class ChatLoopEngine: existing = assistant_message.additional_kwargs.get("tool_calls") if not isinstance(existing, list): existing = [] - seen_ids = {tc.tool_id for tc in existing if tc.tool_id} - merged = list(existing) + by_id = {} + for tc in existing: + if tc.tool_id: + by_id[tc.tool_id] = tc for tc in value: - if tc.tool_id and tc.tool_id not in seen_ids: - merged.append(tc) - seen_ids.add(tc.tool_id) - assistant_message.additional_kwargs["tool_calls"] = merged + if tc.tool_id: + by_id[tc.tool_id] = tc + assistant_message.additional_kwargs["tool_calls"] = list( + by_id.values() + ) continue assistant_message.additional_kwargs[key] = value