{"record":{"id":"afae4eb15d0daafb","repo":"FoundationAgents/MetaGPT","slug":"bytes-filename-must-be-set-when-passing-bytes","errorCode":null,"errorMessage":"bytes_filename must be set when passing bytes","messagePattern":"bytes_filename must be set when passing bytes","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"metagpt/utils/omniparse_client.py","lineNumber":233,"sourceCode":"            the MIME type of the file must be specified when uploading.\n\n        Returns: [bytes, tuple]\n            Returns bytes if only_bytes is True, otherwise returns a tuple (filename, file_bytes, mime_type).\n        \"\"\"\n        if isinstance(file_input, (str, Path)):\n            filename = Path(file_input).name\n            file_bytes = await aread_bin(file_input)\n\n            if only_bytes:\n                return file_bytes\n\n            mime_type = mimetypes.guess_type(file_input)[0]\n            return filename, file_bytes, mime_type\n        elif isinstance(file_input, bytes):\n            if only_bytes:\n                return file_input\n            if not bytes_filename:\n                raise ValueError(\"bytes_filename must be set when passing bytes\")\n\n            mime_type = mimetypes.guess_type(bytes_filename)[0]\n            return bytes_filename, file_input, mime_type\n        else:\n            raise ValueError(\"file_input must be a string (file path) or bytes.\")\n","sourceCodeStart":215,"sourceCodeEnd":239,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/utils/omniparse_client.py#L215-L239","documentation":"Raised by OmniParseClient.get_file_info(): when file_input is raw bytes and only_bytes is False, the method needs a filename to compute a name and MIME type; without bytes_filename it cannot proceed and raises ValueError instead of guessing.","triggerScenarios":"Calling get_file_info(file_bytes, only_bytes=False) or the higher-level parse methods with bytes content but omitting bytes_filename. With only_bytes=True the bytes are returned directly and the guard is skipped.","commonSituations":"Fetching a document over HTTP/a memory buffer and passing response.content directly to the client without naming it; refactoring call sites that previously used file paths.","solutions":["Pass a descriptive filename: client.parsing(data, bytes_filename='doc.pdf').","If you only need the byte payload returned, call get_file_info(..., only_bytes=True).","Derive bytes_filename from the download URL or Content-Disposition before calling."],"exampleFix":"# before\nawait client.parsing(resp.content)  # ValueError: bytes_filename must be set\n\n# after\nawait client.parsing(resp.content, bytes_filename='downloaded.pdf')","handlingStrategy":"type-guard","validationCode":"if isinstance(file_input, (bytes, bytearray)) and not only_bytes and not bytes_filename:\n    raise ValueError('provide bytes_filename for byte input')","typeGuard":"def is_valid_omniparse_input(file_input, bytes_filename: str | None, only_bytes: bool) -> bool:\n    if isinstance(file_input, (str, Path)):\n        return True\n    if isinstance(file_input, bytes):\n        return only_bytes or bool(bytes_filename)\n    return False","tryCatchPattern":"try:\n    res = await client.parsing(data)\nexcept ValueError as e:\n    if 'bytes_filename' in str(e):\n        res = await client.parsing(data, bytes_filename='upload.pdf')","preventionTips":["Always name byte payloads with a real extension (e.g. from Content-Disposition).","Use only_bytes=True when you don't need the filename/MIME tuple.","Prefer passing file paths when available."],"tags":["omniparse","file-upload","validation"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}