assafelovic/gpt-researcher · error · ValueError

Invalid type for path. Expected str, bytes, os.PathLike, or

Error message

Invalid type for path. Expected str, bytes, os.PathLike, or list thereof.

What it means

DocumentLoader.load accepts str/bytes/os.PathLike (a single file/dir path) or a list of such paths; any other type (int, dict, None) reaches the final else and raises this ValueError.

Source

Thrown at gpt_researcher/document/document.py:41

        tasks = []
        if isinstance(self.path, list):
            for file_path in self.path:
                if os.path.isfile(file_path):  # Ensure it's a valid file
                    filename = os.path.basename(file_path)
                    file_name, file_extension_with_dot = os.path.splitext(filename)
                    file_extension = file_extension_with_dot.strip(".").lower()
                    tasks.append(self._load_document(file_path, file_extension))
                    
        elif isinstance(self.path, (str, bytes, os.PathLike)):
            for root, dirs, files in os.walk(self.path):
                for file in files:
                    file_path = os.path.join(root, file)
                    file_name, file_extension_with_dot = os.path.splitext(file)
                    file_extension = file_extension_with_dot.strip(".").lower()
                    tasks.append(self._load_document(file_path, file_extension))
                    
        else:
            raise ValueError("Invalid type for path. Expected str, bytes, os.PathLike, or list thereof.")

        # for root, dirs, files in os.walk(self.path):
        #     for file in files:
        #         file_path = os.path.join(root, file)
        #         file_name, file_extension_with_dot = os.path.splitext(file_path)
        #         file_extension = file_extension_with_dot.strip(".")
        #         tasks.append(self._load_document(file_path, file_extension))

        docs = []
        for pages in await asyncio.gather(*tasks):
            for page in pages:
                if page.page_content:
                    docs.append({
                        "raw_content": page.page_content,
                        "url": os.path.basename(page.metadata['source'])
                    })
                    
        if not docs:

View on GitHub (pinned to 6f998577d5)

Solutions

  1. Pass a str/Path or a list of them to DocumentLoader
  2. Default/validate the path before constructing: Path(doc_path or './docs')
  3. If multiple paths, use a list, not a tuple

Example fix

# before
loader = DocumentLoader(None)
docs = loader.load()
# after
from pathlib import Path
loader = DocumentLoader(str(Path(doc_path).expanduser()))
docs = loader.load()
Defensive patterns

Strategy: type-guard

Validate before calling

import os
from pathlib import Path
p = doc_path
assert p is None or isinstance(p, (str, bytes, os.PathLike, list))
p = str(Path(p or "./docs").resolve())

Type guard

def is_loadable_path(p) -> bool:
    if isinstance(p, (str, bytes, os.PathLike)):
        return True
    return isinstance(p, list) and all(isinstance(i, (str, bytes, os.PathLike)) for i in p)

Try / catch

try:
    docs = loader.load()
except ValueError as e:
    if "Invalid type for path" in str(e):
        docs = DocumentLoader(str(default_path)).load()
    else: raise

Prevention

When it happens

Trigger: Calling DocumentLoader(None, ...).load() (path never set), passing a dict from another loader's output, or an int/None from upstream config like a missing DOC_PATH.

Common situations: Forgetting to pass the path kwarg; DOC_PATH env var unset so path is None; passing a tuple instead of list.

Understand the failure class

Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.

Related errors


AI-assisted analysis of assafelovic/gpt-researcher@6f998577d5 (2026-08-28). Data as JSON: /api/errors/82dd967c25f8653a. Report an issue: GitHub.