Fix JSON::GeneratorError: source sequence is illegal/malformed (JSON::GeneratorError) in Sidekiq
This error occurs when Sidekiq tries to serialize job arguments to JSON but encounters data that cannot be converted, such as binary strings, circular references, or non-UTF-8 encoded text. Ensure all job arguments are JSON-serializable primitive types and convert binary data to Base64 or store it externally before passing to the job.
Reading the Stack Trace
Here's what each line means:
- json (2.7.1) lib/json/common.rb:224:in `generate': Ruby's JSON generator encounters bytes that cannot be encoded as valid JSON/UTF-8.
- sidekiq (7.2.1) lib/sidekiq/client.rb:58:in `normalize_item': Sidekiq normalizes the job payload by serializing it to JSON before pushing to Redis.
- app/controllers/uploads_controller.rb:18:in `create': The controller passes binary file data directly as a job argument.
Common Causes
1. Passing binary data as job argument
Raw file content with non-UTF-8 bytes is passed directly to a Sidekiq job.
class FileProcessingJob
include Sidekiq::Job
def perform(file_content)
# Process the file
end
end
# Controller
FileProcessingJob.perform_async(params[:file].read) # Binary data
2. Non-UTF-8 string arguments
A string with invalid encoding is passed as a job argument.
data = some_api_response.force_encoding('ASCII-8BIT')
ProcessingJob.perform_async(data) # Not valid UTF-8
3. Complex Ruby objects as arguments
Passing Ruby objects that are not JSON-serializable.
user = User.find(1)
NotificationJob.perform_async(user) # Should pass user.id, not the object
The Fix
Store the file using ActiveStorage and pass only the blob ID to the job. The job then retrieves and processes the file. This keeps job arguments small, JSON-serializable, and avoids passing binary data through Redis.
# Controller
def create
file = params[:file]
FileProcessingJob.perform_async(file.read)
end
# Controller
def create
file = params[:file]
blob = ActiveStorage::Blob.create_and_upload!(
io: file,
filename: file.original_filename
)
FileProcessingJob.perform_async(blob.id)
end
class FileProcessingJob
include Sidekiq::Job
def perform(blob_id)
blob = ActiveStorage::Blob.find(blob_id)
blob.open do |tempfile|
# Process the file from tempfile
end
end
end
Testing the Fix
require 'rails_helper'
require 'sidekiq/testing'
RSpec.describe FileProcessingJob do
before { Sidekiq::Testing.fake! }
it 'enqueues with a blob ID' do
blob = ActiveStorage::Blob.create_and_upload!(
io: StringIO.new('test content'),
filename: 'test.txt'
)
expect {
FileProcessingJob.perform_async(blob.id)
}.to change(FileProcessingJob.jobs, :size).by(1)
end
it 'processes the file from the blob' do
Sidekiq::Testing.inline!
blob = ActiveStorage::Blob.create_and_upload!(
io: StringIO.new('test content'),
filename: 'test.txt'
)
expect { FileProcessingJob.perform_async(blob.id) }.not_to raise_error
end
end
Run your tests:
bundle exec rspec spec/jobs/file_processing_job_spec.rb
Pushing Through CI/CD
git checkout -b fix/sidekiq-serialization,git add app/jobs/file_processing_job.rb app/controllers/uploads_controller.rb,git commit -m "fix: store file in ActiveStorage and pass blob ID to Sidekiq job",git push origin fix/sidekiq-serialization
Your CI config should look something like this:
name: CI
on:
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: postgres
ports: ['5432:5432']
redis:
image: redis:7
ports: ['6379:6379']
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: '3.3'
bundler-cache: true
- run: bin/rails db:setup
- run: bundle exec rspec
The Full Manual Process: 18 Steps
Here's every step you just went through to fix this one bug:
- Notice the error alert or see it in your monitoring tool
- Open the error dashboard and read the stack trace
- Identify the file and line number from the stack trace
- Open your IDE and navigate to the file
- Read the surrounding code to understand context
- Reproduce the error locally
- Identify the root cause
- Write the fix
- Run the test suite locally
- Fix any failing tests
- Write new tests covering the edge case
- Run the full test suite again
- Create a new git branch
- Commit and push your changes
- Open a pull request
- Wait for code review
- Merge and deploy to production
- Monitor production to confirm the error is resolved
Total time: 30-60 minutes. For one bug.
Or Let bugstack Fix It in Under 2 minutes
Every step above? bugstack does it automatically.
Step 1: Install the SDK
gem install bugstack
Step 2: Initialize
require 'bugstack'
Bugstack.init(api_key: ENV['BUGSTACK_API_KEY'])
Step 3: There is no step 3.
bugstack handles everything from here:
- Captures the stack trace and request context
- Pulls the relevant source files from your GitHub repo
- Analyzes the error and understands the code context
- Generates a minimal, verified fix
- Runs your existing test suite
- Pushes through your CI/CD pipeline
- Deploys to production (or opens a PR for review)
Time from error to fix deployed: Under 2 minutes.
Human involvement: zero.
Try bugstack Free →No credit card. 5-minute setup. Cancel anytime.
Deploying the Fix (Manual Path)
- Replace binary arguments with references like IDs or URLs.
- Store files in ActiveStorage or S3 before enqueuing jobs.
- Run job specs verifying serialization.
- Open a pull request.
- Merge and verify job processing in staging.
Frequently Asked Questions
BugStack runs the fix through your existing test suite, generates additional edge-case tests, and validates that no other components are affected before marking it safe to deploy.
BugStack never pushes directly to production. Every fix goes through a pull request with full CI checks, so your team can review it before merging.
Sidekiq supports strings, integers, floats, booleans, nil, arrays, and hashes with string keys. Always pass IDs instead of full objects.
Never pass large data as job arguments. Store the data in a database, S3, or cache and pass only a reference ID or URL to the job.