Skip to content

Commit 2deb597

Browse files
committed
gen_ai system
1 parent 485ae4d commit 2deb597

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

sentry_sdk/integrations/langchain.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,13 @@ def on_llm_start(
261261
all_params = kwargs.get("invocation_params", {})
262262
all_params.update(serialized.get("kwargs", {}))
263263

264+
model = (
265+
all_params.get("model")
266+
or all_params.get("model_name")
267+
or all_params.get("model_id")
268+
or ""
269+
)
270+
264271
watched_span = self._create_span(
265272
run_id=run_id,
266273
parent_id=parent_run_id,
@@ -269,19 +276,26 @@ def on_llm_start(
269276
origin=LangchainIntegration.origin,
270277
)
271278
span = watched_span.span
272-
span.set_data(
273-
SPANDATA.GEN_AI_REQUEST_MODEL,
274-
all_params.get(
275-
"model", all_params.get("model_name", all_params.get("model_id"))
276-
),
277-
)
278-
if should_send_default_pii() and self.include_prompts:
279-
set_data_normalized(span, SPANDATA.GEN_AI_REQUEST_MESSAGES, prompts)
279+
280+
if model:
281+
span.set_data(
282+
SPANDATA.GEN_AI_REQUEST_MODEL,
283+
model,
284+
)
285+
286+
ai_type = all_params.get("_type")
287+
if "anthropic" in ai_type:
288+
span.set_data(SPANDATA.GEN_AI_SYSTEM, "anthropic")
289+
elif "openai" in ai_type:
290+
span.set_data(SPANDATA.GEN_AI_SYSTEM, "openai")
280291

281292
for key, attribute in DATA_FIELDS.items():
282293
if key in all_params and all_params[key] is not None:
283294
set_data_normalized(span, attribute, all_params[key], unpack=False)
284295

296+
if should_send_default_pii() and self.include_prompts:
297+
set_data_normalized(span, SPANDATA.GEN_AI_REQUEST_MESSAGES, prompts)
298+
285299
def on_chat_model_start(self, serialized, messages, *, run_id, **kwargs):
286300
# type: (SentryLangchainCallback, Dict[str, Any], List[List[BaseMessage]], UUID, Any) -> Any
287301
"""Run when Chat Model starts running."""
@@ -311,6 +325,12 @@ def on_chat_model_start(self, serialized, messages, *, run_id, **kwargs):
311325
if model:
312326
span.set_data(SPANDATA.GEN_AI_REQUEST_MODEL, model)
313327

328+
ai_type = all_params.get("_type")
329+
if "anthropic" in ai_type:
330+
span.set_data(SPANDATA.GEN_AI_SYSTEM, "anthropic")
331+
elif "openai" in ai_type:
332+
span.set_data(SPANDATA.GEN_AI_SYSTEM, "openai")
333+
314334
for key, attribute in DATA_FIELDS.items():
315335
if key in all_params and all_params[key] is not None:
316336
set_data_normalized(span, attribute, all_params[key], unpack=False)
@@ -429,7 +449,7 @@ def on_llm_end(self, response, *, run_id, **kwargs):
429449
pass
430450

431451
try:
432-
tool_calls = generation_result.message.get("tool_calls")
452+
tool_calls = getattr(generation_result.message, "tool_calls", None)
433453
if tool_calls is not None:
434454
set_data_normalized(
435455
span,

0 commit comments

Comments
 (0)