dd-trace-js icon indicating copy to clipboard operation
dd-trace-js copied to clipboard

feat(langchain, llmobs): expand langchain support for tools and vectorstores

Open sabrenner opened this issue 6 months ago • 4 comments

What does this PR do?

Declares instrumentation for langchain's StructuredTool (from what I could tell is the base class for all langchain tools in Node.js) and VectorStore (the base class for all vectorstore retrievers).

For APM, we do not add I/O tags, and instead just provide basic tracing.

For LLMObs, we submit tool and retrieval spans respectively for tool and vectorstore operations, annotated with the appropriate inputs, outputs, and metadata.

Example Tool Call

const { tool } = require("@langchain/core/tools");
const { z } = require("zod");

const add = tool(
  ({ a, b }) => a + b,
  {
    name: 'add',
    description: 'A tool that adds two numbers',
    schema: z.object({
      a: z.number(),
      b: z.number(),
    })
  }
)

async function main () {
  const result = await add.invoke({ a: 1, b: 2 })
  console.log(result)
}

main()
Screenshot 2025-05-21 at 2 49 56 PM

Example VectorStore Retrieval

const { OpenAIEmbeddings } = require('@langchain/openai')
const { MemoryVectorStore } = require('langchain/vectorstores/memory')

const embeddings = new OpenAIEmbeddings()
const vectorstore = new MemoryVectorStore(embeddings)

async function main () {
  const document1 = {
    pageContent: "The powerhouse of the cell is the mitochondria",
    metadata: { source: "https://example.com" },
    id: '1'
  };
  
  const document2 = {
    pageContent: "Buildings are made out of brick",
    metadata: { source: "https://example.com" },
    id: '2'
  };
  
  const document3 = {
    pageContent: "Mitochondria are made out of lipids",
    metadata: { source: "https://example.com" },
    id: '3'
  };
  
  const documents = [document1, document2, document3];
  
  await vectorstore.addDocuments(documents);

  const filter = (doc) => doc.metadata.source === "https://example.com";
  const similaritySearchWithScoreResults = await vectorstore.similaritySearchWithScore("biology", 2, filter);

  for (const [doc, score] of similaritySearchWithScoreResults) {
    console.log(score, doc)
  }
}

main()
Screenshot 2025-05-21 at 4 59 25 PM

Motivation

MLOB-2865

sabrenner avatar May 21 '25 17:05 sabrenner

Overall package size

Self size: 9.68 MB Deduped: 109.4 MB No deduping: 109.78 MB

Dependency sizes | name | version | self size | total size | |------|---------|-----------|------------| | @datadog/libdatadog | 0.7.0 | 35.02 MB | 35.02 MB | | @datadog/native-appsec | 10.0.0 | 20.3 MB | 20.31 MB | | @datadog/native-iast-taint-tracking | 4.0.0 | 11.72 MB | 11.73 MB | | @datadog/pprof | 5.9.0 | 9.77 MB | 10.14 MB | | @opentelemetry/core | 1.30.1 | 908.66 kB | 7.16 MB | | protobufjs | 7.5.3 | 2.95 MB | 5.6 MB | | @datadog/wasm-js-rewriter | 4.0.1 | 2.85 MB | 3.58 MB | | @datadog/native-metrics | 3.1.1 | 1.02 MB | 1.43 MB | | @opentelemetry/api | 1.8.0 | 1.21 MB | 1.21 MB | | jsonpath-plus | 10.3.0 | 617.18 kB | 1.08 MB | | import-in-the-middle | 1.14.2 | 122.36 kB | 850.93 kB | | lru-cache | 10.4.3 | 804.3 kB | 804.3 kB | | source-map | 0.7.4 | 226 kB | 226 kB | | opentracing | 0.14.7 | 194.81 kB | 194.81 kB | | limiter | 3.0.0 | 157.92 kB | 157.92 kB | | pprof-format | 2.1.0 | 111.69 kB | 111.69 kB | | @datadog/sketches-js | 2.1.1 | 109.9 kB | 109.9 kB | | lodash.sortby | 4.7.0 | 75.76 kB | 75.76 kB | | ignore | 7.0.5 | 63.38 kB | 63.38 kB | | istanbul-lib-coverage | 3.2.2 | 34.37 kB | 34.37 kB | | rfdc | 1.4.1 | 27.15 kB | 27.15 kB | | @isaacs/ttlcache | 1.4.1 | 25.2 kB | 25.2 kB | | dc-polyfill | 0.1.9 | 25.11 kB | 25.11 kB | | tlhunter-sorted-set | 0.1.0 | 24.94 kB | 24.94 kB | | shell-quote | 1.8.3 | 23.74 kB | 23.74 kB | | retry | 0.13.1 | 18.85 kB | 18.85 kB | | semifies | 1.0.0 | 15.84 kB | 15.84 kB | | jest-docblock | 29.7.0 | 8.99 kB | 12.76 kB | | crypto-randomuuid | 1.0.0 | 11.18 kB | 11.18 kB | | ttl-set | 1.0.0 | 4.61 kB | 9.69 kB | | mutexify | 1.4.0 | 5.71 kB | 8.74 kB | | path-to-regexp | 0.1.12 | 6.6 kB | 6.6 kB | | koalas | 1.0.2 | 6.47 kB | 6.47 kB | | module-details-from-path | 1.0.4 | 3.96 kB | 3.96 kB |

🤖 This report was automatically generated by heaviest-objects-in-the-universe

github-actions[bot] avatar May 21 '25 17:05 github-actions[bot]

Codecov Report

Attention: Patch coverage is 85.00000% with 6 lines in your changes missing coverage. Please review.

Project coverage is 82.85%. Comparing base (9e3721c) to head (c79739d).

Files with missing lines Patch % Lines
...race/src/llmobs/plugins/langchain/handlers/tool.js 50.00% 2 Missing :warning:
...c/llmobs/plugins/langchain/handlers/vectorstore.js 86.66% 2 Missing :warning:
...ges/dd-trace/src/llmobs/plugins/langchain/index.js 90.00% 2 Missing :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5760      +/-   ##
==========================================
+ Coverage   82.82%   82.85%   +0.03%     
==========================================
  Files         472      474       +2     
  Lines       19313    19351      +38     
==========================================
+ Hits        15996    16034      +38     
  Misses       3317     3317              

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

codecov[bot] avatar May 21 '25 17:05 codecov[bot]

Datadog Report

Branch report: sabrenner/langchain-expanded-support Commit report: f23b9d0 Test service: dd-trace-js-integration-tests

:white_check_mark: 0 Failed, 1258 Passed, 0 Skipped, 21m 43.09s Total Time

Benchmarks

Benchmark execution time: 2025-07-14 13:38:00

Comparing candidate commit c79739d2ad5ba16156c5c8a81c4a30ac33664ec6 in PR branch sabrenner/langchain-expanded-support with baseline commit 9e3721cd7182717d843af6bee092683dcaa22040 in branch master.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 1264 metrics, 59 unstable metrics.

pr-commenter[bot] avatar May 21 '25 17:05 pr-commenter[bot]