Skip to content

Comments

Fix: Extract filename from filepath in UploadFile methods#951

Open
Copilot wants to merge 5 commits intomainfrom
copilot/fix-upload-file-filename-issue
Open

Fix: Extract filename from filepath in UploadFile methods#951
Copilot wants to merge 5 commits intomainfrom
copilot/fix-upload-file-filename-issue

Conversation

Copy link
Contributor

Copilot AI commented Feb 13, 2026

  • Fix UploadFileAsync(string filePath, ...) to extract filename using Path.GetFileName()
  • Fix UploadFile(string filePath, ...) to extract filename using Path.GetFileName()
  • Update test assertion in FilesTests.cs to expect filename only
  • Simplify test assertion by removing unnecessary conditional (both branches produce same value)
  • Run tests to verify the changes (mock tests passed - 142/142)
  • Request code review (no issues found)
  • Run CodeQL security check (no vulnerabilities detected)

Summary

Successfully fixed the issue where UploadFile methods were sending the full file path as the filename instead of just the filename. Both sync and async methods now properly extract only the filename using Path.GetFileName() before sending to the API. Simplified test assertion based on code review feedback.

Original prompt

Problem

When using OpenAIFileClient.UploadFile(string filePath, FileUploadPurpose purpose) or UploadFileAsync(string filePath, FileUploadPurpose purpose), the entire file path (e.g., C:\Users\me\Documents\data.jsonl) is sent as the filename to the OpenAI API, instead of just the filename (e.g., data.jsonl).

Fixes #308

Root Cause

In src/Custom/Files/OpenAIFileClient.cs, both the sync and async filePath-based overloads pass the raw filePath directly as the filename parameter to the Stream-based overload:

public virtual async Task<ClientResult<OpenAIFile>> UploadFileAsync(string filePath, FileUploadPurpose purpose)
{
    Argument.AssertNotNullOrEmpty(filePath, nameof(filePath));
    using FileStream stream = File.OpenRead(filePath);
    return await UploadFileAsync(stream, filePath, purpose).ConfigureAwait(false);
    //                                  ^^^^^^^^ BUG: passes full path as filename
}

public virtual ClientResult<OpenAIFile> UploadFile(string filePath, FileUploadPurpose purpose)
{
    Argument.AssertNotNullOrEmpty(filePath, nameof(filePath));
    using FileStream stream = File.OpenRead(filePath);
    return UploadFile(stream, filePath, purpose);
    //                       ^^^^^^^^ BUG: passes full path as filename
}

Required Changes

1. Fix src/Custom/Files/OpenAIFileClient.cs

In both the UploadFile(string filePath, ...) and UploadFileAsync(string filePath, ...) methods, use Path.GetFileName(filePath) to extract just the filename before passing it to the stream-based overload.

Async method (around line 207-212):

public virtual async Task<ClientResult<OpenAIFile>> UploadFileAsync(string filePath, FileUploadPurpose purpose)
{
    Argument.AssertNotNullOrEmpty(filePath, nameof(filePath));

    using FileStream stream = File.OpenRead(filePath);
    string filename = Path.GetFileName(filePath);
    return await UploadFileAsync(stream, filename, purpose).ConfigureAwait(false);
}

Sync method (around line 225-230):

public virtual ClientResult<OpenAIFile> UploadFile(string filePath, FileUploadPurpose purpose)
{
    Argument.AssertNotNullOrEmpty(filePath, nameof(filePath));

    using FileStream stream = File.OpenRead(filePath);
    string filename = Path.GetFileName(filePath);
    return UploadFile(stream, filename, purpose);
}

2. Fix test assertions in tests/Files/FilesTests.cs

Around line 180, update the expected filename assertion for the FileSourceKind.UsingFilePath case to expect just the filename (not the full path):

Change:

string expectedFilename = (fileSourceKind == FileSourceKind.UsingFilePath) ? path : filename;

To:

string expectedFilename = (fileSourceKind == FileSourceKind.UsingFilePath) ? Path.GetFileName(path) : filename;

This ensures the test correctly validates the new behavior where only the filename portion is used.

NOTE: Do NOT modify or update any test recording files. Only update the source code and test assertion files.

This pull request was created from Copilot chat.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: ShivangiReja <45216704+ShivangiReja@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix incorrect filename being sent to OpenAI API Fix: Extract filename from filepath in UploadFile methods Feb 13, 2026
Copilot AI requested a review from ShivangiReja February 13, 2026 21:47
Co-authored-by: ShivangiReja <45216704+ShivangiReja@users.noreply.github.com>
Copilot AI requested a review from ShivangiReja February 13, 2026 21:56
@ShivangiReja ShivangiReja marked this pull request as ready for review February 13, 2026 22:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UploadFile/Async with filePath sets whole path as filename

4 participants