microsoft/semantic-kernel · error · FileNotFoundError

File {file_name} not found in repository.

Error message

File {file_name} not found in repository.

What it means

Raised by RepoFilePlugin.read_file_by_name after os.walk over the repo root completes without finding a file matching file_name. Unlike read_file_by_path, this catches the not-found case at the end of the walk rather than via an exception.

Source

Thrown at python/samples/demos/document_generator/plugins/repo_file_plugin.py:39

        try:
            with open(path) as file:
                return file.read()
        except FileNotFoundError:
            raise FileNotFoundError(f"File {path} not found in repository.")

    @kernel_function(
        description="Read a file given the name of the file. Function will search for the file in the repository."
    )
    def read_file_by_name(
        self, file_name: Annotated[str, "The name of the file."]
    ) -> Annotated[str, "Returns the file content."]:
        path = os.path.join(os.path.dirname(__file__), "..", "..", "..", "..")
        for root, dirs, files in os.walk(path):
            if file_name in files:
                print(f"Found file {file_name} in {root}.")
                with open(os.path.join(root, file_name)) as file:
                    return file.read()
        raise FileNotFoundError(f"File {file_name} not found in repository.")

    @kernel_function(description="List all files or subdirectories in a directory.")
    def list_directory(
        self, path: Annotated[str, "Path of a directory relative to the root of the repository."]
    ) -> Annotated[str, "Returns a list of files and subdirectories as a string."]:
        path = os.path.join(os.path.dirname(__file__), "..", "..", "..", "..", path)
        try:
            files = os.listdir(path)
            # Join the list of files into a single string
            return "\n".join(files)
        except FileNotFoundError:
            raise FileNotFoundError(f"Directory {path} not found in repository.")

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Confirm the exact filename (including extension and case) exists in the repository.
  2. Pre-search the tree yourself (glob) to confirm uniqueness/existence before calling.
  3. If multiple files share the name, use read_file_by_path with the specific relative path instead.
  4. Ensure the resolved base (four levels up) is the actual repository root.

Example fix

// before
plugin.read_file_by_name('config')  # missing extension

// after
plugin.read_file_by_name('config.py')
Defensive patterns

Strategy: validation

Validate before calling

import os
base = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', '..'))
matches = []
for root, _, files in os.walk(base):
    if file_name in files:
        matches.append(os.path.join(root, file_name))
if not matches:
    raise FileNotFoundError(f'{file_name} not found under {base}')
if len(matches) > 1:
    raise ValueError(f'{file_name} is ambiguous: {matches}')

Type guard

import os
def file_name_exists(file_name: str) -> bool:
    base = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', '..'))
    for _, _, files in os.walk(base):
        if file_name in files:
            return True
    return False

Prevention

When it happens

Trigger: Calling read_file_by_name with a filename that does not exist anywhere under the resolved repo root; filename typo; the file is in a directory excluded from the walk or in .gitignore'd paths that still exist (gitignored files are still walked by os.walk).

Common situations: LLM supplies a plausible but wrong filename; duplicate filenames across the repo where the first match is returned but the caller expected another; very large repos where os.walk is slow.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/aab0b69ff2b82f0b. Report an issue: GitHub.