langchain-ai / langsmith-sdk

LangSmith Client SDK Implementations
https://smith.langchain.com/
MIT License
387 stars 73 forks source link

Issue: getCurrentRunTree() Method Fails: "Could not get the current run tree" #698

Closed TechWithTy closed 4 weeks ago

TechWithTy commented 4 months ago

Issue you'd like to raise.

Issue Description Description I am encountering an issue with the getCurrentRunTree method in the langsmith library. The error message indicates that the method cannot get the current run tree and suggests that the method is not being called within a traceable function. Despite following the documentation and ensuring that my function calls are within the traceable context, the error persists.

Error Message less


Error: Could not get the current run tree.
Please make sure you are calling this method within a traceable function.
    at getCurrentRunTree (C:\Users\Dell\Downloads\cyberOni\CyberOni-Bun-Server-Ai\node_modules\langsmith\dist\traceable.js:326:15)
    at C:\Users\Dell\Downloads\cyberOni\CyberOni-Bun-Server-Ai\src\lib\langchain\langsmith.ts:10:17
    at C:\Users\Dell\Downloads\cyberOni\CyberOni-Bun-Server-Ai\node_modules\langsmith\dist\traceable.js:139:31
    at run (node:async_hooks:70:11)
    at C:\Users\Dell\Downloads\cyberOni\CyberOni-Bun-Server-Ai\src\lib\langchain\langsmith.ts:48:26
    at run (C:\Users\Dell\Downloads\cyberOni\CyberOni-Bun-Server-Ai\src\lib\langchain\langsmith.ts:46:19)
    at C:\Users\Dell\Downloads\cyberOni\CyberOni-Bun-Server-Ai\src\lib\langchain\langsmith.ts:58:1

Steps to Reproduce Set up a project with the langsmith library. Implement the following code snippet to trace function calls: javascript


import { Client } from "langsmith";
import { traceable, getCurrentRunTree } from "langsmith/traceable";
import { wrapOpenAI } from "langsmith/wrappers";
import { chatOpenAi } from "./openai";
import { GptPrompt } from "@prisma/client";
import { IterableReadableStream } from "@langchain/core/utils/stream";
import { AIMessageChunk } from "@langchain/core/messages";

const formatPrompt = traceable(
  (subject: string) => {
    const run = getCurrentRunTree();
    console.log("formatPrompt Run ID", run?.id);
    console.log("formatPrompt Trace ID", run?.trace_id);
    console.log("formatPrompt Parent Run ID", run?.parent_run?.id);
    return [
      {
        role: "system",
        content: "You are a helpful assistant.",
      },
      {
        role: "user",
        content: `What's a good name for a store that sells ${subject}?`,
      },
    ];
  },
  { name: "formatPrompt" }
);

const traceableCallOpenAI = traceable(
  async (prompt) => {
    const run = getCurrentRunTree();
    console.log("traceableCallOpenAI Run ID", run?.id);
    const response = await chatOpenAi(prompt, {}, {}, "New_run_id", true, process.env.OPENAI_API_KEY);
    console.log(response);
    return response;
  },
  {
    run_type: "llm",
    name: "OpenAI Call Traceable",
  }
);

const run = traceable(
  async () => {
    const prompt = formatPrompt("reply OK");
    const response = await traceableCallOpenAI(prompt);
    console.log(response);
  },
  { name: "run" }
);

run();

Run the script. Expected Behavior The getCurrentRunTree method should return the current run tree without errors when called within a traceable function.

Actual Behavior The getCurrentRunTree method throws an error stating that it could not get the current run tree.

Environment OS: Windows 10 Node.js Version: 20.~ langsmith Version: x.y.z (Please specify the exact version you're using)

Suggestion:

No response

dqbd commented 3 months ago

Hello @TechWithTy! Sorry for the delay. We're only supporting Node.JS 18+, please let us know if the issue is solved by upgrading NodeJS version from 14 to 18.

TechWithTy commented 3 months ago

We were actually using node 20v Somehting sorry for the typo

guidorietbroek commented 3 months ago

I have the same issue and I am on v18.

TechWithTy commented 3 months ago

Can we reopen this @dqbd

guidorietbroek commented 3 months ago

I noticed that it doesn't generate this error inside every function wrapped inside the traceable() function. If I have more debugging information I'll let you know. Both functions had at least a chain consisting of a prompt, model, parser.

TechWithTy commented 3 weeks ago

@guidorietbroek Were you able to fix the issue?

guidorietbroek commented 2 weeks ago

No sorry.