-
Notifications
You must be signed in to change notification settings - Fork 141
Description
Describe the bug
The message history has an incorrect structure when calling tools. OpenAI returns the following error:
An assistant message with 'tool_calls' must be followed by tool messages responding to ea (truncated...)
Additionally, I have observed that the first agent message appears twice.
To Reproduce
To reproduce the behaviour it is needed to implement DatabaseChatHistory.
My testing code is:
`
public function __construct(string $chat_id, int $contextWindow = 50000)
{
parent::__construct($contextWindow);
$this->chat_id = $chat_id;
$this->init();
}
protected function init(): void
{
$value = DB::table('CAS_CallService')->where('CAS_ID', '=', $this->chat_id)->value('CAS_History');
if ($value) {
$messages = \json_decode($value, true);
$this->history = $this->unserializeMessages($messages);
}
}
protected function storeMessage(Message $message): ChatHistoryInterface
{
$this->updateHistoryColumn();
return $this;
}
public function removeOldestMessage(): ChatHistoryInterface
{
$this->updateHistoryColumn();
return $this;
}
protected function clear(): ChatHistoryInterface
{
DB::table('CAS_CallService')->where('CAS_ID', '=', $this->chat_id)->update(['CAS_History' => null]);
$this->history = [];
return $this;
}
protected function updateHistoryColumn(): void
{
DB::table('CAS_CallService')->where('CAS_ID', '=', $this->chat_id)->update(['CAS_History' => json_encode($this->jsonSerialize())]);
}
}
`
Expected behavior
Agent cannot load messages history from DatabaseChatHistory provider based on FileChatHistory
Screenshots
The Neuron AI version you are using:
- Version 1.9.5
Additional context
Example history structure gathered from database
[ { "role": "assistant", "content": "<p>Introduce yourself, check calendar, events .... ;)/p>" }, { "tool_calls": [ { "id": "call_aDLv5vaQ5AeEsKcwUMiBYKr1", "type": "function", "function": { "name": "get_calendar_events", "arguments": "{\"date_from\": \"2025-04-16\", \"date_to\": \"2025-05-16\"}" } }, { "id": "call_1KQ4FiPyylTKymGKdZR1NaHk", "type": "function", "function": { "name": "get_todos", "arguments": "{}" } } ], "role": "assistant", "content": null, "usage": { "input_tokens": 2242, "output_tokens": 61 } }, { "role": "user", "content": null, "tools": [ { "name": "get_calendar_events", "description": "Get calendar events in defined range", "inputs": { "date_from": "2025-04-16", "date_to": "2025-05-16" }, "callId": "call_aDLv5vaQ5AeEsKcwUMiBYKr1", "result": "{}" }, { "name": "get_todos", "description": "Get todos list", "inputs": [], "callId": "call_1KQ4FiPyylTKymGKdZR1NaHk", "result": "{}" } ] }, { "role": "assistant", "content": "<p>👋Welcome.....</p>", "usage": { "input_tokens": -2242, "output_tokens": 536 } }, { "role": "assistant", "content": "<p>👋 Welcome....</p>", "usage": { "input_tokens": -2242, "output_tokens": 536 } }, { "role": "user", "content": "<p>Today is 38,8 degress</p>" }, { "role": "assistant", "content": "<p>Really, 38,8°C?.../p>", "usage": { "input_tokens": 10146, "output_tokens": 149 } } ]