|
| 1 | +"""Tests for CrewAI LLM conversion across supported crewai versions. |
| 2 | +
|
| 3 | +Regression coverage for LE-2092: with crewai >= 1.0, ``crewai.LLM(...)`` is a |
| 4 | +factory that returns provider-specific subclasses of ``BaseLLM`` (for example |
| 5 | +``OpenAICompletion``) which are *not* instances of ``crewai.LLM``. A guard that |
| 6 | +tests ``isinstance(llm, LLM)`` therefore stops being idempotent, and the second |
| 7 | +conversion pass performed by ``BaseCrewComponent.get_tasks_and_agents`` falls |
| 8 | +through to the LangChain branch and raises |
| 9 | +``AttributeError: 'OpenAICompletion' object has no attribute 'get_lc_namespace'``. |
| 10 | +
|
| 11 | +crewai is an optional dependency that is not installable in the default |
| 12 | +workspace (documented httpx conflict), so these tests skip when it is absent. |
| 13 | +They were run against crewai 0.126.0, 0.134.0 and 1.15.10. |
| 14 | +""" |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +pytest.importorskip("crewai", reason="crewai is an optional dependency") |
| 19 | + |
| 20 | +from crewai import BaseLLM |
| 21 | +from lfx.base.agents.crewai.crew import convert_llm |
| 22 | + |
| 23 | +API_KEY = "sk-component-level-key" |
| 24 | +MODEL_NAME = "gpt-4o-mini" |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture |
| 28 | +def langchain_model(): |
| 29 | + langchain_openai = pytest.importorskip("langchain_openai") |
| 30 | + return langchain_openai.ChatOpenAI(model=MODEL_NAME, api_key=API_KEY) |
| 31 | + |
| 32 | + |
| 33 | +def test_should_build_crewai_llm_when_given_langchain_model(langchain_model): |
| 34 | + # Act |
| 35 | + converted = convert_llm(langchain_model) |
| 36 | + |
| 37 | + # Assert |
| 38 | + assert isinstance(converted, BaseLLM) |
| 39 | + assert converted.api_key == API_KEY |
| 40 | + |
| 41 | + |
| 42 | +def test_should_return_same_object_when_llm_is_already_crewai_native(langchain_model): |
| 43 | + """The crew re-converts every agent LLM, so conversion must be idempotent.""" |
| 44 | + # Arrange |
| 45 | + converted = convert_llm(langchain_model) |
| 46 | + |
| 47 | + # Act |
| 48 | + reconverted = convert_llm(converted) |
| 49 | + |
| 50 | + # Assert |
| 51 | + assert reconverted is converted |
| 52 | + assert reconverted.api_key == API_KEY |
| 53 | + |
| 54 | + |
| 55 | +def test_should_preserve_agent_llm_when_crew_reconverts_it(langchain_model): |
| 56 | + """Reproduces the Sequential Task Agent -> Sequential Crew failure path.""" |
| 57 | + # Arrange |
| 58 | + from crewai import Agent |
| 59 | + from lfx.components.crewai.sequential_crew import SequentialCrewComponent |
| 60 | + |
| 61 | + agent = Agent( |
| 62 | + role="Researcher", |
| 63 | + goal="Research the topic", |
| 64 | + backstory="An experienced researcher", |
| 65 | + llm=convert_llm(langchain_model), |
| 66 | + ) |
| 67 | + crew_component = SequentialCrewComponent() |
| 68 | + |
| 69 | + # Act |
| 70 | + _tasks, agents = crew_component.get_tasks_and_agents(agents_list=[agent]) |
| 71 | + |
| 72 | + # Assert |
| 73 | + assert isinstance(agents[0].llm, BaseLLM) |
| 74 | + assert agents[0].llm.api_key == API_KEY |
| 75 | + |
| 76 | + |
| 77 | +def test_should_return_none_when_llm_is_missing(): |
| 78 | + assert convert_llm(None) is None |
0 commit comments