Skip to content

Commit 68b809b

Browse files
Eric-Guocrmne
andauthored
Fix Encoding::CompatibilityError: Unicode Normalization not appropriate for ASCII-8BIT (#218)
Encoding::CompatibilityError: Unicode Normalization not appropriate for ASCII-8BIT is introduced after #215, commit: d34592e The fix is copy from: knu/ruby-domain_name#27 --------- Co-authored-by: Carmine Paolino <carmine@paolino.me>
1 parent d3ab332 commit 68b809b

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

lib/ruby_llm/tool.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ def parameters
4949
end
5050

5151
def name
52-
self.class.name
53-
.unicode_normalize(:nfkd)
54-
.encode('ASCII', replace: '')
55-
.gsub(/[^a-zA-Z0-9_-]/, '-')
56-
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
57-
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
58-
.downcase
59-
.delete_suffix('_tool')
52+
klass_name = self.class.name
53+
normalized = klass_name.to_s.dup.force_encoding('UTF-8').unicode_normalize(:nfkd)
54+
normalized.encode('ASCII', replace: '')
55+
.gsub(/[^a-zA-Z0-9_-]/, '-')
56+
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
57+
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
58+
.downcase
59+
.delete_suffix('_tool')
6060
end
6161

6262
def description

spec/ruby_llm/tool_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,13 @@
3232
stub_const('TestModule::SampleTool', Class.new(described_class))
3333
expect(TestModule::SampleTool.new.name).to eq('test_module--sample')
3434
end
35+
36+
it 'handles ASCII-8BIT encoded class names without raising Encoding::CompatibilityError' do
37+
# This simulates a class name that is ASCII-8BIT encoded
38+
tool_class = Class.new(described_class)
39+
ascii_8bit_name = 'SampleTool'.dup.force_encoding('ASCII-8BIT')
40+
allow(tool_class).to receive(:name).and_return(ascii_8bit_name)
41+
expect(tool_class.new.name).to eq('sample')
42+
end
3543
end
3644
end

0 commit comments

Comments
 (0)