Skip to content

RangeError: Maximum call stack size exceeded when uploading image (in version 0.30.1) #270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ocluf opened this issue Jun 10, 2024 · 4 comments · Fixed by #284
Closed

RangeError: Maximum call stack size exceeded when uploading image (in version 0.30.1) #270

ocluf opened this issue Jun 10, 2024 · 4 comments · Fixed by #284

Comments

@ocluf
Copy link

ocluf commented Jun 10, 2024

I get the following error:

RangeError: Maximum call stack size exceeded
    at bytesToBase64 (/Users/fwtaen/projects/viralgifs/node_modules/.pnpm/replicate@0.30.1/node_modules/replicate/lib/util.js:148:35)
    at /Users/fwtaen/projects/viralgifs/node_modules/.pnpm/replicate@0.30.1/node_modules/replicate/lib/util.js:303:18
    at transform (/Users/fwtaen/projects/viralgifs/node_modules/.pnpm/replicate@0.30.1/node_modules/replicate/lib/util.js:329:16)
    at transform (/Users/fwtaen/projects/viralgifs/node_modules/.pnpm/replicate@0.30.1/node_modules/replicate/lib/util.js:324:25)
    at transformFileInputsToBase64EncodedDataURIs (/Users/fwtaen/projects/viralgifs/node_modules/.pnpm/replicate@0.30.1/node_modules/replicate/lib/util.js:277:16)
    at transformFileInputs (/Users/fwtaen/projects/viralgifs/node_modules/.pnpm/replicate@0.30.1/node_modules/replicate/lib/util.js:238:22)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Replicate.createPrediction (/Users/fwtaen/projects/viralgifs/node_modules/.pnpm/replicate@0.30.1/node_modules/replicate/lib/predictions.js:33:16)
    at async Replicate.run (/Users/fwtaen/projects/viralgifs/node_modules/.pnpm/replicate@0.30.1/node_modules/replicate/index.js:140:20)
    at async main (file:///Users/fwtaen/projects/viralgifs/index.js:17:18)
done

When trying to upload an image like this

import { readFile } from 'fs/promises';

import Replicate from 'replicate';

const replicate = new Replicate({
	auth: process.env.REPLICATE_AUTH
});

async function main() {
	try {
		let file = await readFile(
			'/Users/fwtaen/projects/viralgifs/static/matrix_images/frame_0185.png'
		);

		const output = await replicate.run(
			'adirik/grounding-dino:efd10a8ddc57ea28773327e881ce95e20cc1d734c589f7dd01d2036921ed78aa',
			{
				input: {
					image: file,
					query: 'red pill',
					box_threshold: 0.2,
					text_threshold: 0.2,
					show_visualisation: true
				}
			}
		);
		console.log(output);
	} catch (error) {
		console.error(error);
	} finally {
		console.log('done');
	}
}
main();

I already found this issue #247 which says a fix has been merged in v 0.30.0. However both 0.30.0 and 0.30.1 still give me this error while using pnpm install replicate/replicate-javascript#fix-rangeerror seemed to fix it for me.

I'm on node v20.11.0` M1 pro, macos 14.4.1 (23E224)

@mattt
Copy link
Contributor

mattt commented Jun 10, 2024

Hi @ocluf. There was a problem with the automatic file upload functionality that caused the client library to fall back on the old bytesToBase64 behavior. Please update to 0.30.2 and try again.

@ocluf
Copy link
Author

ocluf commented Jun 10, 2024

I think I messed up the version in this issue. When install 0.30.2 with pnpm i replicate@latest I still get this:

RangeError: Maximum call stack size exceeded
    at bytesToBase64 (/Users/fwtaen/projects/viralgifs/node_modules/.pnpm/replicate@0.30.2/node_modules/replicate/lib/util.js:148:35)
    at /Users/fwtaen/projects/viralgifs/node_modules/.pnpm/replicate@0.30.2/node_modules/replicate/lib/util.js:303:18
    at transform (/Users/fwtaen/projects/viralgifs/node_modules/.pnpm/replicate@0.30.2/node_modules/replicate/lib/util.js:329:16)
    at transform (/Users/fwtaen/projects/viralgifs/node_modules/.pnpm/replicate@0.30.2/node_modules/replicate/lib/util.js:324:25)
    at transformFileInputsToBase64EncodedDataURIs (/Users/fwtaen/projects/viralgifs/node_modules/.pnpm/replicate@0.30.2/node_modules/replicate/lib/util.js:277:16)
    at transformFileInputs (/Users/fwtaen/projects/viralgifs/node_modules/.pnpm/replicate@0.30.2/node_modules/replicate/lib/util.js:238:22)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Replicate.createPrediction (/Users/fwtaen/projects/viralgifs/node_modules/.pnpm/replicate@0.30.2/node_modules/replicate/lib/predictions.js:33:16)
    at async Replicate.run (/Users/fwtaen/projects/viralgifs/node_modules/.pnpm/replicate@0.30.2/node_modules/replicate/index.js:140:20)
    at async main (file:///Users/fwtaen/projects/viralgifs/index.js:15:18)
done

I removed the node_modules folder and reinstalled everything with npm to make sure there weren't any issues there. Still got this

node index.js 
RangeError: Maximum call stack size exceeded
    at bytesToBase64 (/Users/fwtaen/projects/viralgifs/node_modules/replicate/lib/util.js:148:35)
    at /Users/fwtaen/projects/viralgifs/node_modules/replicate/lib/util.js:303:18
    at transform (/Users/fwtaen/projects/viralgifs/node_modules/replicate/lib/util.js:329:16)
    at transform (/Users/fwtaen/projects/viralgifs/node_modules/replicate/lib/util.js:324:25)
    at transformFileInputsToBase64EncodedDataURIs (/Users/fwtaen/projects/viralgifs/node_modules/replicate/lib/util.js:277:16)
    at transformFileInputs (/Users/fwtaen/projects/viralgifs/node_modules/replicate/lib/util.js:238:22)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Replicate.createPrediction (/Users/fwtaen/projects/viralgifs/node_modules/replicate/lib/predictions.js:33:16)
    at async Replicate.run (/Users/fwtaen/projects/viralgifs/node_modules/replicate/index.js:140:20)
    at async main (file:///Users/fwtaen/projects/viralgifs/index.js:15:18)
done

Edit: nevermind you just published 0.30.2. That is a quick response even though it doesn't seem to have fixed the problem on my side, thank you!

@Acconut
Copy link
Contributor

Acconut commented Jun 14, 2024

I just ran into the same problem and in my case it was caused by a missing API token. I forgot to set the environment variable REPLICATE_API_TOKEN. Apparently, if the token is missing, the file uploads fail causing the SDK to fall back to Base64, which runs into the stack limit for larger files. Adding the token fixed it for me, but a better error message from the SDK would be helpful.

aron added a commit that referenced this issue Jul 5, 2024
Fixes #270

We were falling back to base64 encoding the file data when requests to
upload the file failed. However if the client makes an invalid request
such as failing to include Authorization headers by forgetting to auth
then we should surface these errors.

This commit now re-raises any 4xx errors returned while attempting to
upload a file and adds tests to verify the behavior.
@mattt mattt closed this as completed in #284 Jul 5, 2024
mattt pushed a commit that referenced this issue Jul 5, 2024
Fixes #270

We were falling back to base64 encoding the file data when requests to
upload the file failed. However if the client makes an invalid request
such as failing to include Authorization headers by forgetting to auth
then we should surface these errors.

This commit now re-raises any 4xx errors returned while attempting to
upload a file and adds tests to verify the behavior.
@Sofianel5
Copy link

Getting this error under the same circumstances in 0.30.2 and 0.31.1. Double checked that my API token is being passed in and force reinstalled packages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants