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

PineconeStore: NullCannot read properties of undefined (reading 'toString') #6919

Closed
5 tasks done
sam-trost opened this issue Oct 2, 2024 · 1 comment · Fixed by #6920
Closed
5 tasks done

PineconeStore: NullCannot read properties of undefined (reading 'toString') #6919

sam-trost opened this issue Oct 2, 2024 · 1 comment · Fixed by #6920
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature

Comments

@sam-trost
Copy link
Contributor

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain.js documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain.js rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

/* eslint-disable @typescript-eslint/no-explicit-any */
import { expect, jest, test } from "@jest/globals";
import { FakeEmbeddings } from "@langchain/core/utils/testing";
import { PineconeStore } from "../vectorstores.js";

describe("PineconeStore with null pageContent", () => {
  it("should handle null pageContent correctly in _formatMatches", async () => {
    const mockQueryResponse = {
      matches: [
        {
          id: "1",
          score: 0.9,
          metadata: { textKey: null, otherKey: "value" },
        },
      ],
    };

    const client = {
      namespace: jest.fn<any>().mockReturnValue({
        query: jest.fn<any>().mockResolvedValue(mockQueryResponse),
      }),
    };
    const embeddings = new FakeEmbeddings();

    const store = new PineconeStore(embeddings, {
      pineconeIndex: client as any,
    });

    const results = await store.similaritySearchVectorWithScore([], 0);
    expect(results[0][0].pageContent).toEqual("");
  });
});

Error Message and Stack Trace (if applicable)

TypeError: Cannot read properties of undefined (reading 'toString')

  426 |           new Document({
  427 |             id,
> 428 |             pageContent: pageContent.toString(),
      |                                      ^
  429 |             metadata,
  430 |           }),
  431 |           score,

  at PineconeStore.toString [as _formatMatches] (src/vectorstores.ts:428:38)
  at PineconeStore._formatMatches [as similaritySearchVectorWithScore] (src/vectorstores.ts:453:26)
  at Object.<anonymous> (src/tests/vectorstores.test.ts:145:21)

Description

The current version of the pinecone vector store assumes a property named pageContent will be set on each record. This was not previously required when ingesting content.

System Info

└─ langchain@workspace:langchain
├─ Instances: 1
├─ Version: 0.3.2

