Skip to content

Commit bfcf686

Browse files
committed
Should not rely on langchain/node_modules
1 parent efabe85 commit bfcf686

File tree

10 files changed

+69
-48
lines changed

10 files changed

+69
-48
lines changed

test/versioned/langchain-aws/runnables-streaming.test.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 New Relic Corporation. All rights reserved.
2+
* Copyright 2025 New Relic Corporation. All rights reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

@@ -35,10 +35,16 @@ async function beforeEach({ enabled, ctx }) {
3535
ctx.nr.server = server
3636
ctx.nr.agent = helper.instrumentMockedAgent(config)
3737
ctx.nr.agent.config.ai_monitoring.streaming.enabled = enabled
38+
3839
const { ChatPromptTemplate } = require('@langchain/core/prompts')
39-
const { StringOutputParser } = require('@langchain/core/output_parsers')
40+
const { StringOutputParser, CommaSeparatedListOutputParser } = require('@langchain/core/output_parsers')
41+
const { BaseCallbackHandler } = require('@langchain/core/callbacks/base')
4042
const { ChatBedrockConverse } = require('@langchain/aws')
4143
const { BedrockRuntimeClient } = require('@aws-sdk/client-bedrock-runtime')
44+
ctx.nr.ChatPromptTemplate = ChatPromptTemplate
45+
ctx.nr.CommaSeparatedListOutputParser = CommaSeparatedListOutputParser
46+
ctx.nr.BaseCallbackHandler = BaseCallbackHandler
47+
ctx.nr.langchainCoreVersion = require('@langchain/core/package.json').version
4248

4349
// Create the BedrockRuntimeClient with our mock endpoint
4450
const bedrockClient = new BedrockRuntimeClient({

test/versioned/langchain-aws/runnables.test.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 New Relic Corporation. All rights reserved.
2+
* Copyright 2025 New Relic Corporation. All rights reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

@@ -25,10 +25,16 @@ test.beforeEach(async (ctx) => {
2525
const { server, baseUrl } = await createAiResponseServer()
2626
ctx.nr.server = server
2727
ctx.nr.agent = helper.instrumentMockedAgent(config)
28+
2829
const { ChatPromptTemplate } = require('@langchain/core/prompts')
29-
const { StringOutputParser } = require('@langchain/core/output_parsers')
30+
const { StringOutputParser, CommaSeparatedListOutputParser } = require('@langchain/core/output_parsers')
31+
const { BaseCallbackHandler } = require('@langchain/core/callbacks/base')
3032
const { ChatBedrockConverse } = require('@langchain/aws')
3133
const { BedrockRuntimeClient } = require('@aws-sdk/client-bedrock-runtime')
34+
ctx.nr.ChatPromptTemplate = ChatPromptTemplate
35+
ctx.nr.CommaSeparatedListOutputParser = CommaSeparatedListOutputParser
36+
ctx.nr.BaseCallbackHandler = BaseCallbackHandler
37+
ctx.nr.langchainCoreVersion = require('@langchain/core/package.json').version
3238

3339
// Create the BedrockRuntimeClient with our mock endpoint
3440
const bedrockClient = new BedrockRuntimeClient({

test/versioned/langchain-aws/vectorstore.test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 New Relic Corporation. All rights reserved.
2+
* Copyright 2025 New Relic Corporation. All rights reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

@@ -27,14 +27,17 @@ test.beforeEach(async (ctx) => {
2727
const { server, baseUrl } = await createAiResponseServer()
2828
ctx.nr.server = server
2929
ctx.nr.agent = helper.instrumentMockedAgent(config)
30+
3031
const { BedrockEmbeddings } = require('@langchain/aws')
3132
const { BedrockRuntimeClient } = require('@aws-sdk/client-bedrock-runtime')
33+
ctx.nr.langchainCoreVersion = require('@langchain/core/package.json').version
3234

3335
const { Client } = require('@elastic/elasticsearch')
3436
const clientArgs = {
3537
client: new Client({
3638
node: `http://${params.elastic_host}:${params.elastic_port}`
37-
})
39+
}),
40+
indexName: 'test_langchain_aws_vectorstore'
3841
}
3942
const { ElasticVectorSearch } = require('@langchain/community/vectorstores/elasticsearch')
4043

@@ -65,6 +68,7 @@ test.beforeEach(async (ctx) => {
6568
})
6669

6770
test.afterEach(async (ctx) => {
71+
await ctx.nr?.vs?.deleteIfExists()
6872
ctx.nr?.server?.destroy()
6973
helper.unloadAgent(ctx.nr.agent)
7074
// bust the require-cache so it can re-instrument

test/versioned/langchain-openai/runnables-streaming.test.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 New Relic Corporation. All rights reserved.
2+
* Copyright 2025 New Relic Corporation. All rights reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

@@ -34,9 +34,15 @@ async function beforeEach({ enabled, ctx }) {
3434
ctx.nr.server = server
3535
ctx.nr.agent = helper.instrumentMockedAgent(config)
3636
ctx.nr.agent.config.ai_monitoring.streaming.enabled = enabled
37+
3738
const { ChatPromptTemplate } = require('@langchain/core/prompts')
38-
const { StringOutputParser } = require('@langchain/core/output_parsers')
39+
const { StringOutputParser, CommaSeparatedListOutputParser } = require('@langchain/core/output_parsers')
40+
const { BaseCallbackHandler } = require('@langchain/core/callbacks/base')
3941
const { ChatOpenAI } = require('@langchain/openai')
42+
ctx.nr.ChatPromptTemplate = ChatPromptTemplate
43+
ctx.nr.CommaSeparatedListOutputParser = CommaSeparatedListOutputParser
44+
ctx.nr.BaseCallbackHandler = BaseCallbackHandler
45+
ctx.nr.langchainCoreVersion = require('@langchain/core/package.json').version
4046

4147
ctx.nr.prompt = ChatPromptTemplate.fromMessages([['assistant', '{topic} response']])
4248
ctx.nr.model = new ChatOpenAI({

test/versioned/langchain-openai/runnables.test.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 New Relic Corporation. All rights reserved.
2+
* Copyright 2025 New Relic Corporation. All rights reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

@@ -24,9 +24,15 @@ test.beforeEach(async (ctx) => {
2424
const { host, port, server } = await createOpenAIMockServer()
2525
ctx.nr.server = server
2626
ctx.nr.agent = helper.instrumentMockedAgent(config)
27+
2728
const { ChatPromptTemplate } = require('@langchain/core/prompts')
28-
const { StringOutputParser } = require('@langchain/core/output_parsers')
29+
const { StringOutputParser, CommaSeparatedListOutputParser } = require('@langchain/core/output_parsers')
30+
const { BaseCallbackHandler } = require('@langchain/core/callbacks/base')
2931
const { ChatOpenAI } = require('@langchain/openai')
32+
ctx.nr.ChatPromptTemplate = ChatPromptTemplate
33+
ctx.nr.CommaSeparatedListOutputParser = CommaSeparatedListOutputParser
34+
ctx.nr.BaseCallbackHandler = BaseCallbackHandler
35+
ctx.nr.langchainCoreVersion = require('@langchain/core/package.json').version
3036

3137
ctx.nr.prompt = ChatPromptTemplate.fromMessages([['assistant', 'You are a {topic}.']])
3238
ctx.nr.model = new ChatOpenAI({

test/versioned/langchain-openai/vectorstore.test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 New Relic Corporation. All rights reserved.
2+
* Copyright 2025 New Relic Corporation. All rights reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

@@ -26,13 +26,16 @@ test.beforeEach(async (ctx) => {
2626
const { host, port, server } = await createOpenAIMockServer()
2727
ctx.nr.server = server
2828
ctx.nr.agent = helper.instrumentMockedAgent(config)
29+
2930
const { OpenAIEmbeddings } = require('@langchain/openai')
31+
ctx.nr.langchainCoreVersion = require('@langchain/core/package.json').version
3032

3133
const { Client } = require('@elastic/elasticsearch')
3234
const clientArgs = {
3335
client: new Client({
3436
node: `http://${params.elastic_host}:${params.elastic_port}`
35-
})
37+
}),
38+
indexName: 'test_langchain_openai_vectorstore'
3639
}
3740
const { ElasticVectorSearch } = require('@langchain/community/vectorstores/elasticsearch')
3841

@@ -55,6 +58,7 @@ test.beforeEach(async (ctx) => {
5558
})
5659

5760
test.afterEach(async (ctx) => {
61+
await ctx.nr?.vs?.deleteIfExists()
5862
ctx.nr?.server?.close()
5963
helper.unloadAgent(ctx.nr.agent)
6064
// bust the require-cache so it can re-instrument

test/versioned/langchain/runnables-streaming.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 New Relic Corporation. All rights reserved.
2+
* Copyright 2025 New Relic Corporation. All rights reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

@@ -15,7 +15,6 @@ const {
1515
filterLangchainEvents,
1616
filterLangchainEventsByType
1717
} = require('./common')
18-
const { version: pkgVersion } = require('@langchain/core/package.json')
1918
const { DESTINATIONS } = require('../../../lib/config/attribute-filter')
2019
const helper = require('../../lib/agent_helper')
2120

@@ -51,9 +50,8 @@ function runStreamingEnabledTests(config) {
5150

5251
return async (t) => {
5352
await t.test('should log tracking metrics', function(t) {
54-
const { agent } = t.nr
55-
const { version } = require('@langchain/core/package.json')
56-
assertPackageMetrics({ agent, pkg: '@langchain/core', version })
53+
const { agent, langchainCoreVersion } = t.nr
54+
assertPackageMetrics({ agent, pkg: '@langchain/core', version: langchainCoreVersion })
5755
})
5856

5957
await t.test('should create langchain events for every stream call', (t, end) => {
@@ -92,7 +90,7 @@ function runStreamingEnabledTests(config) {
9290
await t.test(
9391
'should increment tracking metric for each langchain chat prompt event',
9492
(t, end) => {
95-
const { agent, prompt, outputParser, model } = t.nr
93+
const { agent, prompt, outputParser, model, langchainCoreVersion } = t.nr
9694

9795
helper.runInTransaction(agent, async (tx) => {
9896
const input = inputData
@@ -104,7 +102,7 @@ function runStreamingEnabledTests(config) {
104102
}
105103

106104
const metrics = agent.metrics.getOrCreateMetric(
107-
`Supportability/Nodejs/ML/Langchain/${pkgVersion}`
105+
`Supportability/Nodejs/ML/Langchain/${langchainCoreVersion}`
108106
)
109107
assert.equal(metrics.callCount > 0, true)
110108

@@ -220,8 +218,7 @@ function runStreamingEnabledTests(config) {
220218
await t.test(
221219
'should create langchain events for every stream call with parser that returns an array as output',
222220
(t, end) => {
223-
const { CommaSeparatedListOutputParser } = require('@langchain/core/output_parsers')
224-
const { agent, prompt, model } = t.nr
221+
const { agent, prompt, model, CommaSeparatedListOutputParser } = t.nr
225222

226223
helper.runInTransaction(agent, async (tx) => {
227224
const parser = new CommaSeparatedListOutputParser()
@@ -273,7 +270,7 @@ function runStreamingEnabledTests(config) {
273270
)
274271

275272
await t.test('should add runId when a callback handler exists', (t, end) => {
276-
const { BaseCallbackHandler } = require('@langchain/core/callbacks/base')
273+
const { BaseCallbackHandler } = t.nr
277274
let runId
278275
const cbHandler = BaseCallbackHandler.fromMethods({
279276
handleChainStart(...args) {
@@ -310,7 +307,7 @@ function runStreamingEnabledTests(config) {
310307
await t.test(
311308
'should create langchain events for every stream call on chat prompt + model + parser with callback',
312309
(t, end) => {
313-
const { BaseCallbackHandler } = require('@langchain/core/callbacks/base')
310+
const { BaseCallbackHandler } = t.nr
314311
const cbHandler = BaseCallbackHandler.fromMethods({
315312
handleChainStart() {}
316313
})
@@ -469,7 +466,7 @@ function runStreamingEnabledTests(config) {
469466
)
470467

471468
await t.test('should create error events from input', (t, end) => {
472-
const { ChatPromptTemplate } = require('@langchain/core/prompts')
469+
const { ChatPromptTemplate } = t.nr
473470
const prompt = ChatPromptTemplate.fromMessages([
474471
['assistant', 'tell me short joke about {topic}']
475472
])
@@ -505,7 +502,7 @@ function runStreamingEnabledTests(config) {
505502
})
506503

507504
await t.test('should create error events when stream fails', (t, end) => {
508-
const { ChatPromptTemplate } = require('@langchain/core/prompts')
505+
const { ChatPromptTemplate } = t.nr
509506
const prompt = ChatPromptTemplate.fromMessages([[errorPromptTemplate[0], errorPromptTemplate[1]]])
510507
const { agent, model, outputParser } = t.nr
511508

@@ -631,7 +628,7 @@ function runStreamingDisabledTest(config) {
631628
await t.test(
632629
'should not create llm events when `ai_monitoring.streaming.enabled` is false',
633630
(t, end) => {
634-
const { agent, prompt, outputParser, model } = t.nr
631+
const { agent, prompt, outputParser, model, langchainCoreVersion } = t.nr
635632

636633
helper.runInTransaction(agent, async (tx) => {
637634
const input = inputData
@@ -651,7 +648,7 @@ function runStreamingDisabledTest(config) {
651648
const events = agent.customEventAggregator.events.toArray()
652649
assert.equal(events.length, 0, 'should not create llm events when streaming is disabled')
653650
const metrics = agent.metrics.getOrCreateMetric(
654-
`Supportability/Nodejs/ML/Langchain/${pkgVersion}`
651+
`Supportability/Nodejs/ML/Langchain/${langchainCoreVersion}`
655652
)
656653
assert.equal(metrics.callCount > 0, true)
657654
const attributes = tx.trace.attributes.get(DESTINATIONS.TRANS_EVENT)

test/versioned/langchain/runnables.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 New Relic Corporation. All rights reserved.
2+
* Copyright 2025 New Relic Corporation. All rights reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

@@ -16,7 +16,6 @@ const {
1616
filterLangchainEvents,
1717
filterLangchainEventsByType
1818
} = require('./common')
19-
const { version: pkgVersion } = require('@langchain/core/package.json')
2019
const { DESTINATIONS } = require('../../../lib/config/attribute-filter')
2120
const helper = require('../../lib/agent_helper')
2221

@@ -43,9 +42,8 @@ function runRunnablesTests(config) {
4342
} = config
4443

4544
test('should log tracking metrics', function(t) {
46-
const { agent } = t.nr
47-
const { version } = require('@langchain/core/package.json')
48-
assertPackageMetrics({ agent, pkg: '@langchain/core', version })
45+
const { agent, langchainCoreVersion } = t.nr
46+
assertPackageMetrics({ agent, pkg: '@langchain/core', version: langchainCoreVersion })
4947
})
5048

5149
test('should create langchain events for every invoke call', (t, end) => {
@@ -74,7 +72,7 @@ function runRunnablesTests(config) {
7472
})
7573

7674
test('should increment tracking metric for each langchain chat prompt event', (t, end) => {
77-
const { agent, prompt, outputParser, model } = t.nr
75+
const { agent, prompt, outputParser, model, langchainCoreVersion } = t.nr
7876

7977
helper.runInTransaction(agent, async (tx) => {
8078
const input = inputData
@@ -84,7 +82,7 @@ function runRunnablesTests(config) {
8482
await chain.invoke(input, options)
8583

8684
const metrics = agent.metrics.getOrCreateMetric(
87-
`Supportability/Nodejs/ML/Langchain/${pkgVersion}`
85+
`Supportability/Nodejs/ML/Langchain/${langchainCoreVersion}`
8886
)
8987
assert.equal(metrics.callCount > 0, true)
9088

@@ -210,8 +208,7 @@ function runRunnablesTests(config) {
210208
})
211209

212210
test('should create langchain events for every invoke call with parser that returns an array as output', (t, end) => {
213-
const { CommaSeparatedListOutputParser } = require('@langchain/core/output_parsers')
214-
const { agent, prompt, model } = t.nr
211+
const { agent, prompt, model, CommaSeparatedListOutputParser } = t.nr
215212

216213
helper.runInTransaction(agent, async (tx) => {
217214
const parser = new CommaSeparatedListOutputParser()
@@ -261,7 +258,7 @@ function runRunnablesTests(config) {
261258
})
262259

263260
test('should add runId when a callback handler exists', (t, end) => {
264-
const { BaseCallbackHandler } = require('@langchain/core/callbacks/base')
261+
const { BaseCallbackHandler } = t.nr
265262
let runId
266263
const cbHandler = BaseCallbackHandler.fromMethods({
267264
handleChainStart(...args) {
@@ -293,7 +290,7 @@ function runRunnablesTests(config) {
293290
})
294291

295292
test('should create langchain events for every invoke call on chat prompt + model + parser with callback', (t, end) => {
296-
const { BaseCallbackHandler } = require('@langchain/core/callbacks/base')
293+
const { BaseCallbackHandler } = t.nr
297294
const cbHandler = BaseCallbackHandler.fromMethods({
298295
handleChainStart() {}
299296
})
@@ -428,9 +425,8 @@ function runRunnablesTests(config) {
428425
})
429426

430427
test('should create error events', (t, end) => {
431-
const { ChatPromptTemplate } = require('@langchain/core/prompts')
428+
const { ChatPromptTemplate, agent, outputParser, model } = t.nr
432429
const prompt = ChatPromptTemplate.fromMessages([[errorPromptTemplate[0], errorPromptTemplate[1]]])
433-
const { agent, outputParser, model } = t.nr
434430

435431
helper.runInTransaction(agent, async (tx) => {
436432
const chain = prompt.pipe(model).pipe(outputParser)

test/versioned/langchain/tools.test.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ const helper = require('../../lib/agent_helper')
1616
const baseUrl = 'http://httpbin.org'
1717
const config = {
1818
ai_monitoring: {
19-
enabled: true,
20-
streaming: {
21-
enabled: true
22-
}
19+
enabled: true
2320
}
2421
}
2522
const { DESTINATIONS } = require('../../../lib/config/attribute-filter')

test/versioned/langchain/vectorstore.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 New Relic Corporation. All rights reserved.
2+
* Copyright 2025 New Relic Corporation. All rights reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

@@ -37,9 +37,8 @@ function runVectorstoreTests(config) {
3737
} = config
3838

3939
test('should log tracking metrics', function(t) {
40-
const { agent } = t.nr
41-
const { version } = require('@langchain/core/package.json')
42-
assertPackageMetrics({ agent, pkg: '@langchain/core', version })
40+
const { agent, langchainCoreVersion } = t.nr
41+
assertPackageMetrics({ agent, pkg: '@langchain/core', version: langchainCoreVersion })
4342
})
4443

4544
test('should create vectorstore events for every similarity search call', (t, end) => {

0 commit comments

Comments
 (0)