How It Works Features Pricing Blog Error Guides
Log In Start Free Trial
Sidekiq · Ruby

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

JSON::GeneratorError (source sequence is illegal/malformed): json (2.7.1) lib/json/common.rb:224:in `generate' sidekiq (7.2.1) lib/sidekiq/client.rb:58:in `normalize_item' sidekiq (7.2.1) lib/sidekiq/client.rb:42:in `push' sidekiq (7.2.1) lib/sidekiq/job.rb:180:in `perform_async' app/controllers/uploads_controller.rb:18:in `create' actionpack (7.1.3) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'

Here's what each line means:

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.

Before (broken)
# Controller
def create
  file = params[:file]
  FileProcessingJob.perform_async(file.read)
end
After (fixed)
# 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:

  1. Notice the error alert or see it in your monitoring tool
  2. Open the error dashboard and read the stack trace
  3. Identify the file and line number from the stack trace
  4. Open your IDE and navigate to the file
  5. Read the surrounding code to understand context
  6. Reproduce the error locally
  7. Identify the root cause
  8. Write the fix
  9. Run the test suite locally
  10. Fix any failing tests
  11. Write new tests covering the edge case
  12. Run the full test suite again
  13. Create a new git branch
  14. Commit and push your changes
  15. Open a pull request
  16. Wait for code review
  17. Merge and deploy to production
  18. 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:

  1. Captures the stack trace and request context
  2. Pulls the relevant source files from your GitHub repo
  3. Analyzes the error and understands the code context
  4. Generates a minimal, verified fix
  5. Runs your existing test suite
  6. Pushes through your CI/CD pipeline
  7. 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)

  1. Replace binary arguments with references like IDs or URLs.
  2. Store files in ActiveStorage or S3 before enqueuing jobs.
  3. Run job specs verifying serialization.
  4. Open a pull request.
  5. 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.