Skip to content

Commit 17a7bc1

Browse files
authored
fix(langchain): do not end span on agent callbacks (#1312)
Co-authored-by: @qnnn
1 parent bd5f72f commit 17a7bc1

File tree

1 file changed

+28
-41
lines changed

1 file changed

+28
-41
lines changed

langfuse/langchain/CallbackHandler.py

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,6 @@ def on_retriever_error(
209209
self._log_debug_event(
210210
"on_retriever_error", run_id, parent_run_id, error=error
211211
)
212-
213-
if run_id is None or run_id not in self.runs:
214-
raise Exception("run not found")
215-
216212
observation = self._detach_observation(run_id)
217213

218214
if observation is not None:
@@ -401,19 +397,17 @@ def on_agent_action(
401397
"on_agent_action", run_id, parent_run_id, action=action
402398
)
403399

404-
if run_id not in self.runs:
405-
raise Exception("run not found")
400+
agent_run = self.runs.get(run_id, None)
406401

407-
agent_run = self.runs[run_id]
408-
if hasattr(agent_run, "_otel_span"):
402+
if agent_run is not None:
409403
agent_run._otel_span.set_attribute(
410404
LangfuseOtelSpanAttributes.OBSERVATION_TYPE, "agent"
411405
)
412406

413-
agent_run.update(
414-
output=action,
415-
input=kwargs.get("inputs"),
416-
).end()
407+
agent_run.update(
408+
output=action,
409+
input=kwargs.get("inputs"),
410+
)
417411

418412
except Exception as e:
419413
langfuse_logger.exception(e)
@@ -430,10 +424,9 @@ def on_agent_finish(
430424
self._log_debug_event(
431425
"on_agent_finish", run_id, parent_run_id, finish=finish
432426
)
433-
if run_id not in self.runs:
434-
raise Exception("run not found")
435-
436-
agent_run = self._detach_observation(run_id)
427+
# Langchain is sending same run ID for both agent finish and chain end
428+
# handle cleanup of observation in the chain end callback
429+
agent_run = self.runs.get(run_id, None)
437430

438431
if agent_run is not None:
439432
agent_run._otel_span.set_attribute(
@@ -443,7 +436,7 @@ def on_agent_finish(
443436
agent_run.update(
444437
output=finish,
445438
input=kwargs.get("inputs"),
446-
).end()
439+
)
447440

448441
except Exception as e:
449442
langfuse_logger.exception(e)
@@ -461,9 +454,6 @@ def on_chain_end(
461454
"on_chain_end", run_id, parent_run_id, outputs=outputs
462455
)
463456

464-
if run_id not in self.runs:
465-
raise Exception("run not found")
466-
467457
span = self._detach_observation(run_id)
468458

469459
if span is not None:
@@ -846,31 +836,28 @@ def on_llm_end(
846836
self._log_debug_event(
847837
"on_llm_end", run_id, parent_run_id, response=response, kwargs=kwargs
848838
)
849-
if run_id not in self.runs:
850-
raise Exception("Run not found, see docs what to do in this case.")
851-
else:
852-
response_generation = response.generations[-1][-1]
853-
extracted_response = (
854-
self._convert_message_to_dict(response_generation.message)
855-
if isinstance(response_generation, ChatGeneration)
856-
else _extract_raw_response(response_generation)
857-
)
839+
response_generation = response.generations[-1][-1]
840+
extracted_response = (
841+
self._convert_message_to_dict(response_generation.message)
842+
if isinstance(response_generation, ChatGeneration)
843+
else _extract_raw_response(response_generation)
844+
)
858845

859-
llm_usage = _parse_usage(response)
846+
llm_usage = _parse_usage(response)
860847

861-
# e.g. azure returns the model name in the response
862-
model = _parse_model(response)
848+
# e.g. azure returns the model name in the response
849+
model = _parse_model(response)
863850

864-
generation = self._detach_observation(run_id)
851+
generation = self._detach_observation(run_id)
865852

866-
if generation is not None:
867-
generation.update(
868-
output=extracted_response,
869-
usage=llm_usage,
870-
usage_details=llm_usage,
871-
input=kwargs.get("inputs"),
872-
model=model,
873-
).end()
853+
if generation is not None:
854+
generation.update(
855+
output=extracted_response,
856+
usage=llm_usage,
857+
usage_details=llm_usage,
858+
input=kwargs.get("inputs"),
859+
model=model,
860+
).end()
874861

875862
except Exception as e:
876863
langfuse_logger.exception(e)

0 commit comments

Comments
 (0)