Skip to content

Commit 0029220

Browse files
committed
update with better types
1 parent 0ef3d71 commit 0029220

File tree

5 files changed

+241
-98
lines changed

5 files changed

+241
-98
lines changed

packages/core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ export { GOOGLE_GENAI_INTEGRATION_NAME } from './utils/google-genai/constants';
146146
export type { GoogleGenAIResponse } from './utils/google-genai/types';
147147
export { createLangChainCallbackHandler } from './utils/langchain';
148148
export { LANGCHAIN_INTEGRATION_NAME } from './utils/langchain/constants';
149+
export type { LangChainOptions, LangChainIntegration } from './utils/langchain/types';
149150
export type { OpenAiClient, OpenAiOptions, InstrumentedMethod } from './utils/openai/types';
150151
export type {
151152
AnthropicAiClient,
@@ -159,7 +160,6 @@ export type {
159160
GoogleGenAIOptions,
160161
GoogleGenAIIstrumentedMethod,
161162
} from './utils/google-genai/types';
162-
export type { LangChainOptions, LangChainIntegration } from './utils/langchain/types';
163163
export type { FeatureFlag } from './utils/featureFlags';
164164

165165
export {

packages/core/src/utils/langchain/index.ts

Lines changed: 76 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ import { startSpanManual } from '../../tracing/trace';
55
import type { Span, SpanAttributeValue } from '../../types-hoist/span';
66
import { GEN_AI_OPERATION_NAME_ATTRIBUTE, GEN_AI_REQUEST_MODEL_ATTRIBUTE } from '../ai/gen-ai-attributes';
77
import { LANGCHAIN_ORIGIN } from './constants';
8-
import type { LangChainLLMResult, LangChainMessage, LangChainOptions, LangChainSerializedLLM } from './types';
8+
import type {
9+
LangChainCallbackHandler,
10+
LangChainLLMResult,
11+
LangChainMessage,
12+
LangChainOptions,
13+
LangChainSerialized,
14+
} from './types';
915
import {
1016
extractChatModelRequestAttributes,
1117
extractLLMRequestAttributes,
@@ -19,42 +25,7 @@ import {
1925
*
2026
* This is a stateful handler that tracks spans across multiple LangChain executions.
2127
*/
22-
export function createLangChainCallbackHandler(options: LangChainOptions = {}): {
23-
handleLLMStart?: (
24-
llm: LangChainSerializedLLM,
25-
prompts: string[],
26-
runId: string,
27-
parentRunId?: string,
28-
extraParams?: Record<string, unknown>,
29-
tags?: string[] | Record<string, unknown>,
30-
metadata?: Record<string, unknown>,
31-
runName?: string | Record<string, unknown>,
32-
) => void;
33-
handleChatModelStart?: (
34-
llm: LangChainSerializedLLM,
35-
messages: LangChainMessage[][],
36-
runId: string,
37-
parentRunId?: string,
38-
extraParams?: Record<string, unknown>,
39-
tags?: string[] | Record<string, unknown>,
40-
metadata?: Record<string, unknown>,
41-
runName?: string | Record<string, unknown>,
42-
) => void;
43-
handleLLMNewToken?: (token: string, runId: string) => void;
44-
handleLLMEnd?: (output: LangChainLLMResult, runId: string) => void;
45-
handleLLMError?: (error: Error, runId: string) => void;
46-
handleChainStart?: (
47-
chain: { name?: string },
48-
inputs: Record<string, unknown>,
49-
runId: string,
50-
parentRunId?: string,
51-
) => void;
52-
handleChainEnd?: (outputs: Record<string, unknown>, runId: string) => void;
53-
handleChainError?: (error: Error, runId: string) => void;
54-
handleToolStart?: (tool: { name?: string }, input: string, runId: string, parentRunId?: string) => void;
55-
handleToolEnd?: (output: string, runId: string) => void;
56-
handleToolError?: (error: Error, runId: string) => void;
57-
} {
28+
export function createLangChainCallbackHandler(options: LangChainOptions = {}): LangChainCallbackHandler {
5829
const recordInputs = options.recordInputs ?? false;
5930
const recordOutputs = options.recordOutputs ?? false;
6031

@@ -76,20 +47,46 @@ export function createLangChainCallbackHandler(options: LangChainOptions = {}):
7647
* Handler for LLM Start
7748
* This handler will be called by LangChain's callback handler when an LLM event is detected.
7849
*/
79-
const handler = {
50+
const handler: LangChainCallbackHandler = {
51+
// Required LangChain BaseCallbackHandler properties
52+
lc_serializable: false,
53+
lc_namespace: ['langchain_core', 'callbacks', 'sentry'],
54+
lc_secrets: undefined,
55+
lc_attributes: undefined,
56+
lc_aliases: undefined,
57+
lc_serializable_keys: undefined,
58+
lc_id: ['langchain_core', 'callbacks', 'sentry'],
59+
lc_kwargs: {},
60+
name: 'SentryCallbackHandler',
61+
62+
// BaseCallbackHandlerInput boolean flags
63+
ignoreLLM: false,
64+
ignoreChain: false,
65+
ignoreAgent: false,
66+
ignoreRetriever: false,
67+
ignoreCustomEvent: false,
68+
raiseError: false,
69+
awaitHandlers: true,
70+
8071
handleLLMStart(
81-
llm: LangChainSerializedLLM,
72+
llm: unknown,
8273
prompts: string[],
8374
runId: string,
8475
_parentRunId?: string,
8576
_extraParams?: Record<string, unknown>,
86-
tags?: string[] | Record<string, unknown>,
77+
tags?: string[],
8778
metadata?: Record<string, unknown>,
88-
_runName?: string | Record<string, unknown>,
79+
_runName?: string,
8980
) {
9081
try {
9182
const invocationParams = getInvocationParams(tags);
92-
const attributes = extractLLMRequestAttributes(llm, prompts, recordInputs, invocationParams, metadata);
83+
const attributes = extractLLMRequestAttributes(
84+
llm as LangChainSerialized,
85+
prompts,
86+
recordInputs,
87+
invocationParams,
88+
metadata,
89+
);
9390
const modelName = attributes[GEN_AI_REQUEST_MODEL_ATTRIBUTE];
9491
const operationName = attributes[GEN_AI_OPERATION_NAME_ATTRIBUTE];
9592

@@ -114,18 +111,24 @@ export function createLangChainCallbackHandler(options: LangChainOptions = {}):
114111

115112
// Chat Model Start Handler
116113
handleChatModelStart(
117-
llm: LangChainSerializedLLM,
118-
messages: LangChainMessage[][],
114+
llm: unknown,
115+
messages: unknown,
119116
runId: string,
120117
_parentRunId?: string,
121118
_extraParams?: Record<string, unknown>,
122-
tags?: string[] | Record<string, unknown>,
119+
tags?: string[],
123120
metadata?: Record<string, unknown>,
124-
_runName?: string | Record<string, unknown>,
121+
_runName?: string,
125122
) {
126123
try {
127124
const invocationParams = getInvocationParams(tags);
128-
const attributes = extractChatModelRequestAttributes(llm, messages, recordInputs, invocationParams, metadata);
125+
const attributes = extractChatModelRequestAttributes(
126+
llm as LangChainSerialized,
127+
messages as LangChainMessage[][],
128+
recordInputs,
129+
invocationParams,
130+
metadata,
131+
);
129132
const modelName = attributes[GEN_AI_REQUEST_MODEL_ATTRIBUTE];
130133
const operationName = attributes[GEN_AI_OPERATION_NAME_ATTRIBUTE];
131134

@@ -151,7 +154,7 @@ export function createLangChainCallbackHandler(options: LangChainOptions = {}):
151154

152155
// LLM End Handler - note: handleLLMEnd with capital LLM (used by both LLMs and chat models!)
153156
handleLLMEnd(
154-
output: LangChainLLMResult,
157+
output: unknown,
155158
runId: string,
156159
_parentRunId?: string,
157160
_tags?: string[],
@@ -160,7 +163,7 @@ export function createLangChainCallbackHandler(options: LangChainOptions = {}):
160163
try {
161164
const span = spanMap.get(runId);
162165
if (span) {
163-
const attributes = extractLlmResponseAttributes(output, recordOutputs);
166+
const attributes = extractLlmResponseAttributes(output as LangChainLLMResult, recordOutputs);
164167
if (attributes) {
165168
span.setAttributes(attributes);
166169
}
@@ -227,7 +230,7 @@ export function createLangChainCallbackHandler(options: LangChainOptions = {}):
227230
},
228231

229232
// Chain End Handler
230-
handleChainEnd(outputs: Record<string, unknown>, runId: string) {
233+
handleChainEnd(outputs: unknown, runId: string) {
231234
try {
232235
const span = spanMap.get(runId);
233236
if (span) {
@@ -298,14 +301,14 @@ export function createLangChainCallbackHandler(options: LangChainOptions = {}):
298301
},
299302

300303
// Tool End Handler
301-
handleToolEnd(output: string, runId: string) {
304+
handleToolEnd(output: unknown, runId: string) {
302305
try {
303306
const span = spanMap.get(runId);
304307
if (span) {
305308
// Add output if recordOutputs is enabled
306309
if (recordOutputs) {
307310
span.setAttributes({
308-
'gen_ai.tool.output': output,
311+
'gen_ai.tool.output': JSON.stringify(output),
309312
});
310313
}
311314
exitSpan(runId);
@@ -334,6 +337,27 @@ export function createLangChainCallbackHandler(options: LangChainOptions = {}):
334337
// silently ignore errors
335338
}
336339
},
340+
341+
// LangChain BaseCallbackHandler required methods
342+
copy() {
343+
return handler;
344+
},
345+
346+
toJSON() {
347+
return {
348+
lc: 1,
349+
type: 'not_implemented',
350+
id: handler.lc_id,
351+
};
352+
},
353+
354+
toJSONNotImplemented() {
355+
return {
356+
lc: 1,
357+
type: 'not_implemented',
358+
id: handler.lc_id,
359+
};
360+
},
337361
};
338362

339363
return handler;

0 commit comments

Comments
 (0)