{"record":{"id":"6f87e96a0de3d561","repo":"FoundationAgents/MetaGPT","slug":"file-input-must-be-a-string-file-path-or-bytes","errorCode":null,"errorMessage":"file_input must be a string (file path) or bytes.","messagePattern":"file_input must be a string \\(file path\\) or bytes\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"metagpt/utils/omniparse_client.py","lineNumber":238,"sourceCode":"        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":220,"sourceCodeEnd":239,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/utils/omniparse_client.py#L220-L239","documentation":"Raised by OmniParseClient.get_file_info(): file_input must be a path-like (str/Path) or bytes; any other type falls into the final else-branch and is rejected with ValueError. This is a strict type guard at the API boundary before any I/O happens.","triggerScenarios":"Passing an open file object, io.BytesIO, bytearray, or None as file_input. E.g. client.parsing(open('a.pdf','rb')) fails because a file object is neither str/Path nor bytes.","commonSituations":"Wrapping the client in code that previously used file handles; passing memoryview/bytearray from network layers; None slipping through when a download step failed silently.","solutions":["Pass a path: client.parsing('docs/a.pdf') or Path('docs/a.pdf').","Or pass bytes: client.parsing(path.read_bytes(), bytes_filename='a.pdf').","For file objects / BytesIO, read first: client.parsing(fo.read(), bytes_filename=fo.name)."],"exampleFix":"# before\nwith open('a.pdf','rb') as f:\n    await client.parsing(f)  # ValueError\n\n# after\nwith open('a.pdf','rb') as f:\n    await client.parsing(f.read(), bytes_filename='a.pdf')","handlingStrategy":"type-guard","validationCode":"assert isinstance(file_input, (str, Path, bytes)), \\\n    'file_input must be a path (str/Path) or bytes'","typeGuard":"from pathlib import Path\ndef is_omniparse_file_input(x) -> bool:\n    return isinstance(x, (str, Path, bytes))","tryCatchPattern":"try:\n    res = await client.parsing(file_input)\nexcept ValueError as e:\n    if 'must be a string' in str(e):\n        data = file_input.read() if hasattr(file_input, 'read') else bytes(file_input)\n        res = await client.parsing(data, bytes_filename='file.bin')","preventionTips":["Materialize file objects/BytesIO to bytes before calling.","Convert bytearray/memoryview to bytes.","Check for None (failed upstream download) before calling."],"tags":["omniparse","type-validation","file-upload"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}