mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-08-10 16:32:28 +00:00
Some checks failed
CodeQL Advanced / Analyze (actions) (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Check Documentation Broken Links / Check broken links (push) Has been cancelled
Vulnerability Scan / Detect changes (push) Has been cancelled
Vulnerability Scan / pip-audit (push) Has been cancelled
Build uv cache / build-cache (3.10) (push) Has been cancelled
Build uv cache / build-cache (3.11) (push) Has been cancelled
Build uv cache / build-cache (3.12) (push) Has been cancelled
Build uv cache / build-cache (3.13) (push) Has been cancelled
* fix: patch the event-bus singleton in usage event tests `TestEmitCallCompletedEventPassesUsage` mocked `CrewAIEventsBus.emit` on the class, but `_emit_call_completed_event` calls the `crewai_event_bus` singleton. Under CI that left the real bus emitting and the mock never seeing a call. Patch the singleton where `base_llm` imports it instead. * fix: harden llm call-failed event test against VCR fallback `test_llm_emits_call_failed_event` marked VCR while mocking `_handle_completion`, and its cassette recorded a successful reply. When the class-level patch missed under xdist, VCR replayed success and the expected exception never fired. Patch the instance method and drop the cassette so the failure path cannot silently succeed. * fix: patch the event-bus singleton instance in emit mocks `patch.object(CrewAIEventsBus, "emit")` misses callers when an instance-level `emit` shadows the class method — common under xdist after other suites touch the singleton. Patch `crewai_event_bus.emit` on the shared instance instead across the LLM event mock fixtures.
97 lines
3.0 KiB
Python
97 lines
3.0 KiB
Python
"""Regression: LiteLLM emits a final usage-only chunk (choices=[]) when
|
|
``stream_options.include_usage`` is set. The old post-loop
|
|
``_extract_finish_reason_and_response_id(last_chunk)`` then silently returned
|
|
(None, None). These tests pin that we capture finish_reason/response_id
|
|
incrementally during the stream loop instead.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
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(crewai_event_bus, "emit") as mock:
|
|
yield mock
|
|
|
|
|
|
def _completed_event(mock_emit) -> LLMCallCompletedEvent:
|
|
matches = [
|
|
call.kwargs["event"]
|
|
for call in mock_emit.call_args_list
|
|
if isinstance(call.kwargs.get("event"), LLMCallCompletedEvent)
|
|
]
|
|
assert matches, "expected an LLMCallCompletedEvent to be emitted"
|
|
assert len(matches) == 1, f"expected one completed event, got {len(matches)}"
|
|
return matches[0]
|
|
|
|
|
|
def _chunks_with_usage_tail() -> list[dict[str, Any]]:
|
|
"""Three-chunk stream mirroring LiteLLM's include_usage behavior:
|
|
two content chunks where the second carries finish_reason="stop",
|
|
then a final usage-only chunk with choices=[]."""
|
|
return [
|
|
{
|
|
"id": "chatcmpl-stream-1",
|
|
"choices": [
|
|
{"delta": {"content": "hi"}, "finish_reason": None}
|
|
],
|
|
},
|
|
{
|
|
"id": "chatcmpl-stream-1",
|
|
"choices": [
|
|
{"delta": {"content": " there"}, "finish_reason": "stop"}
|
|
],
|
|
},
|
|
{
|
|
"id": "chatcmpl-stream-1",
|
|
"choices": [],
|
|
"usage": {
|
|
"prompt_tokens": 1,
|
|
"completion_tokens": 2,
|
|
"total_tokens": 3,
|
|
},
|
|
},
|
|
]
|
|
|
|
|
|
def test_sync_stream_emits_finish_reason_and_response_id_from_loop(mock_emit):
|
|
llm = LLM(model="gpt-4o-mini", is_litellm=True, stream=True)
|
|
|
|
with patch("crewai.llm.litellm.completion", return_value=iter(_chunks_with_usage_tail())):
|
|
result = llm.call("anything")
|
|
|
|
assert result == "hi there"
|
|
|
|
event = _completed_event(mock_emit)
|
|
assert event.finish_reason == "stop"
|
|
assert event.response_id == "chatcmpl-stream-1"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_async_stream_emits_finish_reason_and_response_id_from_loop(mock_emit):
|
|
llm = LLM(model="gpt-4o-mini", is_litellm=True, stream=True)
|
|
|
|
async def _aiter():
|
|
for chunk in _chunks_with_usage_tail():
|
|
yield chunk
|
|
|
|
async def _acompletion(*_args, **_kwargs):
|
|
return _aiter()
|
|
|
|
with patch("crewai.llm.litellm.acompletion", side_effect=_acompletion):
|
|
result = await llm.acall("anything")
|
|
|
|
assert result == "hi there"
|
|
|
|
event = _completed_event(mock_emit)
|
|
assert event.finish_reason == "stop"
|
|
assert event.response_id == "chatcmpl-stream-1"
|