Skip to content

[BUG] ActiveStorage::Blob gets re-uploaded when passed into a message with Rails integration #665

Description

@bubiche

Basic checks

  • I searched existing issues - this hasn't been reported
  • I can reproduce this consistently
  • This is a RubyLLM bug, not my application code

What's broken?

I'm using Rails with ActiveStorage for file attachments with Google Cloud Storage. I want the file to go under specific folder in my bucket so I upload them first with

ActiveStorage::Blob.create_and_upload!(
  ...,
  key: "my/file/path.ext"
)

If I pass the blob object using chat.ask, chat.create_user_message, ..., it'll be re-uploaded instead of reusing my original uploaded file.

How to reproduce

  1. Setup RubyLLM with Rails integration + ActiveStorage for file attachments
  2. Upload a file using ActiveStorage::Blob
blob = ActiveStorage::Blob.create_and_upload!(
  ...,
  key: "my/file/path.ext"
)
  1. Use it with a chat record backed by ActiveRecord
# I might have multiple files here so I'm using a list
chat.ask("Describe the content of the attached file", with: [blob])

Expected behavior

RubyLLM reuse the uploaded file

What actually happened

A new upload is created in my bucket and my old one is "orphaned".

This looks like it's due to build_content wrapping things under RubyLLM::Content.new so ActiveStorage::Blob is wrapped under RubyLLM::Attachment. prepare_for_active_storage doesn't know it's an ActiveStorage::Blob anymore then in the end this line would just create a new blob and re-upload.

The fix for just my use case is probably to update prepare_for_active_storage to handle valid ActiveStorage attachable hash differently then I can just pass in a hash using the with parameter, e.g.

when Hash
  if attachment.key?(:io) && attachment.key?(:filename)
    attachment
  else
    attachment.values.map { |v| prepare_for_active_storage(v) }
  end

Otherwise I think we should make RubyLLM::Content preserve ActiveStorage-native objects.

Please let me know if you are interested in any direction. I'd love to try and create a PR for it.

Environment

  • Ruby version: 3.4.8
  • RubyLLM version: 1.13.2
  • Provider: OpenAI
  • OS: Mac OSX

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions