Problem/Motivation
When the Mistral provider receives tool calls from the model and passes them into addAssistantMessage(), it currently passes a PHP array. The upstream partitech/php-mistral library expects a ToolCallCollection instance, which causes a fatal TypeError and a 500 response in certain agent/tooling flows (notably with canvas_ai + page builder tool).
public function chat(array|string|ChatInput $input, string $model_id, array $tags = []): ChatOutput {
...
// Handle assistant messages with tool calls from previous turns.
if ($role === 'assistant' && $input_message->getTools()) {
$tool_calls = [];
foreach ($input_message->getRenderedTools() as $tool_call) {
$tool_calls[] = $tool_call;
}
$messages->addAssistantMessage($text, $tool_calls); <--- here
continue;
}
Wrapping the tool calls array in new ToolCallCollection($tool_calls) fixes the issue.
Steps to reproduce
1. Enable canvas_ai.
2. Enable all Canvas AI components so they are available to the agent.
4. In the Canvas editor/agent UI, run a prompt that triggers a tool call, e.g.: "calling the page builder tool first, add a card component with title hello world to the content area"
This results in the following error:
#0 /var/www/html/vendor/partitech/php-mistral/src/Messages.php(132): Partitech\PhpMistral\Message->setToolCalls()
#1 /var/www/html/web/modules/contrib/ai_provider_mistral/src/Plugin/AiProvider/MistralProvider.php(268): Partitech\PhpMistral\Messages->addAssistantMessage()
#2 [internal function]: Drupal\ai_provider_mistral\Plugin\AiProvider\MistralProvider->chat()
#3 /var/www/html/web/modules/contrib/ai/src/Plugin/ProviderProxy.php(269): ReflectionMethod->invokeArgs()
#4 /var/www/html/web/modules/contrib/ai/src/Plugin/ProviderProxy.php(133): Drupal\ai\Plugin\ProviderProxy->wrapperCall()
#5 /var/www/html/web/modules/contrib/ai_agents/src/PluginBase/AiAgentEntityWrapper.php(546): Drupal\ai\Plugin\ProviderProxy->__call()
#6 /var/www/html/web/modules/contrib/ai_agents/src/PluginBase/AiAgentEntityWrapper.php(578): Drupal\ai_agents\PluginBase\AiAgentEntityWrapper->determineSolvability()
#7 /var/www/html/web/modules/contrib/ai_agents/src/Plugin/AiFunctionCall/AiAgentWrapper.php(181): Drupal\ai_agents\PluginBase\AiAgentEntityWrapper->determineSolvability()
#8 /var/www/html/web/modules/contrib/ai_agents/src/PluginBase/AiAgentEntityWrapper.php(1184): Drupal\ai_agents\Plugin\AiFunctionCall\AiAgentWrapper->execute()
#9 /var/www/html/web/modules/contrib/ai_agents/src/PluginBase/AiAgentEntityWrapper.php(482): Drupal\ai_agents\PluginBase\AiAgentEntityWrapper->executeTool()
#10 /var/www/html/web/modules/contrib/ai_agents/src/PluginBase/AiAgentEntityWrapper.php(578): Drupal\ai_agents\PluginBase\AiAgentEntityWrapper->determineSolvability()
#11 /var/www/html/web/modules/contrib/canvas/modules/canvas_ai/src/Controller/CanvasBuilder.php(235): Drupal\ai_agents\PluginBase\AiAgentEntityWrapper->determineSolvability()
#12 [internal function]: Drupal\canvas_ai\Controller\CanvasBuilder->render()
Proposed resolution
Wrap $tool_calls in a ToolCallCollection before passing it to addAssistantMessage().
Issue fork ai_provider_mistral-3567414
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #4
ademarco commentedComment #5
marcus_johansson commentedThank you, I can replicate the error and this MR fixes it. Will merge as soon as Gitlab start working again.
Comment #7
marcus_johansson commentedMerged now, thanks!