Encoding::InvalidByteSequenceError
RubyERRORNotableEncoding
Byte sequence is invalid in source encoding
Quick Answer
Use encode with invalid: :replace or String#scrub to sanitise invalid bytes rather than letting them raise.
What this means
Raised when a string contains bytes that are invalid for its declared encoding. Common when reading binary data, legacy files, or network responses that are not properly tagged with their encoding.
Why it happens
- 1Reading a non-UTF-8 file without specifying the source encoding
- 2Receiving raw binary data over a socket and treating it as UTF-8
Fix
Scrub invalid bytes
Scrub invalid bytes
safe = raw.scrub('') # removes invalid bytes
safe = raw.scrub { |b| "\x#{b.unpack1('H*')}" } # replace with hexWhy this works
String#scrub replaces or removes byte sequences that are invalid in the string's current encoding.
Code examples
Reproducing the errorruby
"ÿþ".encode('UTF-8', 'UTF-8')
# Encoding::InvalidByteSequenceError: "ÿ" on UTF-8Safe encode with replaceruby
clean = str.encode('UTF-8', invalid: :replace, replace: '?')String#scrubruby
clean = str.scrub # replaces invalid bytes with U+FFFD
Same error in other languages
Sources
Official documentation ↗
Ruby Core Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev