Skip to content
Prev Previous commit
Next Next commit
more tests
  • Loading branch information
RulaKhaled committed Oct 21, 2025
commit 630dfb2ea74ced1599a4b57814d884c0b770abd7
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { ChatAnthropic } from '@langchain/anthropic';
import * as Sentry from '@sentry/node';
import express from 'express';

function startMockAnthropicServer() {
const app = express();
app.use(express.json());

app.post('/v1/messages', (req, res) => {
const model = req.body.model;

// Simulate tool call response
res.json({
id: 'msg_tool_test_123',
type: 'message',
role: 'assistant',
model: model,
content: [
{
type: 'text',
text: 'Let me check the weather for you.',
},
{
type: 'tool_use',
id: 'toolu_01A09q90qw90lq917835lq9',
name: 'get_weather',
input: { location: 'San Francisco, CA' },
},
{
type: 'text',
text: 'The weather looks great!',
},
],
stop_reason: 'tool_use',
stop_sequence: null,
usage: {
input_tokens: 20,
output_tokens: 30,
},
});
});

return new Promise(resolve => {
const server = app.listen(0, () => {
resolve(server);
});
});
}

async function run() {
const server = await startMockAnthropicServer();
const baseUrl = `http://localhost:${server.address().port}`;

await Sentry.startSpan({ op: 'function', name: 'main' }, async () => {
const model = new ChatAnthropic({
model: 'claude-3-5-sonnet-20241022',
temperature: 0.7,
maxTokens: 150,
apiKey: 'mock-api-key',
clientOptions: {
baseURL: baseUrl,
},
});

await model.invoke('What is the weather in San Francisco?', {
tools: [
{
name: 'get_weather',
description: 'Get the current weather in a given location',
input_schema: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'The city and state, e.g. San Francisco, CA',
},
},
required: ['location'],
},
},
],
});
});

await Sentry.flush(2000);

server.close();
}

run();

Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ async function run() {
}
});

await Sentry.flush(2000);

server.close();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,38 @@ describe('LangChain integration', () => {
.completed();
});
});

const EXPECTED_TRANSACTION_TOOL_CALLS = {
transaction: 'main',
spans: expect.arrayContaining([
expect.objectContaining({
data: expect.objectContaining({
'gen_ai.operation.name': 'chat',
'sentry.op': 'gen_ai.chat',
'sentry.origin': 'auto.ai.langchain',
'gen_ai.system': 'anthropic',
'gen_ai.request.model': 'claude-3-5-sonnet-20241022',
'gen_ai.request.temperature': 0.7,
'gen_ai.request.max_tokens': 150,
'gen_ai.usage.input_tokens': 20,
'gen_ai.usage.output_tokens': 30,
'gen_ai.usage.total_tokens': 50,
'gen_ai.response.id': expect.any(String),
'gen_ai.response.model': expect.any(String),
'gen_ai.response.stop_reason': 'tool_use',
'gen_ai.response.tool_calls': expect.any(String),
}),
description: 'chat claude-3-5-sonnet-20241022',
op: 'gen_ai.chat',
origin: 'auto.ai.langchain',
status: 'ok',
}),
]),
};

createEsmAndCjsTests(__dirname, 'scenario-tools.mjs', 'instrument.mjs', (createRunner, test) => {
test('creates langchain spans with tool calls', async () => {
await createRunner().ignore('event').expect({ transaction: EXPECTED_TRANSACTION_TOOL_CALLS }).start().completed();
});
});
});
5 changes: 3 additions & 2 deletions packages/core/src/utils/langchain/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,9 @@ export function extractLlmResponseAttributes(
setIfDefined(attrs, GEN_AI_RESPONSE_FINISH_REASONS_ATTRIBUTE, asString(finishReasons));
}

// Tool calls metadata (names, IDs) are not PII, so capture them regardless of recordOutputs
addToolCallsAttributes(response.generations as LangChainMessage[][], attrs);

if (recordOutputs) {
const texts = response.generations
.flat()
Expand All @@ -397,8 +400,6 @@ export function extractLlmResponseAttributes(
if (texts.length > 0) {
setIfDefined(attrs, GEN_AI_RESPONSE_TEXT_ATTRIBUTE, asString(texts));
}

addToolCallsAttributes(response.generations as LangChainMessage[][], attrs);
}
}

Expand Down