└─ Dependencies
├─ @faker-js/faker@npm:^7.6.0 → npm:7.6.0
├─ @jest/globals@npm:^29.5.0 → npm:29.5.0
├─ @langchain/core@workspace:* → workspace:langchain-core
├─ @langchain/scripts@>=0.1.0 <0.2.0 → workspace:libs/langchain-scripts
├─ @tsconfig/recommended@npm:^1.0.2 → npm:1.0.2
├─ @types/handlebars@npm:^4.1.0 → npm:4.1.0
├─ @types/html-to-text@npm:^9 → npm:9.0.0
├─ @types/js-yaml@npm:^4 → npm:4.0.5
├─ @types/jsdom@npm:^21.1.1 → npm:21.1.1
├─ @types/uuid@npm:^9 → npm:9.0.1
├─ @types/ws@npm:^8 → npm:8.5.8
├─ axios@npm:^0.26.0 → npm:0.26.1
├─ cheerio@npm:1.0.0-rc.12 → npm:1.0.0-rc.12
├─ dotenv@npm:^16.0.3 → npm:16.0.3
├─ dpdm@patch:dpdm@npm%3A3.12.0#./.yarn/patches/dpdm-npm-3.12.0-0dfdd8e3b8.patch::locator=langchainjs%40workspace%3A. → patch:dpdm@npm%3A3.12.0#./.yarn/patches/dpdm-npm-3.12.0-0dfdd8e3b8.patch::version=3.12.0&hash=2406a8&locator=langchainjs%40workspace%3A.
├─ eslint-plugin-no-instanceof@npm:^1.0.1 → npm:1.0.1
├─ eslint@npm:^8.33.0 → npm:8.35.0
├─ handlebars@npm:^4.7.8 → npm:4.7.8
├─ jest-environment-node@npm:^29.6.4 → npm:29.6.4
├─ js-tiktoken@npm:^1.0.12 → npm:1.0.12
├─ js-yaml@npm:^4.1.0 → npm:4.1.0
├─ jsonpointer@npm:^5.0.1 → npm:5.0.1
├─ openai@npm:^4.41.1 → npm:4.42.0
├─ openapi-types@npm:^12.1.3 → npm:12.1.3
├─ p-retry@npm:4 → npm:4.6.2
├─ peggy@npm:^3.0.2 → npm:3.0.2
├─ prettier@npm:^2.8.3 → npm:2.8.4
├─ release-it@npm:^17.6.0 → npm:17.6.0
├─ rimraf@npm:^5.0.1 → npm:5.0.1
├─ rollup@npm:^3.19.1 → npm:3.19.1
├─ typescript@patch:typescript@~5.1.6#~builtin<compat/typescript> → patch:typescript@npm%3A5.1.6#~builtin<compat/typescript>::version=5.1.6&hash=77c9e2
├─ uuid@npm:^10.0.0 → npm:10.0.0
├─ wikipedia@npm:^2.1.2 → npm:2.1.2
├─ yaml@npm:^2.2.1 → npm:2.2.1
├─ zod@npm:3.23.8 → npm:3.23.8
├─ @langchain/anthropic@workspace:* → workspace:libs/langchain-anthropic [773c0]
├─ @langchain/aws@workspace:* → workspace:libs/langchain-aws [773c0]
├─ @langchain/cohere@workspace:* → workspace:libs/langchain-cohere [773c0]
├─ @langchain/google-genai@workspace:* → workspace:libs/langchain-google-genai [773c0]
├─ @langchain/google-vertexai@workspace:* → workspace:libs/langchain-google-vertexai [773c0]
├─ @langchain/groq@workspace:* → workspace:libs/langchain-groq [773c0]
├─ @langchain/mistralai@workspace:* → workspace:libs/langchain-mistralai [773c0]
├─ @langchain/ollama@workspace:* → workspace:libs/langchain-ollama [773c0]
├─ @langchain/openai@workspace:* → workspace:libs/langchain-openai [773c0]
├─ @langchain/textsplitters@workspace:* → workspace:libs/langchain-textsplitters [773c0]
├─ @swc/core@npm:^1.3.90 → npm:1.3.90 [6dbfd]
├─ @swc/jest@npm:^0.2.29 → npm:0.2.29 [6dbfd]
├─ @typescript-eslint/eslint-plugin@npm:^5.58.0 → npm:5.58.0 [6d556]
├─ @typescript-eslint/parser@npm:^5.58.0 → npm:5.58.0 [6d556]
├─ eslint-config-airbnb-base@npm:^15.0.0 → npm:15.0.0 [6d556]
├─ eslint-config-prettier@npm:^8.6.0 → npm:8.7.0 [6dbfd]
├─ eslint-plugin-import@npm:^2.27.5 → npm:2.27.5 [6d556]
├─ eslint-plugin-jest@npm:^27.6.0 → npm:27.6.0 [6d556]
├─ eslint-plugin-prettier@npm:^4.2.1 → npm:4.2.1 [6dbfd]
├─ jest@npm:^29.5.0 → npm:29.7.0 [6dbfd]
├─ langsmith@npm:^0.1.56 → npm:0.1.56 [0b9fc]
├─ ts-jest@npm:^29.1.0 → npm:29.1.0 [221f6]
├─ typeorm@npm:^0.3.20 → npm:0.3.20 [3a972]
└─ zod-to-json-schema@npm:^3.22.3 → npm:3.22.3 [221f6]

Copy link

dosubot bot commented Oct 2, 2024

Hey there, @sam-trost! I'm here to help you with any bugs, questions, or contributions. Let's work on solving the issue together.

I found a similar discussion regarding the "TypeError: Cannot read properties of undefined (reading 'toString')" error in the PineconeStore when handling null pageContent. The issue is that the current implementation assumes pageContent is always defined, which can lead to this error if it is null.

To resolve this, you can modify the code in the _formatMatches method to handle null pageContent by providing a default value. Here's a possible solution:

private _formatMatches(
  matches: ScoredPineconeRecord<RecordMetadata>[] = []
): [Document, number][] {
  const documentsWithScores: [Document, number][] = [];

  for (const record of matches) {
    const {
      id,
      score,
      metadata: { [this.textKey]: pageContent, ...metadata } = {
        [this.textKey]: "",
      },
    } = record;

    if (score) {
      documentsWithScores.push([
        new Document({
          id,
          pageContent: pageContent ? pageContent.toString() : "", // Provide a default empty string if pageContent is null
          metadata,
        }),
        score,
      ]);
    }
  }

  return documentsWithScores;
}

This modification ensures that if pageContent is null, an empty string is used instead, preventing the toString method from being called on an undefined value [1].

To continue talking to Dosu, mention @dosu.

@dosubot dosubot bot added the auto:bug Related to a bug, vulnerability, unexpected error with an existing feature label Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto:bug Related to a bug, vulnerability, unexpected error with an existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant