{"record":{"id":"efddb9132460bec7","repo":"xtekky/gpt4free","slug":"failed-to-upload-file-response-status-error-te","errorCode":null,"errorMessage":"Failed to upload file: {response.status} {error_text}","messagePattern":"Failed to upload file: (.+?) (.+?)","errorType":"http","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"g4f/Provider/DeepAI.py","lineNumber":69,"sourceCode":"        content_type, _ = mimetypes.guess_type(filename)\n        content_type = content_type or \"image/png\"\n\n        data = FormData()\n        data.add_field(\"file\", file_data, filename=filename, content_type=content_type)\n        upload_headers = {\n            k: v\n            for k, v in headers.items()\n            if k.lower() not in [\"content-type\", \"api-key\"]\n        }\n        async with session.post(\n            \"https://api.deepai.org/chat_attachments/upload\",\n            headers=upload_headers,\n            data=data,\n            proxy=proxy,\n        ) as response:\n            if not response.ok:\n                error_text = await response.text()\n                raise RuntimeError(\n                    f\"Failed to upload file: {response.status} {error_text}\"\n                )\n            res_json = await response.json()\n            if res_json.get(\"success\"):\n                return res_json[\"attachment\"][\"uuid\"]\n            raise RuntimeError(f\"Failed to upload file: {res_json}\")\n\n    @classmethod\n    def generate_api_key(cls, user_agent: str) -> str:\n        myrandomstr = str(round(random.random() * 100000000000))\n\n        def myhashfunction(input_str: str) -> str:\n            return hashlib.md5(input_str.encode(\"utf-8\")).hexdigest()[::-1]\n\n        hash1 = myhashfunction(\n            user_agent\n            + myrandomstr\n            + \"hackers_become_a_little_stinkier_every_time_they_hack\"","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/Provider/DeepAI.py#L51-L87","documentation":"DeepAI's file-attachment helper POSTs to https://api.deepai.org/chat_attachments/upload and raises on any non-OK status, including the body text. Common failures: 401/403 (invalid or missing api-key header), 413 (file too large), 422/400 (unsupported file type), and 429 (rate limit). Note the upload headers deliberately strip content-type and api-key from the generic header set, so auth may ride on cookies/other headers.","triggerScenarios":"Uploading an oversized or unsupported file type; expired/invalid DeepAI API key or session; uploading with media bytes that are empty or corrupted; rate-limited after repeated uploads.","commonSituations":"Passing images larger than DeepAI's limit; api-key header excluded and session not established beforehand; sending a filename without a recognized extension; MIME sniffing failing on binary data.","solutions":["Read the status in the message: 401/403 → regenerate the API key (cls.generate_api_key) or refresh session; 413 → compress/resize the file; 422 → convert to a supported format; 429 → back off","Verify the media tuple format (mime type, filename, bytes) matches what the provider expects","Check file size against DeepAI's documented attachment limit before uploading"],"exampleFix":"# before\nasync for chunk in DeepAI.create_async_generator(model, messages, media=[(\"image/png\", \"pic.png\", huge_bytes)]):\n    ...\n\n# after — validate before upload\nif len(huge_bytes) > 10 * 1024 * 1024:\n    raise ValueError('Attachment exceeds 10MB limit')\nasync for chunk in DeepAI.create_async_generator(model, messages, media=[(\"image/png\", \"pic.png\", huge_bytes)]):\n    ...","handlingStrategy":"validation","validationCode":"MAX_UPLOAD_BYTES = 10 * 1024 * 1024\nSUPPORTED_MIMES = {\"image/png\", \"image/jpeg\", \"image/gif\", \"image/webp\"}\n\ndef media_is_uploadable(media):\n    mime, name, data = media\n    return (\n        mime in SUPPORTED_MIMES\n        and len(data) <= MAX_UPLOAD_BYTES\n        and len(data) > 0\n    )\n\nassert all(media_is_uploadable(m) for m in media or [])","typeGuard":"def is_valid_media_list(media) -> bool:\n    if not media:\n        return True\n    return all(\n        isinstance(m, (tuple, list)) and len(m) == 3\n        and isinstance(m[0], str) and '/' in m[0]\n        and isinstance(m[1], str)\n        and isinstance(m[2], (bytes, bytearray)) and len(m[2]) > 0\n        for m in media\n    )","tryCatchPattern":"try:\n    async for chunk in DeepAI.create_async_generator(model, messages, media=media):\n        yield chunk\nexcept RuntimeError as e:\n    text = str(e)\n    if ' 413 ' in text:\n        raise ValueError('Attachment too large for DeepAI')\n    elif ' 401 ' in text or ' 403 ' in text:\n        raise AuthNeeded('DeepAI API key/session invalid')\n    else:\n        raise","preventionTips":["Compress/resize images before upload to stay under the size limit","Validate media tuples (mime, filename, bytes) before calling the provider","Regenerate the DeepAI API key via the provider's flow when 401/403 appears"],"tags":["upload","http","file-attachment","api","deepai"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}