crewAIInc / crewAI

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.
https://crewai.com
MIT License
19.09k stars 2.64k forks source link

Agent code overwrites the LLM callbacks #408

Closed rakeshgohel01 closed 2 weeks ago

rakeshgohel01 commented 5 months ago

Issue

When I supplied callbacks directly to LLM object before passing to Agent constructor, the callbacks set by me were never called

Explanation

If an LLM is created with callbacks and assigned to agent, it gets overwritten in agent initialization when pydantic finishes validation.

code

@model_validator(mode="after")
def set_agent_executor(self) -> "Agent":
    """set agent executor is set."""
    if hasattr(self.llm, "model_name"):
        self.llm.callbacks = [ **//THIS OVERRIDES THE LLM CALLBACKS**
            TokenCalcHandler(self.llm.model_name, self._token_process)
        ]
    if not self.agent_executor:
        self.set_cache_handler(self.cache_handler)
    return self

Ideally it should append TokenCalcHandler to callbacks not assigned

github-actions[bot] commented 3 weeks ago

This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] commented 2 weeks ago

This issue was closed because it has been stalled for 5 days with no activity.