fix: fall back to plain generate() when web_search tool returns HTTP error

mistral-large-latest may not support the web_search tool type on all API
tiers; catch the exception and retry without web search so the pipeline
does not crash with needs_review.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Simon Kuehn 2026-05-18 08:18:54 +00:00
parent 4739a0b1fe
commit 00dc232426

View file

@ -74,6 +74,7 @@ final class MistralClient implements OllamaClientInterface
{
$messages = [['role' => 'user', 'content' => $prompt]];
try {
$response = $this->httpClient->request('POST', $this->mistralBaseUrl.'/v1/chat/completions', [
'headers' => ['Authorization' => 'Bearer '.$this->mistralApiKey],
'json' => [
@ -86,14 +87,17 @@ final class MistralClient implements OllamaClientInterface
/** @var array{choices: array{0: array{finish_reason: string, message: array{content: ?string, tool_calls?: list<array{id: string, function: array{name: string, arguments: string}}>}}}} $data */
$data = $response->toArray();
} catch (\Throwable) {
// Model does not support web_search tool — fall back to plain generation
return $this->generate($model, $prompt);
}
$choice = $data['choices'][0];
if ('tool_calls' !== $choice['finish_reason']) {
return (string) $choice['message']['content'];
}
// Append assistant's tool call turn, then return empty tool results so
// Mistral's servers can complete the search and produce the final answer.
$messages[] = $choice['message'];
foreach ($choice['message']['tool_calls'] ?? [] as $toolCall) {
$messages[] = [
@ -103,6 +107,7 @@ final class MistralClient implements OllamaClientInterface
];
}
try {
$final = $this->httpClient->request('POST', $this->mistralBaseUrl.'/v1/chat/completions', [
'headers' => ['Authorization' => 'Bearer '.$this->mistralApiKey],
'json' => [
@ -117,6 +122,9 @@ final class MistralClient implements OllamaClientInterface
$finalData = $final->toArray();
return $finalData['choices'][0]['message']['content'];
} catch (\Throwable) {
return $this->generate($model, $prompt);
}
}
private function guessMimeType(string $path): string