@@ -237,26 +237,6 @@ def get_word_length(word: str) -> int:
237237 return len (word )
238238
239239
240- global stream_result_mock # type: Mock
241- global llm_type # type: str
242-
243-
244- class MockOpenAI (ChatOpenAI ):
245- def _stream (
246- self ,
247- messages : List [BaseMessage ],
248- stop : Optional [List [str ]] = None ,
249- run_manager : Optional [CallbackManagerForLLMRun ] = None ,
250- ** kwargs : Any ,
251- ) -> Iterator [ChatGenerationChunk ]:
252- for x in stream_result_mock ():
253- yield x
254-
255- @property
256- def _llm_type (self ) -> str :
257- return llm_type
258-
259-
260240def test_langchain_text_completion (
261241 sentry_init ,
262242 capture_events ,
@@ -1488,8 +1468,22 @@ def test_langchain_openai_tools_agent_stream_with_config(
14881468
14891469
14901470def test_langchain_error (sentry_init , capture_events ):
1491- global llm_type
1492- llm_type = "acme-llm"
1471+ class MockOpenAI (ChatOpenAI ):
1472+ def _stream (
1473+ self ,
1474+ messages : List [BaseMessage ],
1475+ stop : Optional [List [str ]] = None ,
1476+ run_manager : Optional [CallbackManagerForLLMRun ] = None ,
1477+ ** kwargs : Any ,
1478+ ) -> Iterator [ChatGenerationChunk ]:
1479+ stream_result_mock = Mock (side_effect = ValueError ("API rate limit error" ))
1480+
1481+ for x in stream_result_mock ():
1482+ yield x
1483+
1484+ @property
1485+ def _llm_type (self ) -> str :
1486+ return "acme-llm"
14931487
14941488 sentry_init (
14951489 integrations = [LangchainIntegration (include_prompts = True )],
@@ -1508,8 +1502,6 @@ def test_langchain_error(sentry_init, capture_events):
15081502 MessagesPlaceholder (variable_name = "agent_scratchpad" ),
15091503 ]
15101504 )
1511- global stream_result_mock
1512- stream_result_mock = Mock (side_effect = ValueError ("API rate limit error" ))
15131505 llm = MockOpenAI (
15141506 model_name = "gpt-3.5-turbo" ,
15151507 temperature = 0 ,
@@ -1527,8 +1519,22 @@ def test_langchain_error(sentry_init, capture_events):
15271519
15281520
15291521def test_span_status_error (sentry_init , capture_events ):
1530- global llm_type
1531- llm_type = "acme-llm"
1522+ class MockOpenAI (ChatOpenAI ):
1523+ def _stream (
1524+ self ,
1525+ messages : List [BaseMessage ],
1526+ stop : Optional [List [str ]] = None ,
1527+ run_manager : Optional [CallbackManagerForLLMRun ] = None ,
1528+ ** kwargs : Any ,
1529+ ) -> Iterator [ChatGenerationChunk ]:
1530+ stream_result_mock = Mock (side_effect = ValueError ("API rate limit error" ))
1531+
1532+ for x in stream_result_mock ():
1533+ yield x
1534+
1535+ @property
1536+ def _llm_type (self ) -> str :
1537+ return "acme-llm"
15321538
15331539 sentry_init (
15341540 integrations = [LangchainIntegration (include_prompts = True )],
@@ -1547,8 +1553,6 @@ def test_span_status_error(sentry_init, capture_events):
15471553 MessagesPlaceholder (variable_name = "agent_scratchpad" ),
15481554 ]
15491555 )
1550- global stream_result_mock
1551- stream_result_mock = Mock (side_effect = ValueError ("API rate limit error" ))
15521556 llm = MockOpenAI (
15531557 model_name = "gpt-3.5-turbo" ,
15541558 temperature = 0 ,
@@ -1781,8 +1785,32 @@ def test_langchain_callback_list_existing_callback(sentry_init):
17811785
17821786def test_langchain_message_role_mapping (sentry_init , capture_events ):
17831787 """Test that message roles are properly normalized in langchain integration."""
1784- global llm_type
1785- llm_type = "openai-chat"
1788+
1789+ class MockOpenAI (ChatOpenAI ):
1790+ def _stream (
1791+ self ,
1792+ messages : List [BaseMessage ],
1793+ stop : Optional [List [str ]] = None ,
1794+ run_manager : Optional [CallbackManagerForLLMRun ] = None ,
1795+ ** kwargs : Any ,
1796+ ) -> Iterator [ChatGenerationChunk ]:
1797+ stream_result_mock = Mock (
1798+ side_effect = [
1799+ [
1800+ ChatGenerationChunk (
1801+ type = "ChatGenerationChunk" ,
1802+ message = AIMessageChunk (content = "Test response" ),
1803+ ),
1804+ ]
1805+ ]
1806+ )
1807+
1808+ for x in stream_result_mock ():
1809+ yield x
1810+
1811+ @property
1812+ def _llm_type (self ) -> str :
1813+ return "openai-chat"
17861814
17871815 sentry_init (
17881816 integrations = [LangchainIntegration (include_prompts = True )],
@@ -1799,18 +1827,6 @@ def test_langchain_message_role_mapping(sentry_init, capture_events):
17991827 ]
18001828 )
18011829
1802- global stream_result_mock
1803- stream_result_mock = Mock (
1804- side_effect = [
1805- [
1806- ChatGenerationChunk (
1807- type = "ChatGenerationChunk" ,
1808- message = AIMessageChunk (content = "Test response" ),
1809- ),
1810- ]
1811- ]
1812- )
1813-
18141830 llm = MockOpenAI (
18151831 model_name = "gpt-3.5-turbo" ,
18161832 temperature = 0 ,
0 commit comments