Skip to content

Commit 9669277

Browse files
committed
nicer traces
1 parent 4f140e2 commit 9669277

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

sentry_sdk/integrations/langchain.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,21 @@ def on_llm_start(
183183
return
184184
all_params = kwargs.get("invocation_params", {})
185185
all_params.update(serialized.get("kwargs", {}))
186+
186187
watched_span = self._create_span(
187188
run_id,
188189
kwargs.get("parent_run_id"),
189-
op=OP.GEN_AI_RUN,
190+
op=OP.GEN_AI_PIPELINE,
190191
name=kwargs.get("name") or "Langchain LLM call",
191192
origin=LangchainIntegration.origin,
192193
)
193194
span = watched_span.span
195+
span.set_data(
196+
SPANDATA.GEN_AI_REQUEST_MODEL,
197+
all_params.get(
198+
"model", all_params.get("model_name", all_params.get("model_id"))
199+
),
200+
)
194201
if should_send_default_pii() and self.include_prompts:
195202
set_data_normalized(span, SPANDATA.GEN_AI_REQUEST_MESSAGES, prompts)
196203
for k, v in DATA_FIELDS.items():
@@ -305,22 +312,21 @@ def on_chain_start(self, serialized, inputs, *, run_id, **kwargs):
305312
with capture_internal_exceptions():
306313
if not run_id:
307314
return
308-
watched_span = self._create_span(
315+
"""watched_span = self._create_span(
309316
run_id,
310317
kwargs.get("parent_run_id"),
311-
op=(
312-
OP.GEN_AI_RUN
313-
if kwargs.get("parent_run_id") is not None
314-
else OP.GEN_AI_PIPELINE
315-
),
318+
# not sure about this one - it kinda spams the UI with a lot of spans
319+
op=OP.GEN_AI_PIPELINE,
316320
name=kwargs.get("name") or "Chain execution",
317-
origin=LangchainIntegration.origin,
321+
origin=LangchainIntegration.origin
318322
)
323+
watched_span.span.set_data(SPANDATA.GEN_AI_REQUEST_MODEL, kwargs.get("model", kwargs.get("model_name", kwargs.get("model_id"))))
319324
metadata = kwargs.get("metadata")
320325
if metadata:
321326
set_data_normalized(
322327
watched_span.span, SPANDATA.GEN_AI_REQUEST_METADATA, metadata
323328
)
329+
"""
324330

325331
def on_chain_end(self, outputs, *, run_id, **kwargs):
326332
# type: (SentryLangchainCallback, Dict[str, Any], UUID, Any) -> Any
@@ -423,6 +429,11 @@ def on_tool_end(self, output, *, run_id, **kwargs):
423429
def on_tool_error(self, error, *args, run_id, **kwargs):
424430
# type: (SentryLangchainCallback, Union[Exception, KeyboardInterrupt], UUID, Any) -> Any
425431
"""Run when tool errors."""
432+
# TODO(shellmayr): how to correctly set the status when the toolfails
433+
if run_id and run_id in self.span_map:
434+
span_data = self.span_map[run_id]
435+
if span_data:
436+
span_data.span.set_status("unknown")
426437
self._handle_error(run_id, error)
427438

428439

0 commit comments

Comments
 (0)