mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-05-06 01:32:36 +00:00
fix: preserve explicit name inside @crew-decorated methods
This commit is contained in:
@@ -238,7 +238,11 @@ def crew(
|
||||
|
||||
crew_instance: Crew = _call_method(meth, self, *args, **kwargs)
|
||||
|
||||
crew_instance.name = getattr(self, "_crew_name", None) or crew_instance.name
|
||||
# Override only when the Crew's name is the auto-resolved class-name
|
||||
# fallback, so an explicit `Crew(name=...)` inside the factory wins.
|
||||
crewbase_name = getattr(self, "_crew_name", None)
|
||||
if crewbase_name and crew_instance.name == type(crew_instance).__name__:
|
||||
crew_instance.name = crewbase_name
|
||||
|
||||
def callback_wrapper(
|
||||
hook: Callable[Concatenate[CrewInstance, P2], R2], instance: CrewInstance
|
||||
|
||||
@@ -268,6 +268,31 @@ def test_crew_decorator_propagates_class_name_to_instance():
|
||||
assert crew_instance.name == "InternalCrew"
|
||||
|
||||
|
||||
def test_crew_decorator_preserves_explicit_name():
|
||||
"""Explicit Crew(name=...) inside @crew should win over the @CrewBase class name."""
|
||||
sample_agent = Agent(role="r", goal="g", backstory="b")
|
||||
sample_task = Task(description="d", expected_output="o", agent=sample_agent)
|
||||
|
||||
@CrewBase
|
||||
class NamedCrewFactory:
|
||||
agents_config = None
|
||||
tasks_config = None
|
||||
agents: list[BaseAgent] = [sample_agent]
|
||||
tasks: list[Task] = [sample_task]
|
||||
|
||||
@crew
|
||||
def crew(self):
|
||||
return Crew(
|
||||
name="My Explicit Name",
|
||||
agents=[sample_agent],
|
||||
tasks=[sample_task],
|
||||
)
|
||||
|
||||
factory_cls = cast(type[Any], NamedCrewFactory)
|
||||
crew_instance: Crew = cast(Any, factory_cls()).crew()
|
||||
assert crew_instance.name == "My Explicit Name"
|
||||
|
||||
|
||||
@tool
|
||||
def simple_tool():
|
||||
"""Return 'Hi!'"""
|
||||
|
||||
Reference in New Issue
Block a user