microsoft/semantic-kernel · error · FileNotFoundError

File not found: {file_path}

Error message

File not found: {file_path}

What it means

A FileNotFoundError raised when a user issues an [upload <purpose> <path>] command in the assistants group chat demo but the supplied path does not resolve to an existing file on disk. The regex parse succeeds and produces a path, but os.path.exists() returns False, so the upload is aborted before calling the assistant's add_file.

Source

Thrown at python/samples/demos/assistants_group_chat/group_chat.py:61

    This will upload file.txt to the assistant for use with the code interpreter tool.

    Type "exit" to exit the chat.
    """
    )


def parse_upload_command(user_input: str):
    """Parse the user input for an upload command."""
    match = re.search(r"\[upload\s+(code_interpreter|file_search)\s+(.+)\]", user_input)
    if match:
        return match.group(1), match.group(2)
    return None, None


async def handle_file_upload(assistant_agent: OpenAIAssistantAgent, purpose: str, file_path: str):
    """Handle the file upload command."""
    if not os.path.exists(file_path):
        raise FileNotFoundError(f"File not found: {file_path}")

    file_id = await assistant_agent.add_file(file_path, purpose="assistants")
    print(f"File uploaded: {file_id}")

    if purpose == "code_interpreter":
        await enable_code_interpreter(assistant_agent, file_id)
    elif purpose == "file_search":
        await enable_file_search(assistant_agent, file_id)


async def enable_code_interpreter(assistant_agent: OpenAIAssistantAgent, file_id: str):
    """Enable the file for code interpreter."""
    assistant_agent.code_interpreter_file_ids.append(file_id)
    tools = [{"type": "file_search"}, {"type": "code_interpreter"}]
    tool_resources = {"code_interpreter": {"file_ids": assistant_agent.code_interpreter_file_ids}}
    await assistant_agent.modify_assistant(
        assistant_id=assistant_agent.assistant.id, tools=tools, tool_resources=tool_resources
    )

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Provide an absolute path in the upload command, or cd to the directory containing the file before launching the demo.
  2. Verify the file exists with the same user/permissions that run the demo.
  3. If the path has spaces, ensure the regex captures the full remainder (group 2 is .+) and quote-check the typed command.

Example fix

// before
[upload code_interpreter resources/my file.csv]

// after
[upload code_interpreter /abs/path/to/resources/my file.csv]
Defensive patterns

Strategy: validation

Validate before calling

import os

abs_path = os.path.abspath(file_path)
if not os.path.isfile(abs_path):
    print(f"No such file: {abs_path}")  # surface to user before upload
    return

Type guard

import os

def is_valid_upload_path(p: str) -> bool:
    return bool(p) and os.path.isfile(os.path.abspath(p))

Try / catch

try:
    await handle_file_upload(agent, purpose, file_path)
except FileNotFoundError:
    print(f"Could not find '{file_path}'. Check the path and retry.")

Prevention

When it happens

Trigger: Typing an upload command like [upload code_interpreter ./data.csv] where ./data.csv does not exist relative to the process working directory, or the file was never created/moved/deleted.

Common situations: Relative paths resolved against an unexpected cwd; file lives under a different folder than where the demo is launched; the path contains spaces or was mistyped; the file was removed between sessions.

Related errors


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