From 3b7e4841fbf41e9dcea8102d4d3cea46cb136c0e Mon Sep 17 00:00:00 2001 From: Lukas Schaefer Date: Mon, 14 Sep 2026 15:13:47 -0400 Subject: [PATCH] fix: use Mistral document_url shape for PDF attachments Signed-off-by: Lukas Schaefer --- lib/Service/OpenAiFileService.php | 15 +++++++++++++-- lib/Service/ServiceConfig.php | 4 ++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/Service/OpenAiFileService.php b/lib/Service/OpenAiFileService.php index 936108a5..cc9f8e4c 100644 --- a/lib/Service/OpenAiFileService.php +++ b/lib/Service/OpenAiFileService.php @@ -235,7 +235,7 @@ private function buildVideoContent(File $file, string $fileType, ServiceConfig $ } /** - * @return list + * @return list> */ private function buildDocumentContent(File $file, string $fileType, ServiceConfig $service): array { if (!$service->getMultimodalDocumentEnabled()) { @@ -248,11 +248,22 @@ private function buildDocumentContent(File $file, string $fileType, ServiceConfi 'text' => 'Filename:' . $file->getName() . "\nContent:\n" . $text, ]]; } + $dataUri = 'data:' . $fileType . ';base64,' . base64_encode(stream_get_contents($file->fopen('rb'))); + + if ($service->isUsingMistral()) { + // Mistral does not accept the default openai shape so we use a fallback for them + return [[ + 'type' => 'document_url', + 'document_url' => $dataUri, + 'document_name' => $file->getName(), + ]]; + } + return [[ 'type' => 'file', 'file' => [ 'filename' => $file->getName(), - 'file_data' => 'data:' . $fileType . ';base64,' . base64_encode(stream_get_contents($file->fopen('rb'))), + 'file_data' => $dataUri, ], ]]; } diff --git a/lib/Service/ServiceConfig.php b/lib/Service/ServiceConfig.php index 7cff54e6..62906b93 100644 --- a/lib/Service/ServiceConfig.php +++ b/lib/Service/ServiceConfig.php @@ -273,6 +273,10 @@ public function isUsingOpenRouter(): bool { return str_starts_with(strtolower($this->url), 'https://openrouter.ai'); } + public function isUsingMistral(): bool { + return str_starts_with(strtolower($this->url), 'https://api.mistral.ai'); + } + public function getApiKey(): string { return $this->apiKey; }