Skip to content

Commit 3e207ab

Browse files
ci: Add langchain-base to uv typing group (#7016)
Add `langchain-base` to the typing dependency group and fix the resulting mypy errors.
1 parent 3e96fc3 commit 3e207ab

4 files changed

Lines changed: 478 additions & 152 deletions

File tree

‎pyproject.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ typing = [
7171
"starlette>=1.3.1",
7272
"python-multipart>=0.0.32",
7373
"pydantic>=2.13.4",
74+
"langchain-core>=1.5.3",
7475
]
7576
test = [
7677
"dataclasses ; python_full_version < '3.7'",

‎sentry_sdk/integrations/langchain.py‎

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
manager,
5353
)
5454
from langchain_core.messages import BaseMessage
55-
from langchain_core.outputs import LLMResult
55+
from langchain_core.outputs import ChatGeneration, LLMResult
5656

5757
except ImportError:
5858
raise DidNotEnable("langchain not installed")
@@ -247,7 +247,7 @@ def setup_once() -> None:
247247
_patch_embeddings_provider(OllamaEmbeddings)
248248

249249

250-
class SentryLangchainCallback(BaseCallbackHandler): # type: ignore[misc]
250+
class SentryLangchainCallback(BaseCallbackHandler):
251251
"""Callback handler that creates Sentry spans."""
252252

253253
def __init__(
@@ -561,44 +561,32 @@ def on_llm_end(
561561
except IndexError:
562562
generation = None
563563

564-
if generation is not None:
565-
set_on_span = (
566-
span.set_attribute
567-
if isinstance(span, StreamedSpan)
568-
else span.set_data
569-
)
564+
set_on_span = (
565+
span.set_attribute if isinstance(span, StreamedSpan) else span.set_data
566+
)
570567

571-
try:
572-
response_model = generation.message.response_metadata.get(
573-
"model_name"
568+
if generation is not None and generation.generation_info is not None:
569+
finish_reason = generation.generation_info.get("finish_reason")
570+
if finish_reason is not None:
571+
set_on_span(
572+
SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS,
573+
[finish_reason],
574574
)
575-
if response_model is not None:
576-
set_on_span(SPANDATA.GEN_AI_RESPONSE_MODEL, response_model)
577-
except AttributeError:
578-
pass
579575

580-
try:
581-
finish_reason = generation.generation_info.get("finish_reason")
582-
if finish_reason is not None:
583-
set_on_span(
584-
SPANDATA.GEN_AI_RESPONSE_FINISH_REASONS,
585-
[finish_reason],
586-
)
587-
except AttributeError:
588-
pass
576+
if isinstance(generation, ChatGeneration):
577+
response_model = generation.message.response_metadata.get("model_name")
578+
if response_model is not None:
579+
set_on_span(SPANDATA.GEN_AI_RESPONSE_MODEL, response_model)
589580

590-
try:
591-
if should_send_default_pii() and self.include_prompts:
592-
tool_calls = getattr(generation.message, "tool_calls", None)
593-
if tool_calls is not None and tool_calls != []:
594-
set_data_normalized(
595-
span,
596-
SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS,
597-
tool_calls,
598-
unpack=False,
599-
)
600-
except AttributeError:
601-
pass
581+
if should_send_default_pii() and self.include_prompts:
582+
tool_calls = getattr(generation.message, "tool_calls", None)
583+
if tool_calls is not None and tool_calls != []:
584+
set_data_normalized(
585+
span,
586+
SPANDATA.GEN_AI_RESPONSE_TOOL_CALLS,
587+
tool_calls,
588+
unpack=False,
589+
)
602590

603591
if should_send_default_pii() and self.include_prompts:
604592
set_data_normalized(
@@ -612,7 +600,7 @@ def on_llm_end(
612600

613601
def on_llm_error(
614602
self: "SentryLangchainCallback",
615-
error: "Union[Exception, KeyboardInterrupt]",
603+
error: "BaseException",
616604
*,
617605
run_id: "UUID",
618606
**kwargs: "Any",
@@ -719,8 +707,8 @@ def on_tool_end(
719707

720708
def on_tool_error(
721709
self,
722-
error: "SentryLangchainCallback",
723-
*args: "Union[Exception, KeyboardInterrupt]",
710+
error: "BaseException",
711+
*,
724712
run_id: "UUID",
725713
**kwargs: "Any",
726714
) -> "Any":

0 commit comments

Comments
 (0)