Skip to content

fix: Add automatic MIME type detection for file uploads #2520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

mayur1377
Copy link

@mayur1377 mayur1377 commented Aug 7, 2025

  • Add _guess_content_type_from_filename() for filename-based detection
  • Add _sniff_content_type_from_bytes() for content-based detection
  • Add _ensure_tuple_with_content_type() helper function
  • Enhance _transform_file() and _async_transform_file() to include content types
  • Support PDF, PNG, JPEG, GIF detection from file signatures
  • Fixes Uploading PDF via Files API and using in Streaming gives 400 bad request #2472: PDF upload via Files API now includes proper Content-Type

This resolves the 400 Bad Request error when uploading PDF files for streaming use cases.

  • I understand that this repository is auto-generated and my pull request may not be merged

example :
Before :
Screenshot 2025-08-08 at 1 59 30 AM

After :
Screenshot 2025-08-08 at 2 03 40 AM

Additional context & links

- Add _guess_content_type_from_filename() for filename-based detection
- Add _sniff_content_type_from_bytes() for content-based detection
- Add _ensure_tuple_with_content_type() helper function
- Enhance _transform_file() and _async_transform_file() to include content types
- Support PDF, PNG, JPEG, GIF detection from file signatures
- Fixes openai#2472: PDF upload via Files API now includes proper Content-Type

This resolves the 400 Bad Request error when uploading PDF files for streaming use cases.
@mayur1377 mayur1377 requested a review from a team as a code owner August 7, 2025 18:47
Copy link

@rickpr rickpr left a comment

Choose a reason for hiding this comment

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

I tried this and can no longer even upload a PDF

mayur1377 and others added 2 commits August 8, 2025 01:56
- Add _guess_content_type_from_filename() for filename-based detection
- Add _sniff_content_type_from_bytes() for content-based detection
- Add _ensure_tuple_with_content_type() helper function
- Enhance _transform_file() and _async_transform_file() to include content types
- Support PDF, PNG, JPEG, GIF detection from file signatures
- Fixes openai#2472: PDF upload via Files API now includes proper Content-Type

This resolves the 400 Bad Request error when uploading PDF files for streaming use cases.
Copy link

@rickpr rickpr left a comment

Choose a reason for hiding this comment

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

Working for me! I wasn't able to figure out how to pass the original message from the client to the API, but the upload now correctly adds the MIME type. Nicely done!

Maybe some tests would be in order too to ensure this behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Uploading PDF via Files API and using in Streaming gives 400 bad request
4 participants