{"record":{"id":"66eda04232f859f4","repo":"medusajs/medusa","slug":"a-medusa-search-document-exceeds-the-max-write-b","errorCode":null,"errorMessage":"A Medusa search document exceeds the ${MAX_WRITE_BYTES}-byte provider limit","messagePattern":"A Medusa search document exceeds the (.+?)-byte provider limit","errorType":"validation","errorClass":"MedusaError","httpStatus":400,"severity":"error","filePath":"packages/modules/search/src/providers/search-medusa/services/medusa-search.ts","lineNumber":296,"sourceCode":"      },\n    }\n  }\n\n  async searchMany(\n    inputs: SearchTypes.ProviderSearchQuery[]\n  ): Promise<SearchTypes.SearchResult[]> {\n    return await Promise.all(inputs.map((input) => this.search(input)))\n  }\n\n  protected chunkRows(rows: Row[]): Row[][] {\n    const chunks: Row[][] = []\n    let chunk: Row[] = []\n    let bytes = 0\n\n    for (const row of rows) {\n      const rowBytes = Buffer.byteLength(JSON.stringify(row), \"utf8\")\n      if (rowBytes > MAX_WRITE_BYTES) {\n        throw new MedusaError(\n          MedusaError.Types.INVALID_DATA,\n          `A Medusa search document exceeds the ${MAX_WRITE_BYTES}-byte provider limit`\n        )\n      }\n      if (chunk.length && bytes + rowBytes > MAX_WRITE_BYTES) {\n        chunks.push(chunk)\n        chunk = []\n        bytes = 0\n      }\n      chunk.push(row)\n      bytes += rowBytes\n    }\n\n    if (chunk.length) {\n      chunks.push(chunk)\n    }\n    return chunks\n  }","sourceCodeStart":278,"sourceCodeEnd":314,"githubUrl":"https://github.com/medusajs/medusa/blob/5e06e544a296b9033f20f71f11c559f81a0e5739/packages/modules/search/src/providers/search-medusa/services/medusa-search.ts#L278-L314","documentation":"When upserting documents, each row is serialized to JSON and must fit within MAX_WRITE_BYTES on its own. A single document larger than that byte limit cannot be chunked and triggers this INVALID_DATA error.","triggerScenarios":"upsertDocuments (e.g. via search module index sync or a reindex) receives at least one row whose JSON.stringify byte length exceeds MAX_WRITE_BYTES — typically a product with huge description text, very long variant lists, or a large embedded vector/blob.","commonSituations":"Indexing products with big HTML descriptions, thousands of variants/options, or AI-generated vectors attached to documents; migrating from another provider with no document size cap and reindexing existing catalog data.","solutions":["Trim or truncate oversized text fields (e.g. long descriptions) before indexing","Split huge nested arrays (variants, metadata) or stop indexing the bloated field via the index field settings","Sanitize documents pre-upload: JSON.stringify and check byte length, and reduce/omit the offending field"],"exampleFix":"// before\nawait searchModuleService.upsertDocuments(indexName, products)\n// after - prune heavy fields before indexing\nconst slim = products.map((p) => ({\n  ...p,\n  description: p.description?.slice(0, 10_000),\n  metadata: undefined,\n}))\nawait searchModuleService.upsertDocuments(indexName, slim)\n","handlingStrategy":"validation","validationCode":"const MAX_WRITE_BYTES = 10 * 1024 * 1024 // match provider constant\nfunction pruneForIndex(doc) {\n  const clone = { ...doc }\n  for (const k of [\"metadata\", \"description\"]) {\n    if (clone[k] && Buffer.byteLength(JSON.stringify(clone[k]), \"utf8\") > 100_000) {\n      delete clone[k]\n    }\n  }\n  return Buffer.byteLength(JSON.stringify(clone), \"utf8\") <= MAX_WRITE_BYTES\n    ? clone\n    : null\n}","typeGuard":"const isWithinWriteLimit = (doc) =>\n  Buffer.byteLength(JSON.stringify(doc), \"utf8\") <= MAX_WRITE_BYTES","tryCatchPattern":"try { await upsertDocuments(index, docs) } catch (e) { if (e instanceof MedusaError && /provider limit/.test(e.message)) { /* split or truncate the offending document, then re-upsert the rest */ } throw e }","preventionTips":["Truncate long text fields before indexing","Exclude volatile heavy blobs (raw HTML, metadata) from the index definition","Add a byte-length check in bulk indexing pipelines and log offending document ids"],"tags":["search","document-size","indexing","limits"],"backgroundTag":"payload-too-large","analyzedSha":"5e06e544a296b9033f20f71f11c559f81a0e5739","analyzedAt":"2026-08-27T07:24:39.599Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}