Skip to content

[BUG] When sending a message with attachments, chat inconsistently analyzes the correct attachments #576

@nativestranger

Description

@nativestranger

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?

To Reproduce

  1. Create a Chat
  2. Start a conversation — begin adding attachments (add multiple messages with varying attachments)
  3. Try to specify a new attachment
  4. See inconsistent behavior

How We Resolved This For Now

#516

   # within chat_stream_job.rb#perform

    # Path A: User message has attachments → use chat.prompt() with blobs
    # Path B: Text-only → use chat.complete() as before
    if @user_msg.attachments.attached?
      stream_with_attachments
    else
      stream_text_only
    end

    # also within chat_stream_job.rb — uses #prompt to ensure only latest attachments are passed to LLM for consistency

    def stream_with_attachments
      blobs = @user_msg.attachments.blobs.to_a
    
      response = @chat.prompt(@user_msg.content, with: blobs) do |chunk|
        break if stopped?
    
        chunk_text = extract_chunk_text(chunk)
        next if chunk_text.nil? || chunk_text.empty?

        @accumulated += chunk_text
        
        # Create assistant message on first chunk
        @llm_msg ||= @chat.messages.create!(role: :assistant, content: @accumulated)
        @llm_msg.update_columns(content: @accumulated)
        @llm_msg.broadcast_full_replace!
     end
   end

How to reproduce

  1. Create a chat with attachments
  2. Chat repetitively, uploading new attachments per message and asking about details on each one
  3. Observe the issue

Expected behavior

Expected chat to reliably respond to the latest messages with their distinct attachments as the attachment context.

What actually happened

Before

Chat gets confused, referencing the wrong attachment (I saw this in old tests) or referencing a RubyLLM::Content object (I saw this in recent tests)

Environment

  • Ruby Version: 4.0.0
  • RubyLLM version 1.11.0
  • Provider Anthropic
  • OS: MacOS

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