diff --git a/lib/ruby_llm/attachment.rb b/lib/ruby_llm/attachment.rb index f5098365f..36251e136 100644 --- a/lib/ruby_llm/attachment.rb +++ b/lib/ruby_llm/attachment.rb @@ -37,7 +37,7 @@ def active_storage? @source.is_a?(ActiveStorage::Attached::Many) end - def content + def content # rubocop:disable Metrics/PerceivedComplexity return @content if defined?(@content) && !@content.nil? if url? @@ -53,6 +53,8 @@ def content nil end + @content&.force_encoding(Encoding::UTF_8) if text? + @content end diff --git a/spec/fixtures/multilingual.txt b/spec/fixtures/multilingual.txt new file mode 100644 index 000000000..a6042ec87 --- /dev/null +++ b/spec/fixtures/multilingual.txt @@ -0,0 +1 @@ +Hei på deg diff --git a/spec/ruby_llm/attachment_spec.rb b/spec/ruby_llm/attachment_spec.rb index 2bcd8cd92..2096200f3 100644 --- a/spec/ruby_llm/attachment_spec.rb +++ b/spec/ruby_llm/attachment_spec.rb @@ -22,4 +22,10 @@ expect(status.success?).to be(true), stderr expect(stdout.strip).to eq('ruby.txt,text/plain') end + + it 'serializes non-ASCII text/* attachment content as JSON' do + attachment = described_class.new('spec/fixtures/multilingual.txt') + + expect { JSON.generate(text: attachment.content) }.not_to output.to_stderr + end end