|
1 | 1 | from typing import List, Optional, Any, Iterator
|
| 2 | +from unittest import mock |
2 | 3 | from unittest.mock import Mock
|
3 | 4 |
|
4 | 5 | import pytest
|
|
12 | 13 | # Langchain < 0.2
|
13 | 14 | from langchain_community.chat_models import ChatOpenAI
|
14 | 15 |
|
15 |
| -from langchain_core.callbacks import CallbackManagerForLLMRun |
| 16 | +from langchain_core.callbacks import BaseCallbackManager, CallbackManagerForLLMRun |
16 | 17 | from langchain_core.messages import BaseMessage, AIMessageChunk
|
17 | 18 | from langchain_core.outputs import ChatGenerationChunk
|
18 | 19 |
|
19 | 20 | from sentry_sdk import start_transaction
|
20 |
| -from sentry_sdk.integrations.langchain import LangchainIntegration |
| 21 | +from sentry_sdk.integrations.langchain import ( |
| 22 | + LangchainIntegration, |
| 23 | + SentryLangchainCallback, |
| 24 | +) |
21 | 25 | from langchain.agents import tool, AgentExecutor, create_openai_tools_agent
|
22 | 26 | from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
23 | 27 |
|
@@ -342,3 +346,131 @@ def test_span_origin(sentry_init, capture_events):
|
342 | 346 | assert event["contexts"]["trace"]["origin"] == "manual"
|
343 | 347 | for span in event["spans"]:
|
344 | 348 | assert span["origin"] == "auto.ai.langchain"
|
| 349 | + |
| 350 | + |
| 351 | +def test_langchain_callback_manager(sentry_init): |
| 352 | + sentry_init( |
| 353 | + integrations=[LangchainIntegration()], |
| 354 | + traces_sample_rate=1.0, |
| 355 | + ) |
| 356 | + local_manager = BaseCallbackManager(handlers=[]) |
| 357 | + |
| 358 | + with mock.patch("sentry_sdk.integrations.langchain.manager") as mock_manager_module: |
| 359 | + mock_configure = mock_manager_module._configure |
| 360 | + |
| 361 | + # Explicitly re-run setup_once, so that mock_manager_module._configure gets patched |
| 362 | + LangchainIntegration.setup_once() |
| 363 | + |
| 364 | + callback_manager_cls = Mock() |
| 365 | + |
| 366 | + mock_manager_module._configure( |
| 367 | + callback_manager_cls, local_callbacks=local_manager |
| 368 | + ) |
| 369 | + |
| 370 | + assert mock_configure.call_count == 1 |
| 371 | + |
| 372 | + call_args = mock_configure.call_args |
| 373 | + assert call_args.args[0] is callback_manager_cls |
| 374 | + |
| 375 | + passed_manager = call_args.args[2] |
| 376 | + assert passed_manager is not local_manager |
| 377 | + assert local_manager.handlers == [] |
| 378 | + |
| 379 | + [handler] = passed_manager.handlers |
| 380 | + assert isinstance(handler, SentryLangchainCallback) |
| 381 | + |
| 382 | + |
| 383 | +def test_langchain_callback_manager_with_sentry_callback(sentry_init): |
| 384 | + sentry_init( |
| 385 | + integrations=[LangchainIntegration()], |
| 386 | + traces_sample_rate=1.0, |
| 387 | + ) |
| 388 | + sentry_callback = SentryLangchainCallback(0, False) |
| 389 | + local_manager = BaseCallbackManager(handlers=[sentry_callback]) |
| 390 | + |
| 391 | + with mock.patch("sentry_sdk.integrations.langchain.manager") as mock_manager_module: |
| 392 | + mock_configure = mock_manager_module._configure |
| 393 | + |
| 394 | + # Explicitly re-run setup_once, so that mock_manager_module._configure gets patched |
| 395 | + LangchainIntegration.setup_once() |
| 396 | + |
| 397 | + callback_manager_cls = Mock() |
| 398 | + |
| 399 | + mock_manager_module._configure( |
| 400 | + callback_manager_cls, local_callbacks=local_manager |
| 401 | + ) |
| 402 | + |
| 403 | + assert mock_configure.call_count == 1 |
| 404 | + |
| 405 | + call_args = mock_configure.call_args |
| 406 | + assert call_args.args[0] is callback_manager_cls |
| 407 | + |
| 408 | + passed_manager = call_args.args[2] |
| 409 | + assert passed_manager is local_manager |
| 410 | + |
| 411 | + [handler] = passed_manager.handlers |
| 412 | + assert handler is sentry_callback |
| 413 | + |
| 414 | + |
| 415 | +def test_langchain_callback_list(sentry_init): |
| 416 | + sentry_init( |
| 417 | + integrations=[LangchainIntegration()], |
| 418 | + traces_sample_rate=1.0, |
| 419 | + ) |
| 420 | + local_callbacks = [] |
| 421 | + |
| 422 | + with mock.patch("sentry_sdk.integrations.langchain.manager") as mock_manager_module: |
| 423 | + mock_configure = mock_manager_module._configure |
| 424 | + |
| 425 | + # Explicitly re-run setup_once, so that mock_manager_module._configure gets patched |
| 426 | + LangchainIntegration.setup_once() |
| 427 | + |
| 428 | + callback_manager_cls = Mock() |
| 429 | + |
| 430 | + mock_manager_module._configure( |
| 431 | + callback_manager_cls, local_callbacks=local_callbacks |
| 432 | + ) |
| 433 | + |
| 434 | + assert mock_configure.call_count == 1 |
| 435 | + |
| 436 | + call_args = mock_configure.call_args |
| 437 | + assert call_args.args[0] is callback_manager_cls |
| 438 | + |
| 439 | + passed_callbacks = call_args.args[2] |
| 440 | + assert passed_callbacks is not local_callbacks |
| 441 | + assert local_callbacks == [] |
| 442 | + |
| 443 | + [handler] = passed_callbacks |
| 444 | + assert isinstance(handler, SentryLangchainCallback) |
| 445 | + |
| 446 | + |
| 447 | +def test_langchain_callback_list_existing_callback(sentry_init): |
| 448 | + sentry_init( |
| 449 | + integrations=[LangchainIntegration()], |
| 450 | + traces_sample_rate=1.0, |
| 451 | + ) |
| 452 | + sentry_callback = SentryLangchainCallback(0, False) |
| 453 | + local_callbacks = [sentry_callback] |
| 454 | + |
| 455 | + with mock.patch("sentry_sdk.integrations.langchain.manager") as mock_manager_module: |
| 456 | + mock_configure = mock_manager_module._configure |
| 457 | + |
| 458 | + # Explicitly re-run setup_once, so that mock_manager_module._configure gets patched |
| 459 | + LangchainIntegration.setup_once() |
| 460 | + |
| 461 | + callback_manager_cls = Mock() |
| 462 | + |
| 463 | + mock_manager_module._configure( |
| 464 | + callback_manager_cls, local_callbacks=local_callbacks |
| 465 | + ) |
| 466 | + |
| 467 | + assert mock_configure.call_count == 1 |
| 468 | + |
| 469 | + call_args = mock_configure.call_args |
| 470 | + assert call_args.args[0] is callback_manager_cls |
| 471 | + |
| 472 | + passed_callbacks = call_args.args[2] |
| 473 | + assert passed_callbacks is local_callbacks |
| 474 | + |
| 475 | + [handler] = passed_callbacks |
| 476 | + assert handler is sentry_callback |
0 commit comments