pinecone-ts-client icon indicating copy to clipboard operation
pinecone-ts-client copied to clipboard

[Bug] Building for Vercel/Nextjs Edge Runtime fails on Dynamic Code Evaluation

Open DhenPadilla opened this issue 1 year ago • 5 comments

Is this a new bug?

  • [X] I believe this is a new bug
  • [X] I have searched the existing issues, and I could not find an existing issue for this bug

Current Behavior

I have a streaming RAG edge function API call that first calls pinecone (then cohere), then provides the context to OpenAI for completion.

I'm experiencing an issue with the building this new api call as it seems like the compilation of pinecone's sdk creates a new Function dynamic code evaluation snippet after ajv.compile() of the schema.

Full stack trace

Failed to compile.

./node_modules/@pinecone-database/pinecone/node_modules/ajv/dist/compile/index.js
Dynamic Code Evaluation (e. g. 'eval', 'new Function', 'WebAssembly.compile') not allowed in Edge Runtime
Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation

Import trace for requested module:
./node_modules/@pinecone-database/pinecone/node_modules/ajv/dist/compile/index.js
./node_modules/@pinecone-database/pinecone/node_modules/ajv/dist/core.js
./node_modules/@pinecone-database/pinecone/node_modules/ajv/dist/ajv.js
./node_modules/@pinecone-database/pinecone/dist/validator.js
./node_modules/@pinecone-database/pinecone/dist/pinecone.js
./node_modules/@pinecone-database/pinecone/dist/index.js
./src/utils/rag.ts
./src/utils/streamableRag.ts

I inspected the code that was written before the compile (validator.js) - and it seems that there already is a catch for edge Runtime? But somehow gets ignored

var buildValidator = function (errorMessagePrefix, schema) {
    if ((0, environment_1.isEdge)()) {
        // Ajv schema compilation does not work in the Edge Runtime.
        return function (data) { }; // eslint-disable-line
    }
    if (typeof process !== 'undefined' &&
        process &&
        process.env &&
        process.env.PINECONE_DISABLE_RUNTIME_VALIDATIONS) {
        // Runtime method validations are most useful when learning to use the client
        // in an interactive REPL or when developing an application that does not use
        // Typescript to provide the benefits of static type-checking. However, if your
        // application is using Typescript and/or you have gained confidence of correct
        // usage through testing, you may want to disable these runtime validations
        // to improve performance.
        //
        // The PINECONE_DISABLE_RUNTIME_VALIDATIONS env var provides a way to disable
        // all runtime validation. If it is set, all validator functions will immediately
        // return without performing any validation.
        return function (data) { }; // eslint-disable-line
    }
    var ajv = new ajv_1.default({ allErrors: true });
    var validate = ajv.compile(schema);
    return function (data) {
        var valid = validate(data);
        if (!valid) {
            var errors = validate.errors || [];
            var msg = (0, exports.errorFormatter)(errorMessagePrefix, errors);
            throw new errors_1.PineconeArgumentError(msg);
        }
        return data;
    };
};

Right now, I'm using:

unstable_allowDynamic: [
        '**/node_modules/@pinecone-database/pinecone/node_modules/ajv/dist/compile/index.js',
    ],

to address build failures and compile to completion, though my hope here is so that I don't have to use this workaround as I'd rather not have the security vulnerability.

Expected Behavior

I was expecting an npm run build with pinecone index instantiated in my edge run time runs to completion without using unstable_allowDynamic

Steps To Reproduce

  1. Create an edge function in next using a pages-api route (not a router api)
  2. Under the edge function, set:
export const config = {
    runtime: 'edge'
}
  1. Create new Pinecone: 1. client, 2. index and 3. query call in the edge function

Using the OS and the given pinecone node sdk version + next 13.5.6 next build

Relevant log output

No response

Environment

- **OS**: macOS 13.5 v22G74
- **Language version**: Node 18.15.0
- **Pinecone client version**: 1.1.2 (`"@pinecone-database/pinecone": "^1.1.2`)
- **Next JS**: 13.5.6 (`"next": "13.5.6"`)

Additional Context

No response

DhenPadilla avatar Jan 10 '24 11:01 DhenPadilla