Skip to content
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

[Bug] v1 Index<T>.query throwing TypeError: Cannot read properties of undefined (reading 'text') during migration #124

Closed
2 tasks done
mattgraphlan opened this issue Sep 19, 2023 · 17 comments
Labels
bug Something isn't working

Comments

@mattgraphlan
Copy link

Is this a new bug?

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

Current Behavior

I'm migrating to v1 as described in https://github.com/pinecone-io/pinecone-ts-client/blob/main/v1-migration.md#query

I can upsert after migrating just fine, but I can't query. My code is as follows:

    const namespace = ""
    const topK = 10
    const queryRequest = {
      vector: embeddings,
      topK,
      includeMetadata: true,
      includeValues: true,
    } as QueryByVectorValues;
    try {
      // Query the index with the defined request
      const ns = index.namespace(namespace);
      const queryResult = await ns.query(queryRequest);
      return queryResult.matches || [];
    } catch (e) {
      console.error("Error querying embeddings: ", e, (e as Error).stack);
      throw new Error(`Error querying embeddings: ${e}`);
    }

I have verified that there are actually embeddings (RecordValues).

The stack trace is a bit unhelpful:

TypeError: Cannot read properties of undefined (reading 'text')
    at eval (webpack-internal:///(rsc)/./node_modules/@pinecone-database/pinecone/dist/errors/utils.js:136:40)
    at step (webpack-internal:///(rsc)/./node_modules/@pinecone-database/pinecone/dist/errors/utils.js:107:23)
    at Object.eval [as next] (webpack-internal:///(rsc)/./node_modules/@pinecone-database/pinecone/dist/errors/utils.js:48:20)
    at eval (webpack-internal:///(rsc)/./node_modules/@pinecone-database/pinecone/dist/errors/utils.js:26:71)
    at new Promise (<anonymous>)
    at __awaiter (webpack-internal:///(rsc)/./node_modules/@pinecone-database/pinecone/dist/errors/utils.js:8:12)
    at extractMessage (webpack-internal:///(rsc)/./node_modules/@pinecone-database/pinecone/dist/errors/utils.js:129:12)
    at eval (webpack-internal:///(rsc)/./node_modules/@pinecone-database/pinecone/dist/errors/handling.js:178:76)
    at step (webpack-internal:///(rsc)/./node_modules/@pinecone-database/pinecone/dist/errors/handling.js:107:23)
    at Object.eval [as next] (webpack-internal:///(rsc)/./node_modules/@pinecone-database/pinecone/dist/errors/handling.js:48:20)
    at eval (webpack-internal:///(rsc)/./node_modules/@pinecone-database/pinecone/dist/errors/handling.js:26:71)
    at new Promise (<anonymous>)
    at __awaiter (webpack-internal:///(rsc)/./node_modules/@pinecone-database/pinecone/dist/errors/handling.js:8:12)
    at eval (webpack-internal:///(rsc)/./node_modules/@pinecone-database/pinecone/dist/errors/handling.js:171:36)
    at eval (webpack-internal:///(rsc)/./node_modules/@pinecone-database/pinecone/dist/errors/handling.js:151:25)
    at step (webpack-internal:///(rsc)/./node_modules/@pinecone-database/pinecone/dist/errors/handling.js:107:23)

Any ideas?

Expected Behavior

No error to be thrown by ns.query, or, a helpful error message or stack trace.

Steps To Reproduce

See current behavior code.

Relevant log output

No response

Environment

- **OS**: Mac OS 13.5.2 (22G91)
- **Language version**: TypeScript, Nextjs 13
- **Pinecone client version**: 1.0.1

Additional Context

No response

@mattgraphlan mattgraphlan added the bug Something isn't working label Sep 19, 2023
@mattgraphlan
Copy link
Author

FWIW, this works in place of ns.query above:

    const url = `https://${process.env.PINECONE_HOST!}/query`;

    const res = await fetch(url, {
      method: "POST",
      headers: {
        accept: "application/json",
        "Content-Type": "application/json",
        "Api-Key": process.env.PINECONE_API_KEY || "unknown",
      },
      body: JSON.stringify({
        includeValues: false,
        includeMetadata: true,
        topK: topK,
        vector: embeddings,
      }),
    });
    const resJson = await res.json();
    return resJson;

@vishwajeetraj11
Copy link

getting the same error? the above solution didn't work for me.

@gbertb
Copy link

gbertb commented Sep 21, 2023

I'm getting the same error as well. Using the config laid out from the read me

@jhamon
Copy link
Collaborator

jhamon commented Sep 21, 2023

Sorry all for the trouble. Looking into it now.

@adelowo
Copy link

adelowo commented Sep 23, 2023

@jhamon any luck here?

@vishwajeetraj11
Copy link

        const url = `${process.env.PINECONE_HOST!}/query`;

        const res = await fetch(url, {
            method: "POST",
            headers: {
                accept: "application/json",
                "Content-Type": "application/json",
                "Api-Key": process.env.PINECONE_API_KEY || "unknown",
            },
            body: JSON.stringify({
                includeValues: false,
                includeMetadata: true,
                topK: <topK>,
                vector: embeddings,
                namespace: <namespace>,
            }),
        });
        const resJson = await res.json() as QueryResponse<RecordMetadata>;

This worked for me.

@jhamon
Copy link
Collaborator

jhamon commented Sep 26, 2023

Is anyone seeing this error outside of a next.js context? My working theory is that the fetch implementation is missing/non-functional due to next.js stubbing out the cross-fetch polyfill we rely on. But if people are also seeing this outside the context of a next.js app, we might be dealing with multiple unrelated issues that result in similar errors.

jhamon added a commit that referenced this issue Sep 27, 2023
## Problem

Next.js seems to stub out the cross-fetch polyfill which has the effect
of silently breaking code which depends on cross-fetch to make API
requests.

Related bugs: 
- #124 
- v1 migration problems in
pinecone-io/pinecone-vercel-starter#14

## Solution

I have added a `getFetch` utility which looks for a `fetch`
implementation already loaded in the global scope and returns the
cross-fetch implementation only as a last resort.

- Now when using our client library in a next.js project, this should
result in the `@vercel/fetch` implementation being used in favor of the
disabled polyfill.
- When using this client library in other environments where next.js is
not being used, it still activates the cross-fetch polyfill.
- Also, I've exposed an experimental configuration option for anyone who
wants to pass in a different fetch implementation to use. This could
open up some flexibility for people with specific network configurations
who need the ability to configure the http client being used in some
way. If the user provides a fetch implementation through the `fetchApi`
config option, it will take priority over a fetch in the global scope or
the cross-fetch polyfill.

### Other changes

- Added additional integration tests for `query` method
- Added an explicit dependency on the `encoding` module to resolve a
warning message [similar to this
one](vercel/next.js#54109)

## Type of Change

- [x] Bug fix (non-breaking change which fixes an issue)

## Test Plan

I am currently testing this change in the context of work-in-progress to
migrate our vercel-starter example to using the v1 version of the
client. pinecone-io/pinecone-vercel-starter#14

- Checkout the PR branch from pinecone-vercel-starter in a sibling
directory
- Install my local changes to this client with `npm install
../pinecone-ts-client`
- Try to run the starter project with `npm run dev`
- Attempt to use the sample app and observe it progresses beyond the
error related broken cross-fetch
@jhamon
Copy link
Collaborator

jhamon commented Sep 28, 2023

I think I've solved this issue in the v1.1.0 release. Please upgrade and let us know if you're still having a problem with this.

@Nipunwalia08
Copy link

Nipunwalia08 commented Oct 2, 2023

I think I've solved this issue in the v1.1.0 release. Please upgrade and let us know if you're still having a problem with this.

Updated the package but still facing the issue
image

error querying embeddings [TypeError: Cannot read properties of undefined (reading 'text')] ⨯ node_modules\next\dist\esm\server\future\route-modules\app-route\module.js (200:34) @ eval ⨯ No response is returned from route handler 'C:\Users\nipun\Documents\pdfchat\pdfchat\src\app\api\chat\route.ts'. Ensure you return a Responseor aNextResponse in all branches of your handler. null

@jhamon
Copy link
Collaborator

jhamon commented Oct 2, 2023

@Nipunwalia08 From that stacktrace alone I'm not sure that this is a Pinecone error. Can you open a fresh issue in this repo (just to focus the discussion around your situation) and include some additional info about what you have in your src/app/api/chat/route.ts file, your next.js version, etc?

@jhamon jhamon closed this as completed Oct 3, 2023
@mario-oliver
Copy link

@jhamon thanks for taking a look and fixing the issue in a new release!

Interestingly enough, I am getting this issue (with the same stack trace as OP) when upserting. I created a simple PDF from which to debug to see if any of the created vectors contained undefined metadata that would cause the reading of the text property to error out.

Just to start the conversation, would there be any resemblance between the ns.query() and the ns.upsert()'s implementation of the cross-fetch polyfill that pinecone relies on? Let me know if anything jumps out at first glance; otherwise, I will open a fresh issue with further details.

@jhamon
Copy link
Collaborator

jhamon commented Oct 6, 2023

@jhamon thanks for taking a look and fixing the issue in a new release!

Interestingly enough, I am getting this issue (with the same stack trace as OP) when upserting. I created a simple PDF from which to debug to see if any of the created vectors contained undefined metadata that would cause the reading of the text property to error out.

Just to start the conversation, would there be any resemblance between the ns.query() and the ns.upsert()'s implementation of the cross-fetch polyfill that pinecone relies on? Let me know if anything jumps out at first glance; otherwise, I will open a fresh issue with further details.

I'm wondering if there is something happening that is tied to a specific version of nextjs, since I did a lot of hands-on testing with our pinecone-vercel-starter (see the upgrade/troubleshooting saga here) where I was able to repro the issue with 1.0.1 and see it resolved in 1.1.0 after adding in some additional logic about when to use cross-fetch. That's why I closed this issue, since the original filers have not chimed in to say the problem was ongoing and I had confidence from my own testing.

The missing text() error is not related to metadata. It happens when calling extractMessage in this handleApiError error handler function. There are a couple of different error callbacks involved here, but by the time this function is executing 1) an error occurred 2) handleFetchError already checked whether the error is a FetchError (occurs when a fetch is made but no server response comes back, either due to a misconfiguration, a Pinecone outage, or other network problem affecting the request) and so 3) we expect any errors left over are ResponseError (i.e. non-200 HTTP responses). .text() is expected on ResponseError objects, but whatever is being thrown here is evidently something else that does not have this method.

It seems like there is at least one other error case not covered here (i.e. FetchError and ResponseError are not the only things that can occur). I should check rather than casting here to avoid masking the real issue.

@jhamon jhamon reopened this Oct 6, 2023
jhamon added a commit that referenced this issue Oct 9, 2023
## Problem

Some users are still running into errors that can't be properly
diagnosed because of a bug in error handling causing the real error to
get obscured by an unrelated problem. There's a point at which we assume
we should be dealing with a `ResponseError` with a `text()` field; in
reality, that's not a correct assumption so it's not safe to cast to
that type.

- #124 

## Solution

This was one of those changes where you pull on the thread and the whole
carpet unravels.

- Refactor error handling away from nested callbacks into a flat error
handler function
- Wrap the underlying exception by attaching it as a `cause` whenever a
`PineconeConnectionError` exception is being thrown. This will help us
investigate #124
- Migrate away from method-specific error handling to a consistent
approach based on middleware functions. We already had an
httpErrorMapper, but with this approach it only has to get wired up
twice instead of like 20 times so that really simplifies what we need to
cover in unit testing.
- Implement a bunch of new integration tests to give confidence the
middleware is actually working as expected.
- The cost of this change is we lose a little bit of convenience in the
404 case where we were fetching and presenting the list of valid
indexes/collections. But that small bit of convenience doesn't seem
worth a radically more complex approach to error handling.
- We no longer need to test exception handling in most method
implementations
- It unlocks the ability for some really cool debug options. See #136.

#### Summary of files changed:
- **Error handling logic** in `src/errors/handling.ts`: 
    - Refactor from nested callbacks into a flat error handler function.
- Continue to delegate to httpErrorMapper, but check that the error
actually is a ResponseError before trying to access the `text()`
property.
- For other errors, wrap the underlying errors and throw
`PineconeConnectionError`
- **Error classes** in `src/errors`: 
- `PineconeConnectionError` and `BasePineconeError` modified to allow
reference to underlying errors to be captured.
- Revised error message that explains more reasons why
`PineconeConnectionError` might occur.
- **Middleware**: These get invoked at different points in the lifecycle
of every request.
    - Defined middleware using `handleApiError` 
- Wired up new middleware for error handling in `src/pinecone.ts` and
`src/data/index.ts`. Now we can be confident we are handling errors
consistently across every method without the need for a lot of
repetitive unit testing to verify wiring on each client method.
- **New integration tests**. 
- Added a more robust suite of integration tests to exercise the error
handling in a variety of real scenarios.
    - Deleted method-specific error handling that is no longer needed
    -  Deleted method-level unit testing of error handling
 

## Type of Change

- [x] Bug fix (non-breaking change which fixes an issue)

## Test Plan

Describe specific steps for validating this change.
@jhamon
Copy link
Collaborator

jhamon commented Oct 10, 2023

I just shipped a v1.1.1 release that I don't think will completely fix the problem but will hopefully result in a more useful error message than Cannot read properties of undefined (reading 'text')

If anyone experiencing this problem can share full details about your environment and dependencies (ideally a minimum reproduction case), that would be very helpful. So far I have not been able to reproduce the issue in our sample projects and integration tests.

Specifically it would help me to know:

  • Where are you running this when you see the error? On local, in Vercel edge runtime, or elsewhere (please specify: AWS lambda, etc)?
  • What dependencies does your project have in project.json?

I appreciate everyone's patience as we work through this.

@RickRyan26
Copy link

RickRyan26 commented Nov 20, 2023

@jhamon Hello again,

I'm using Sveltekit so the problem is very likely NodeJS/Vercel related, not NextJS.

Pre v1.1.1 the titled error is thrown when using Vercel Serverless or Edge; both fail.

Once updated to v1.1.1 the new error is as follows:

error code: 525 Status: 525. (Probably a failed SSL Handshake)

error:  PineconeUnmappedHttpError: An unexpected error occured while calling the https://rs-6d43a5d.svc.northamerica-northeast1-gcp.pinecone.io/query endpoint.  error code: 525 Status: 525.
    at (../../../../node_modules/.pnpm/@pinecone-database+pinecone@1.1.1/node_modules/@pinecone-database/pinecone/src/errors/http.ts:162:12)
    at (../../../../node_modules/.pnpm/@pinecone-database+pinecone@1.1.1/node_modules/@pinecone-database/pinecone/src/errors/handling.ts:22:30)
    at (../../../../node_modules/.pnpm/@pinecone-database+pinecone@1.1.1/node_modules/@pinecone-database/pinecone/src/errors/utils.ts:4:27)
    at (../../../../node_modules/.pnpm/@pinecone-database+pinecone@1.1.1/node_modules/@pinecone-database/pinecone/src/errors/utils.ts:4:27)
    at (../../../../node_modules/.pnpm/@pinecone-database+pinecone@1.1.1/node_modules/@pinecone-database/pinecone/src/errors/utils.ts:4:27) {
  name: 'PineconeUnmappedHttpError',
  cause: undefined
}

Thank you and let me know if theres anything I can test to help resolve this.

@RickRyan26
Copy link

@jhamon Any update/thoughts on this? Not being able to use Vercel is a critical hit

@RickRyan26
Copy link

RickRyan26 commented Nov 30, 2023

Can we please have a progress update here? I need to know if there's anything I can do to help or if Pinecone is abandoning vercel. If so I'll need to sprint to pgvector to meet my teams release date because currently every single npm version of pincone is erroring out. Thank you

@anawishnoff anawishnoff added the status:needs-triage An issue that needs to be triaged by the Pinecone team label Aug 6, 2024
@anawishnoff
Copy link
Contributor

As this issue is almost a year old, I'll go ahead and close this as stale. However please feel free to reopen or file a new issue if you're still having trouble here - thank you!

@anawishnoff anawishnoff removed the status:needs-triage An issue that needs to be triaged by the Pinecone team label Aug 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

9 participants