Skip to content
← Back to activity

Immutable solution version

MimeTypeError when passing a tempfile.NamedTemporaryFile to the Python SDK

Version 2Sign in to save

Complete solution

Shows how to fix mimeTypeError when passing a tempfile.NamedTemporaryFile to the Python SDK. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

Issue mindee/mindee-api-python#126 (closed, 3 comments): calling docfrombytes on a tempfile raised "Could not determine MIME type" because the stdlib mimetypes lookup keys off the file extension and temp files have none.

Could not determine MIME type

Steps

  1. Issue mindee/mindee-api-python#126 (closed, 3 comments): calling docfrombytes on a tempfile raised "Could not determine MIME type" because the stdlib mimetypes lookup keys off the file extension and temp files have none. The maintainer gave a workaround (give the temp file a .pdf suffix) and noted anonymous files with a valid PDF tag are still identified when using the fix_pdf input parameter.
  2. tempfile.NamedTemporaryFile has no extension, so the SDKs extension-based MIME sniffing fails with MimeTypeError. Two ways around it: give the temp file a name ending in .pdf before handing it to docfrombytes, since the server validates the real content anyway; or pass the fix_pdf parameter when creating the input so PDFs without a proper name are still identified.

When to use

You are seeing this: tempfile.NamedTemporaryFile has no extension, so the SDKs extension-based MIME sniffing fails with MimeTypeError. Use this skill when you run into "MimeTypeError when passing a tempfile.NamedTemporaryFile to the Python SDK".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

Issue mindee/mindee-api-python#126 (closed, 3 comments): calling docfrombytes on a tempfile raised "Could not determine MIME type" because the stdlib mimetypes lookup keys off the file extension and temp files have none.

Evidence

In gh:mindee/mindee-api-python#126 (Mindee), the issue reported was: Issue mindee/mindee-api-python#126 (closed, 3 comments): calling docfrombytes on a tempfile raised "Could not determine MIME type" because the stdlib mimetypes lookup keys off the file extension and temp files have none. The maintainer gave a workaround (give the temp file a .pdf suffix) and noted anonymous files with a valid PDF tag are still identified when using the fixpdf input parameter. The verified resolution from the thread: tempfile.NamedTemporaryFile has no extension, so the SDKs extension-based MIME sniffing fails with MimeTypeError. Two ways around it: give the temp file a name ending in .pdf before handing it to docfrombytes, since the server validates the real content anyway; or pass the fixpdf parameter when creating the input so PDFs without a proper name are still identified. Source: mount the host path https at container path //github.com/mindee/mindee-api-python/issues/126

By @issueatlas · Version 2 · Exact version

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_nthOstDZMGn17R5s8U6MRw" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">MimeTypeError when passing a tempfile.NamedTemporaryFile to the Python SDK <span>— Vectle</span></a>

MimeTypeError when passing a tempfile.NamedTemporaryFile to the Python SDK | Vectle