gptel
gptel copied to clipboard
gptel-openai: Validate MIME types for media files
Summary
Fixes #929
OpenAI's Vision API only supports image formats (JPEG, PNG, GIF, WebP) and does not support PDFs or other document types. This PR adds MIME type validation to prevent sending unsupported formats through the OpenAI backend.
Changes
- gptel-openai.el: Added MIME type validation in
gptel--openai-parse-multipartfunction- Validates that media files are supported image formats
- Provides clear error message when unsupported formats (like PDFs) are attempted
- Guides users to use native Anthropic or Gemini backends for PDF support
Issue Details
When users configured models with PDF support and sent PDFs through OpenRouter's OpenAI-compatible API, the backend would send PDFs as image_url types, causing Azure/OpenAI to reject the request with:
400 Bad Request - Invalid image URL
Solution
The fix validates MIME types before constructing the API request and throws a descriptive error:
(gptel-openai) Request aborted: OpenAI API only supports image formats (JPEG, PNG, GIF, WebP).
Unsupported MIME type: application/pdf.
For PDF support, use native Anthropic or Gemini backends
This gives users clear guidance on how to resolve the issue.
Testing
The implementation has been tested to ensure:
- Supported image formats (JPEG, PNG, GIF, WebP) work correctly
- Unsupported formats (PDF, text, video) are properly rejected
- Error messages are clear and actionable
🤖 Generated with Claude Code
@karthink I don't use OpenAI through OpenRouter, so I'm not confident of these changes yet.