FoundationAgents/MetaGPT · error · ValueError

file_input must be a string (file path) or bytes.

Error message

file_input must be a string (file path) or bytes.

What it means

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.

Source

Thrown at metagpt/utils/omniparse_client.py:238

        if isinstance(file_input, (str, Path)):
            filename = Path(file_input).name
            file_bytes = await aread_bin(file_input)

            if only_bytes:
                return file_bytes

            mime_type = mimetypes.guess_type(file_input)[0]
            return filename, file_bytes, mime_type
        elif isinstance(file_input, bytes):
            if only_bytes:
                return file_input
            if not bytes_filename:
                raise ValueError("bytes_filename must be set when passing bytes")

            mime_type = mimetypes.guess_type(bytes_filename)[0]
            return bytes_filename, file_input, mime_type
        else:
            raise ValueError("file_input must be a string (file path) or bytes.")

View on GitHub (pinned to 11cdf466d0)

Solutions

  1. Pass a path: client.parsing('docs/a.pdf') or Path('docs/a.pdf').
  2. Or pass bytes: client.parsing(path.read_bytes(), bytes_filename='a.pdf').
  3. For file objects / BytesIO, read first: client.parsing(fo.read(), bytes_filename=fo.name).

Example fix

# before
with open('a.pdf','rb') as f:
    await client.parsing(f)  # ValueError

# after
with open('a.pdf','rb') as f:
    await client.parsing(f.read(), bytes_filename='a.pdf')
Defensive patterns

Strategy: type-guard

Validate before calling

assert isinstance(file_input, (str, Path, bytes)), \
    'file_input must be a path (str/Path) or bytes'

Type guard

from pathlib import Path
def is_omniparse_file_input(x) -> bool:
    return isinstance(x, (str, Path, bytes))

Try / catch

try:
    res = await client.parsing(file_input)
except ValueError as e:
    if 'must be a string' in str(e):
        data = file_input.read() if hasattr(file_input, 'read') else bytes(file_input)
        res = await client.parsing(data, bytes_filename='file.bin')

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of FoundationAgents/MetaGPT@11cdf466d0 (2026-08-14). Data as JSON: /api/errors/6f87e96a0de3d561. Report an issue: GitHub.