Skip to content

[FEATURE] Support for handling ActionDispatch::Http::UploadedFile in RubyLLM (filename is always nil) #468

@pekopekopekopayo

Description

@pekopekopekopayo

Scope check

  • This is core LLM communication (not application logic)
  • This benefits most users (not just my use case)
  • This can't be solved in application code with current RubyLLM
  • I read the Contributing Guide

Due diligence

  • I searched existing issues
  • I checked the documentation

What problem does this solve?

Hello,
I’m a Rails developer using RubyLLM.

When a client uploads a file, I want to receive it in my Rails controller (params), combine it with a prompt, and send both to RubyLLM. However, when using ActionDispatch::Http::UploadedFile, the filename attribute always becomes nil, which causes an error.

I understand that RubyLLM is not Rails-dependent, but it would be great if this use case could be supported, since file uploads are very common in Rails apps.

I read through the contributing guide, but I’m not sure if this is something appropriate to open an issue for.
If I misunderstood something, I apologize — I just wanted to share this in case it helps improve the project.
Also, English is not my first language, so please excuse any awkward phrasing.

Thank you for your time and for this awesome project!

Proposed solution

used code

RubyLLM.chat.ask("prompt", with: params[:pdf]) # <- error
RubyLLM.chat.ask("prompt", with: params[:pdf].path) # <- work but filename is not original

before

def initialize(source, filename: nil)
      @source = source
      if url?
        @source = URI source
        @filename = filename || File.basename(@source.path).to_s
      elsif path?
        @source = Pathname.new source
        @filename = filename || @source.basename.to_s
      elsif active_storage?
        @filename = filename || extract_filename_from_active_storage
      else
        @filename = filename
      end

      determine_mime_type
end

after

def initialize(source, filename: nil)
      @source = source
      if url?
        @source = URI source
        @filename = filename || File.basename(@source.path).to_s
      elsif path?
        @source = Pathname.new source
        @filename = filename || @source.basename.to_s
      elsif io_like?
        @filename = if filename.present?
                      filename
                    elsif defined?(ActionDispatch::Http::UploadedFile) && @source.is_a?(ActionDispatch::Http::UploadedFile)
                      @source.original_filename.to_s
                    else
                      File.basename(@source.path).to_s
                    end
      elsif active_storage?
        @filename = filename || extract_filename_from_active_storage
      else
        @filename = filename
      end

      determine_mime_type
    end

Why this belongs in RubyLLM

Since it provides convenience to people who use files, I believe it belongs to RubyLLM.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions