diff --git a/lib/crewai/tests/cassettes/utilities/test_llm_emits_call_failed_event.yaml b/lib/crewai/tests/cassettes/utilities/test_llm_emits_call_failed_event.yaml deleted file mode 100644 index e572b5b5d..000000000 --- a/lib/crewai/tests/cassettes/utilities/test_llm_emits_call_failed_event.yaml +++ /dev/null @@ -1,105 +0,0 @@ -interactions: -- request: - body: '{"messages": [{"role": "user", "content": "Hello, how are you?"}], "model": - "gpt-4o-mini", "stop": []}' - headers: - accept: - - application/json - accept-encoding: - - gzip, deflate - connection: - - keep-alive - content-length: - - '102' - content-type: - - application/json - cookie: - - _cfuvid=IY8ppO70AMHr2skDSUsGh71zqHHdCQCZ3OvkPi26NBc-1740424913267-0.0.1.1-604800000; - __cf_bm=fU6K5KZoDmgcEuF8_yWAYKUO5fKHh6q5.wDPnna393g-1740424913-1.0.1.1-2iOaq3JVGWs439V0HxJee0IC9HdJm7dPkeJorD.AGw0YwkngRPM8rrTzn_7ht1BkbOauEezj.wPKcBz18gIYUg - host: - - api.openai.com - user-agent: - - OpenAI/Python 1.61.0 - x-stainless-arch: - - arm64 - x-stainless-async: - - 'false' - x-stainless-lang: - - python - x-stainless-os: - - MacOS - x-stainless-package-version: - - 1.61.0 - x-stainless-raw-response: - - 'true' - x-stainless-retry-count: - - '0' - x-stainless-runtime: - - CPython - x-stainless-runtime-version: - - 3.12.8 - method: POST - uri: https://api.openai.com/v1/chat/completions - response: - body: - string: "{\n \"id\": \"chatcmpl-B4YLA2SrC2rwdVQ3U87G5a0P5lsLw\",\n \"object\"\ - : \"chat.completion\",\n \"created\": 1740425016,\n \"model\": \"gpt-4o-mini-2024-07-18\"\ - ,\n \"choices\": [\n {\n \"index\": 0,\n \"message\": {\n \ - \ \"role\": \"assistant\",\n \"content\": \"Hello! I'm just a\ - \ computer program, so I don't have feelings, but I'm here and ready to help\ - \ you. How can I assist you today?\",\n \"refusal\": null\n },\n\ - \ \"logprobs\": null,\n \"finish_reason\": \"stop\"\n }\n ],\n\ - \ \"usage\": {\n \"prompt_tokens\": 13,\n \"completion_tokens\": 30,\n\ - \ \"total_tokens\": 43,\n \"prompt_tokens_details\": {\n \"cached_tokens\"\ - : 0,\n \"audio_tokens\": 0\n },\n \"completion_tokens_details\"\ - : {\n \"reasoning_tokens\": 0,\n \"audio_tokens\": 0,\n \"\ - accepted_prediction_tokens\": 0,\n \"rejected_prediction_tokens\": 0\n\ - \ }\n },\n \"service_tier\": \"default\",\n \"system_fingerprint\":\ - \ \"fp_709714d124\"\n}\n" - headers: - CF-RAY: - - 9171d4c0ed44236e-SJC - Connection: - - keep-alive - Content-Type: - - application/json - Date: - - Mon, 24 Feb 2025 19:23:38 GMT - Server: - - cloudflare - Transfer-Encoding: - - chunked - X-Content-Type-Options: - - nosniff - access-control-expose-headers: - - X-Request-ID - alt-svc: - - h3=":443"; ma=86400 - cf-cache-status: - - DYNAMIC - openai-organization: - - crewai-iuxna1 - openai-processing-ms: - - '1954' - openai-version: - - '2020-10-01' - strict-transport-security: - - max-age=31536000; includeSubDomains; preload - x-ratelimit-limit-requests: - - '30000' - x-ratelimit-limit-tokens: - - '150000000' - x-ratelimit-remaining-requests: - - '29999' - x-ratelimit-remaining-tokens: - - '149999978' - x-ratelimit-reset-requests: - - 2ms - x-ratelimit-reset-tokens: - - 0s - x-request-id: - - req_ea2703502b8827e4297cd2a7bae9d9c8 - status: - code: 200 - message: OK -version: 1 diff --git a/lib/crewai/tests/events/test_llm_finish_reason_response_id.py b/lib/crewai/tests/events/test_llm_finish_reason_response_id.py index 091875fdf..5a1d4892d 100644 --- a/lib/crewai/tests/events/test_llm_finish_reason_response_id.py +++ b/lib/crewai/tests/events/test_llm_finish_reason_response_id.py @@ -4,7 +4,7 @@ from unittest.mock import MagicMock, patch import pytest -from crewai.events.event_bus import CrewAIEventsBus +from crewai.events.event_bus import crewai_event_bus from crewai.events.types.llm_events import ( LLMCallCompletedEvent, LLMCallStartedEvent, @@ -31,7 +31,7 @@ class _StubLLM(BaseLLM): @pytest.fixture def mock_emit(): - with patch.object(CrewAIEventsBus, "emit") as mock: + with patch.object(crewai_event_bus, "emit") as mock: yield mock diff --git a/lib/crewai/tests/events/test_llm_usage_event.py b/lib/crewai/tests/events/test_llm_usage_event.py index 0a8ffc69f..4e8f93461 100644 --- a/lib/crewai/tests/events/test_llm_usage_event.py +++ b/lib/crewai/tests/events/test_llm_usage_event.py @@ -4,7 +4,6 @@ from unittest.mock import patch import pytest from pydantic import BaseModel -from crewai.events.event_bus import CrewAIEventsBus from crewai.events.types.llm_events import LLMCallCompletedEvent, LLMCallType from crewai.llm import LLM from crewai.llms.base_llm import BaseLLM @@ -203,7 +202,9 @@ class _StubLLM(BaseLLM): class TestEmitCallCompletedEventPassesUsage: @pytest.fixture def mock_emit(self): - with patch.object(CrewAIEventsBus, "emit") as mock: + from crewai.events.event_bus import crewai_event_bus + + with patch.object(crewai_event_bus, "emit") as mock: yield mock @pytest.fixture diff --git a/lib/crewai/tests/llms/google/test_google.py b/lib/crewai/tests/llms/google/test_google.py index 0213eb525..2c03dcd23 100644 --- a/lib/crewai/tests/llms/google/test_google.py +++ b/lib/crewai/tests/llms/google/test_google.py @@ -123,12 +123,12 @@ def test_gemini_completion_initialization_parameters(): def test_gemini_started_event_surfaces_max_output_tokens(): - from crewai.events.event_bus import CrewAIEventsBus + from crewai.events.event_bus import crewai_event_bus from crewai.events.types.llm_events import LLMCallStartedEvent llm = LLM(model="google/gemini-2.0-flash-001", max_output_tokens=2000, api_key="test-key") - with patch.object(CrewAIEventsBus, "emit") as mock_emit: + with patch.object(crewai_event_bus, "emit") as mock_emit: llm._emit_call_started_event(messages="hi") event = mock_emit.call_args[1]["event"] diff --git a/lib/crewai/tests/llms/test_tool_call_streaming.py b/lib/crewai/tests/llms/test_tool_call_streaming.py index 03bc0fd9b..54cc2fd1d 100644 --- a/lib/crewai/tests/llms/test_tool_call_streaming.py +++ b/lib/crewai/tests/llms/test_tool_call_streaming.py @@ -38,18 +38,10 @@ def get_temperature_tool_schema() -> dict[str, Any]: @pytest.fixture def mock_emit() -> MagicMock: - """Mock the singleton event bus emit used by LLM providers. + from crewai.events.event_bus import crewai_event_bus - Patch the singleton instance (not only the class) so a leftover - instance-level ``emit`` from other tests cannot shadow the mock. - """ - from crewai.events.event_bus import CrewAIEventsBus, crewai_event_bus - - with ( - patch.object(CrewAIEventsBus, "emit") as class_mock, - patch.object(crewai_event_bus, "emit", new=class_mock), - ): - yield class_mock + with patch.object(crewai_event_bus, "emit") as mock: + yield mock def _event_from_emit_call(call: Any) -> Any: diff --git a/lib/crewai/tests/test_llm.py b/lib/crewai/tests/test_llm.py index 1c98d751e..7e60b18c4 100644 --- a/lib/crewai/tests/test_llm.py +++ b/lib/crewai/tests/test_llm.py @@ -534,9 +534,9 @@ def assert_event_count( @pytest.fixture def mock_emit() -> MagicMock: - from crewai.events.event_bus import CrewAIEventsBus + from crewai.events.event_bus import crewai_event_bus - with patch.object(CrewAIEventsBus, "emit") as mock_emit: + with patch.object(crewai_event_bus, "emit") as mock_emit: yield mock_emit diff --git a/lib/crewai/tests/test_llm_streaming_finish_reason.py b/lib/crewai/tests/test_llm_streaming_finish_reason.py index ff8a94d4e..c90fe77a4 100644 --- a/lib/crewai/tests/test_llm_streaming_finish_reason.py +++ b/lib/crewai/tests/test_llm_streaming_finish_reason.py @@ -11,14 +11,14 @@ from unittest.mock import patch import pytest -from crewai.events.event_bus import CrewAIEventsBus +from crewai.events.event_bus import crewai_event_bus from crewai.events.types.llm_events import LLMCallCompletedEvent from crewai.llm import LLM @pytest.fixture def mock_emit(): - with patch.object(CrewAIEventsBus, "emit") as mock: + with patch.object(crewai_event_bus, "emit") as mock: yield mock diff --git a/lib/crewai/tests/utilities/test_events.py b/lib/crewai/tests/utilities/test_events.py index d9c9d5529..a5e22fe81 100644 --- a/lib/crewai/tests/utilities/test_events.py +++ b/lib/crewai/tests/utilities/test_events.py @@ -1244,7 +1244,6 @@ def test_llm_completed_event_includes_usage(): assert event.usage.get("total_tokens", 0) > 0 -@pytest.mark.vcr() def test_llm_emits_call_failed_event(): received_events = [] event_received = threading.Event() @@ -1256,12 +1255,10 @@ def test_llm_emits_call_failed_event(): error_message = "OpenAI API call failed: Simulated API failure" - with patch( - "crewai.llms.providers.openai.completion.OpenAICompletion._handle_completion" - ) as mock_handle_completion: - mock_handle_completion.side_effect = Exception("Simulated API failure") - - llm = LLM(model="gpt-4o-mini") + llm = LLM(model="gpt-4o-mini") + with patch.object( + llm, "_handle_completion", side_effect=Exception("Simulated API failure") + ): with pytest.raises(Exception) as exc_info: llm.call("Hello, how are you?")