Skip to content

Commit 6cda4e0

Browse files
committed
check if span is recording before adding to it
1 parent 00cdaf6 commit 6cda4e0

File tree

1 file changed

+7
-7
lines changed
  • packages/core/src/utils/langchain

1 file changed

+7
-7
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function createLangChainCallbackHandler(options: LangChainOptions = {}):
3737
*/
3838
const exitSpan = (runId: string): void => {
3939
const span = spanMap.get(runId);
40-
if (span) {
40+
if (span?.isRecording()) {
4141
span.end();
4242
spanMap.delete(runId);
4343
}
@@ -152,7 +152,7 @@ export function createLangChainCallbackHandler(options: LangChainOptions = {}):
152152
_extraParams?: Record<string, unknown>,
153153
) {
154154
const span = spanMap.get(runId);
155-
if (span) {
155+
if (span?.isRecording()) {
156156
const attributes = extractLlmResponseAttributes(output as LangChainLLMResult, recordOutputs);
157157
if (attributes) {
158158
span.setAttributes(attributes);
@@ -164,7 +164,7 @@ export function createLangChainCallbackHandler(options: LangChainOptions = {}):
164164
// LLM Error Handler - note: handleLLMError with capital LLM
165165
handleLLMError(error: Error, runId: string) {
166166
const span = spanMap.get(runId);
167-
if (span) {
167+
if (span?.isRecording()) {
168168
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'llm_error' });
169169
exitSpan(runId);
170170
}
@@ -209,7 +209,7 @@ export function createLangChainCallbackHandler(options: LangChainOptions = {}):
209209
// Chain End Handler
210210
handleChainEnd(outputs: unknown, runId: string) {
211211
const span = spanMap.get(runId);
212-
if (span) {
212+
if (span?.isRecording()) {
213213
// Add outputs if recordOutputs is enabled
214214
if (recordOutputs) {
215215
span.setAttributes({
@@ -223,7 +223,7 @@ export function createLangChainCallbackHandler(options: LangChainOptions = {}):
223223
// Chain Error Handler
224224
handleChainError(error: Error, runId: string) {
225225
const span = spanMap.get(runId);
226-
if (span) {
226+
if (span?.isRecording()) {
227227
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'chain_error' });
228228
exitSpan(runId);
229229
}
@@ -268,7 +268,7 @@ export function createLangChainCallbackHandler(options: LangChainOptions = {}):
268268
// Tool End Handler
269269
handleToolEnd(output: unknown, runId: string) {
270270
const span = spanMap.get(runId);
271-
if (span) {
271+
if (span?.isRecording()) {
272272
// Add output if recordOutputs is enabled
273273
if (recordOutputs) {
274274
span.setAttributes({
@@ -282,7 +282,7 @@ export function createLangChainCallbackHandler(options: LangChainOptions = {}):
282282
// Tool Error Handler
283283
handleToolError(error: Error, runId: string) {
284284
const span = spanMap.get(runId);
285-
if (span) {
285+
if (span?.isRecording()) {
286286
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'tool_error' });
287287
exitSpan(runId);
288288
}

0 commit comments

Comments
 (0)