langsmith-sdk icon indicating copy to clipboard operation
langsmith-sdk copied to clipboard

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

Open TechWithTy opened this issue 9 months ago • 5 comments

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

TechWithTy avatar May 14 '24 15:05 TechWithTy