Skip to content

Commit 1212984

Browse files
DustinFishercrmne
andauthored
Fix compatibility issue with URL attachments wrong number of arguments (#250)
- Fix ArgumentError: wrong number of arguments (given 2, expected 0..1) that occurs when Marcel processes URI objects - Use filename-based MIME detection for URLs instead of passing URI objects to Marcel - Add test that reproduces the issue and validates the fix - Maintains backward compatibility and improves performance by avoiding network calls <img width="1316" alt="Screenshot 2025-06-16 at 9 34 13 AM" src="https://github.com/user-attachments/assets/b0d29eeb-66aa-45b5-aed9-5c33adf0c0b7" /> ## What this does <!-- Clear description of what this PR does and why --> ## Type of change - [x] Bug fix - [ ] New feature - [ ] Breaking change - [ ] Documentation - [ ] Performance improvement ## Scope check - [x] I read the [Contributing Guide](https://github.com/crmne/ruby_llm/blob/main/CONTRIBUTING.md) - [x] This aligns with RubyLLM's focus on **LLM communication** - [x] This isn't application-specific logic that belongs in user code - [x] This benefits most users, not just my specific use case ## Quality check - [x] I ran `overcommit --install` and all hooks pass - [x] I tested my changes thoroughly - [ ] I updated documentation if needed - [x] I didn't modify auto-generated files manually (`models.json`, `aliases.json`) ## API changes - [ ] Breaking change - [ ] New public methods/classes - [ ] Changed method signatures - [x] No API changes ## Related issues Fixes #247 --------- Co-authored-by: Carmine Paolino <[email protected]>
1 parent f33e5d8 commit 1212984

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/ruby_llm/attachment.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def to_h
9999
def determine_mime_type
100100
return @mime_type = active_storage_content_type if active_storage? && active_storage_content_type.present?
101101

102-
@mime_type = RubyLLM::MimeType.for(@source, name: @filename)
102+
@mime_type = RubyLLM::MimeType.for(url? ? nil : @source, name: @filename)
103103
@mime_type = RubyLLM::MimeType.for(content) if @mime_type == 'application/octet-stream'
104104
@mime_type = 'audio/wav' if @mime_type == 'audio/x-wav' # Normalize WAV type
105105
end

spec/ruby_llm/chat_content_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,30 @@
156156
end
157157
end
158158
end
159+
160+
describe 'URL attachment handling' do # rubocop:disable RSpec/MultipleMemoizedHelpers
161+
it 'handles URL MIME type detection without ArgumentError' do
162+
attachment = RubyLLM::Attachment.new(image_url)
163+
164+
expect(attachment.mime_type).to be_present
165+
end
166+
167+
it 'creates content with URL attachments' do # rubocop:disable RSpec/MultipleExpectations
168+
content = RubyLLM::Content.new('Describe this image', image_url)
169+
170+
expect(content.attachments).not_to be_empty
171+
expect(content.attachments.first).to be_a(RubyLLM::Attachment)
172+
end
173+
174+
it 'prevents ArgumentError: wrong number of arguments when processing URL attachments' do # rubocop:disable RSpec/MultipleExpectations
175+
require 'open-uri' # required to trigger the marcel/open-uri compatibility issue
176+
177+
attachment = RubyLLM::Attachment.new(image_url)
178+
179+
expect { attachment.mime_type }.not_to raise_error
180+
181+
expect(attachment.mime_type).to eq('image/png')
182+
expect(attachment.send(:url?)).to be true
183+
end
184+
end
159185
end

0 commit comments

Comments
 (0)