Error importing data into Sanity via CLI due to malformed data and internal server error.

14 replies
Last updated: Nov 7, 2023
I’m trying to import data into Sanity via the CLI but I’m getting the following error:
sanity dataset import ../json/ndjson/sources.ndjson production --replace
✔ [100%] Fetching available datasets
✔ [100%] Reading/validating data file (96ms)
✖ [  0%] Importing documents (2.92s)

Error: Mutation failed: Internal server error:
- Internal server error
    at onResponse (~/.nvm/versions/node/v18.6.0/lib/node_modules/@sanity/cli/lib/_chunks/loadEnv-6ca0aa8a.js:1234:13)
    at applyMiddleware (~/.nvm/versions/node/v18.6.0/lib/node_modules/@sanity/cli/node_modules/get-it/dist/index.cjs:59:13)
    at onResponse (~/.nvm/versions/node/v18.6.0/lib/node_modules/@sanity/cli/node_modules/get-it/dist/index.cjs:103:22)
    at ~/.nvm/versions/node/v18.6.0/lib/node_modules/@sanity/cli/node_modules/get-it/dist/index.cjs:130:55
    at callback (~/.nvm/versions/node/v18.6.0/lib/node_modules/@sanity/cli/node_modules/get-it/dist/index.cjs:485:46)
    at ~/.nvm/versions/node/v18.6.0/lib/node_modules/@sanity/cli/node_modules/get-it/dist/index.cjs:548:14
    at DestroyableTransform.<anonymous> (~/.nvm/versions/node/v18.6.0/lib/node_modules/@sanity/cli/node_modules/get-it/dist/index.cjs:261:13)
    at Object.onceWrapper (node:events:627:28)
    at DestroyableTransform.emit (node:events:525:35)
    at endReadableNT (~/.nvm/versions/node/v18.6.0/lib/node_modules/@sanity/cli/node_modules/readable-stream/lib/_stream_readable.js:1010:12)
AI Update

I understand you're hitting an "Internal server error" when trying to import an NDJSON file into Sanity. This is a frustrating error because it's not immediately clear what's causing it. Let me help you troubleshoot this.

The "Internal server error" during sanity dataset import typically indicates one of these issues:

Common Causes and Solutions

1. Document Size Limits

Sanity has document size limits. If your sources.ndjson file contains any documents that are too large, the import will fail with an internal server error. According to the importing data documentation, check your NDJSON file for unusually large documents:

# Check individual document sizes (line length as rough proxy)
awk '{print length, NR}' sources.ndjson | sort -rn | head

If you find very large documents, you'll need to split them or reduce their size before importing.

2. Invalid NDJSON Format

Each line must be a valid, complete JSON object. Common issues include:

  • Missing commas or brackets
  • Invalid escape characters
  • Non-UTF8 characters
  • Empty lines

Validate your NDJSON format:

# Test if each line is valid JSON
while IFS= read -r line; do echo "$line" | jq empty; done < sources.ndjson

3. Schema Validation Issues

If your documents don't match your current schema (missing required fields, invalid field types), the import can fail. Make sure your schema is deployed and matches the data structure in your NDJSON file.

4. Asset References

If your NDJSON references assets that don't exist in the target dataset, this can cause issues. The import documentation notes that assets need to be available when importing documents that reference them.

Debugging Steps

Enable debug logging to get more detailed error information:

# Set debug logging level
SANITY_LOG_LEVEL=debug sanity dataset import ../json/ndjson/sources.ndjson production --replace

Or use the --debug flag if available in your CLI version.

Try Importing in Smaller Batches

If the file is large, try splitting it into smaller chunks:

# Split into files with 100 documents each
split -l 100 sources.ndjson sources-part-

# Import each part
for file in sources-part-*; do
  sanity dataset import "$file" production
done

This helps identify which specific document(s) are causing the issue.

Alternative: Use the Client Library

If CLI import continues to fail, you can use the JavaScript client to import documents with better error handling:

import {createClient} from '@sanity/client'
import fs from 'fs'

const client = createClient({
  projectId: 'your-project-id',
  dataset: 'production',
  token: 'your-token',
  useCdn: false
})

const ndjson = fs.readFileSync('sources.ndjson', 'utf8')
const documents = ndjson.split('\n').filter(Boolean).map(JSON.parse)

for (const doc of documents) {
  try {
    await client.createOrReplace(doc)
    console.log(`Imported: ${doc._id}`)
  } catch (err) {
    console.error(`Failed to import ${doc._id}:`, err.message)
  }
}

This gives you line-by-line error reporting to identify problematic documents.

Check API Rate Limits

As mentioned in the importing data guide, if you're doing a programmatic import, make sure you're respecting API rate limits by using a queue with reasonable concurrency.

If none of these solutions work, I'd recommend reaching out to Sanity support with the debug logs and a sample of your NDJSON file (with sensitive data removed) so they can investigate the specific internal server error.

At first I thought that it was because my data was malformed since I was trying to import new data. So then I tried exporting the data that was already in Sanity via the CLI. That worked correctly. Then I made some small change to a title field and tried importing it and it gave me the same error:
Mutation failed: internal server error
.
So even the exported data couldn’t be imported.
Then I thought that maybe it was because I had made changes locally in my Sanity studio that had not been deployed. Surely it was that. So I deployed and tried again and received the same error.
Then I thought to check the sanity status page and I saw this: https://www.sanity-status.com/incidents/dl95rf3n4rfw
It does mention
dataset
so I thought it could be related. But on the other hand it doesn’t seem like it is related and it says that this issue is already resolved.
It’s not giving me much indication of what the error is. Just
Internal server error
.
Is it a permissions issue?
I just checked the billing details of my plan. I’m on the free plan and it said that I should add billing details just incase there are overages. I added my billing details and tried the import again, which is just 2 records, and I’m still getting the same error.
I’m running
"sanity": "^3.19.1"
and I have
sanity/cli
installed globally at
3.19.1
also.
It seems that my data the I’m trying to import is malformed or has some character in it that is causing the error.
Still trying to find the exact cause.
Ok, I found the error: I had made new
_id
fields for each entry based off of an existing string field in the incoming data. One record had a
.
(period) in it and that is not allowed to be in the
_id
field. After I fixed that everything imported just fine.
🎉
It also appears that there were two records that had malformed dates.

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?