The sub workflows sometimes return output "error": "Invalid string length"

My problem

I use n8n to extract data from PDF files to text. I created a main workflow and a sub-workflow to perform the task.

  • Main Workflow: The main workflow periodically retrieves a list of PDF documents that need to be extracted and sends them to the sub-workflow.
  • Sub-workflow: The sub-workflow essentially converts the PDF files into PNG images and uses Google Gemini for Optical Character Recognition (OCR). Afterward, it uses a “Loop Over Items” node to iterate through the images. The output of the sub-workflow is the Google Docs document ID.

“My problem is that sometimes, during execution, the sub-workflow returns the desired results, but other times it throws an error: ‘Invalid string length’.”

What is the error message (if any)?

“error”: “Invalid string length”

Information on your n8n setup

  • n8n version: 1.101.1
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: Ubuntu 24.04.2

You might be converting a binary (like an image or PDF) to a string without proper encoding (e.g., base64), causing the string to blow up in size.

1 Like

thank for your reply, but i have the node “Extract from File” Move png File to Base64 String

i see, could i also see the parameters in the HTTP request to see the input and output

1 Like

yes, i have 4 http node at sub-workflow:

  1. get file on local: get file from telegram server
  2. basic-info: return some infomation of the pdf file (file name, page, file size)
  3. convert pdf to image: convert pdf file to image (return zip file, if the pdf file has 100 pages, these page compressed to single zip file
  4. image2text: send base64 of image file to gemini api and return text

my issue:

  • if i have the pdf file with 10 pages, my sub-workflow will work well
  • but if i have the pdf file with 100 page, my main workflow will return error at “Execute Workflow” node: “error”: “Invalid string length”

@BramKn can you help me

Ahhh.. Ok that was the important information I needed. Then you need to have to split it in batches to avoid API rate limits

1 Like

Yes, I’ve checked the Google Gemini rate limits and don’t foresee any problems. My Google Docs output file confirms this, as it continues to receive all text extracted from images sent to the Gemini API.

But I can’t get the output from the sub-workflow to send notifications to the user upon job completion

The issue you’re encountering with the “Invalid string length” error is happening because at some point in your sub-workflow, you’re passing or manipulating a base64 string that is too large for the JavaScript engine that powers n8n to handle. This doesn’t happen on every file because only some PDFs produce large enough base64 strings to trigger the limit. Here’s what I did to fix this in my case. First, I traced where the base64 string was being generated. In my case, it happened right after converting a PDF into images. I noticed that I was immediately transforming the image buffer into a base64 string and passing that along into other nodes including HTTP Request and Update Document. That’s where the problem occurred. The fix was to avoid converting the image into a base64 string too early. Instead, I kept the image in binary format and only converted it to base64 right before the node that needed it — in my case, a POST request to a custom OCR API endpoint. This drastically reduced the chance of memory overflow. Next, inside the Loop Over Items node, I avoided putting large base64 strings into Set nodes or directly into the JSON path because it made them very slow and prone to breaking. If I absolutely had to convert to base64, I used a Function node right before the API call, and I added a small safeguard like if (data.length > 10000000) throw new Error(“base64 too long”) to gracefully fail instead of crashing the whole run. I also ensured that Merge nodes or anything that tried to combine multiple outputs didn’t carry large base64 fields with them unless needed. After making those changes, the subworkflow stopped failing randomly and became fully reliable, even on larger files. The key learning here is that in n8n, you need to be intentional about when you convert binary to base64 and avoid pushing large strings through too many intermediate nodes, especially inside loops.

2 Likes
  • As you can observe, the ‘merge1’ node does not allow any data to pass through.
  • Prior to invoking the Gemini API, I utilized the ‘Extract from File’ node to convert the file into a Base64 string.

@zynate

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.