Skip to content

Conversation

@davidsonsns
Copy link
Contributor

Background

Mistral's API requires PDF documents to be formatted as either HTTPS URLs or data URIs with the format data:application/pdf;base64,<encoded-data>. When attempting to send PDF files as base64 directly from API endpoints (common use case: file upload → Mistral), the existing implementation handled this correctly but lacked test coverage to validate the format. This left uncertainty about whether PDFs could be sent directly without uploading to cloud storage first.

Summary

  • Added test coverage validating PDF base64 data is correctly formatted with data:application/pdf;base64, prefix
  • Refactored duplicated URL/base64 formatting logic into shared formatFileUrl helper function
  • Improved type safety with explicit LanguageModelV3DataContent typing

Manual Verification

Simulated real-world API flow where PDF is received and sent directly to Mistral:

  1. Received PDF as Uint8Array (mimicking file upload at API endpoint)
  2. Passed to convertToMistralChatMessages with mediaType: 'application/pdf'
  3. Verified output format matches Mistral's requirement: data:application/pdf;base64,AAECAw==
  4. All 45 tests pass (Node + Edge environments)

This eliminates the need to upload PDFs to cloud storage before sending to Mistral.

Checklist

  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • I have reviewed this pull request (self-review)

Future Work

N/A

Related Issues

N/A

- Add test case for PDF documents with base64 data (Uint8Array)
- Extract duplicated URL/base64 handling logic into formatFileUrl helper function
- Improve code maintainability by reducing duplication between image and PDF handling

refactor(mistral): enhance formatFileUrl function for improved readability

- Refactor the formatFileUrl function to use destructured parameters for better clarity.
- Update the function calls within convertToMistralChatMessages to maintain consistent formatting.

This change aims to improve code readability and maintainability.
@davidsonsns davidsonsns changed the title Feat/mistral pdf base64 test coverage feat(mistral): pdf base64 test coverage Nov 11, 2025
@davidsonsns davidsonsns changed the title feat(mistral): pdf base64 test coverage fix(mistral): pdf base64 test coverage Nov 11, 2025
@gr2m gr2m added ai/provider provider/mistral Issues related to the @ai-sdk/mistral provider and removed ai/core labels Nov 14, 2025
@gr2m gr2m changed the title fix(mistral): pdf base64 test coverage fix(mistral): convert messages with PDF file parts from Uint8Array to base64 Nov 14, 2025
Copy link
Collaborator

@gr2m gr2m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! And congratulations to your first contribution to the AI SDK!

@gr2m gr2m enabled auto-merge (squash) November 14, 2025 18:58
@gr2m gr2m merged commit 8f430af into vercel:main Nov 14, 2025
32 of 33 checks passed
@davidsonsns davidsonsns deleted the feat/mistral-pdf-base64-test-coverage branch November 16, 2025 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai/provider provider/mistral Issues related to the @ai-sdk/mistral provider

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants