langchain-ai / langchainjs

🦜🔗 Build context-aware reasoning applications 🦜🔗
https://js.langchain.com/docs/
MIT License
11.81k stars 1.98k forks source link

TypeError: Converting circular structure to JSON when enabling LangSmith #5198

Open sinedied opened 2 months ago

sinedied commented 2 months ago

Checked other resources

Example Code

The issue occurs when enabling Langsmith on this sample, in particular the Azure path: https://github.com/Azure-Samples/serverless-chat-langchainjs

The issue seems to come from the AzureCosmosDBVectorStore. I'm trying to investigate what could be the cause of the circular structure, do you have any hint on how to debug which object is the root cause?

Error Message and Stack Trace (if applicable)

[api] [2024-04-23T15:11:38.138Z] TypeError: Converting circular structure to JSON
[api] [2024-04-23T15:11:38.138Z]     --> starting at object with constructor 'Socket'
[api] [2024-04-23T15:11:38.138Z]     |     property 'parser' -> object with constructor 'HTTPParser'
[api] [2024-04-23T15:11:38.138Z]     --- property 'socket' closes the circle
[api] [2024-04-23T15:11:38.138Z]     at JSON.stringify (<anonymous>)
[api] [2024-04-23T15:11:38.138Z]     at Client.batchIngestRuns (/Users/sinedied/projects/serverless-chat-langchainjs/node_modules/langsmith/dist/client.cjs:637:51)
[api] [2024-04-23T15:11:38.138Z]     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
[api] [2024-04-23T15:11:38.139Z]     at async Client.drainAutoBatchQueue (/Users/sinedied/projects/serverless-chat-langchainjs/node_modules/langsmith/dist/client.cjs:477:17)

Description

Trying to enable Langsmith, but the app crashes when trying to send the report.

System Info

npm ls langchain @langchain/comm unity serverless-chat-langchainjs@1.0.0 /Users/sinedied/projects/serverless-chat-langchainjs └─┬ api@1.0.0 -> ./packages/api ├── @langchain/community@0.0.51 -> ./packages/api └─┬ langchain@0.1.35 └── @langchain/community@0.0.51 deduped -> ./packages/api

platform mac, node v20.11.0, npm 10.2.4

dosubot[bot] commented 2 months ago

To address the "TypeError: Converting circular structure to JSON" issue you're encountering with LangSmith, particularly in the context of AzureCosmosDBVectorStore, the problem likely stems from a circular reference within the objects being serialized. Here's a focused approach to tackle this:

  1. Identify and Debug Circular References: Utilize console.log() or Node.js's util.inspect() to inspect objects just before serialization. This can help pinpoint where the circular reference occurs.

  2. Custom Replacer for JSON.stringify(): Implement a custom replacer function to handle circular references during JSON serialization. Here's a concise example:

    function getCircularReplacer() {
     const seen = new WeakSet();
     return (key, value) => {
       if (typeof value === "object" && value !== null) {
         if (seen.has(value)) {
           return;
         }
         seen.add(value);
       }
       return value;
     };
    }
    JSON.stringify(obj, getCircularReplacer());
  3. Apply and Test: After applying the above solution, thoroughly test to ensure the issue is resolved without side effects.

The error trace you provided points to Client.batchIngestRuns, suggesting the issue might not be directly within AzureCosmosDBVectorStore but possibly in the objects it handles or the method itself. Apply the debugging steps to this area as well.

If the problem persists, providing more details about the operations leading to the error could help in offering a more specific solution.

#### Details **Sources** - [libs/langchain-community/src/vectorstores/azure_cosmosdb.ts](https://github.com/langchain-ai/langchainjs/blob/main/libs/langchain-community/src/vectorstores/azure_cosmosdb.ts) - [langchain-core/src/utils/@cfworker/json-schema/src/dereference.ts](https://github.com/langchain-ai/langchainjs/blob/main/langchain-core/src/utils/@cfworker/json-schema/src/dereference.ts) - [examples/src/indexes/vector_stores/azure_cosmosdb/azure_cosmosdb.ts](https://github.com/langchain-ai/langchainjs/blob/main/examples/src/indexes/vector_stores/azure_cosmosdb/azure_cosmosdb.ts) - [langchain-core/src/tracers/console.ts](https://github.com/langchain-ai/langchainjs/blob/main/langchain-core/src/tracers/console.ts) - [langchain-core/src/callbacks/manager.ts](https://github.com/langchain-ai/langchainjs/blob/main/langchain-core/src/callbacks/manager.ts) - [langchain/src/smith/runner_utils.ts](https://github.com/langchain-ai/langchainjs/blob/main/langchain/src/smith/runner_utils.ts)
**About Dosu** This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

sinedied commented 2 months ago

Ok, removing entirely AzureCosmosDBVectorStore from the chain did not solve the issue. I still get that error, but it cannot be reproduced all the time. Not sure how I should go to debug this or help debugging this?

Kniggishood commented 1 month ago

Heyhey, can you provide the code sample for this? What does your wrappedFunc look like?

sinedied commented 1 month ago

Yes, I could reproduce the error on this sample/branch: https://github.com/Azure-Samples/serverless-chat-langchainjs/tree/mongodb-vcore

I'm currently off till next week, I'll try to create a simpler repo for reproducing the issue.