diff --git a/lib/crewai/src/crewai/crew.py b/lib/crewai/src/crewai/crew.py index 2029c3858..743ca407c 100644 --- a/lib/crewai/src/crewai/crew.py +++ b/lib/crewai/src/crewai/crew.py @@ -1079,7 +1079,11 @@ class Crew(FlowTrackable, BaseModel): response_template=getattr(agent, "response_template", None), ).task_execution() - return prompt_result.get("system", "") or prompt_result.get("prompt", "") + system: str = prompt_result.get("system", "") or "" + if system: + return system + prompt: str = prompt_result.get("prompt", "") or "" + return prompt @staticmethod def _common_prefix(strings: list[str]) -> str: @@ -1132,13 +1136,11 @@ class Crew(FlowTrackable, BaseModel): "warming shared prefix first", ) first_agent, _ = agent_prompts[0] - if hasattr(first_agent, "llm") and hasattr( - first_agent.llm, "preload_probe" - ): + if isinstance(first_agent.llm, BaseLLM): first_agent.llm.preload_probe(prefix) for agent, prompt in agent_prompts: - if hasattr(agent, "llm") and hasattr(agent.llm, "preload_probe"): + if isinstance(agent.llm, BaseLLM): agent.llm.preload_probe(prompt) return self._logger.log( @@ -1152,14 +1154,14 @@ class Crew(FlowTrackable, BaseModel): with ThreadPoolExecutor(max_workers=min(len(agent_prompts), 4)) as pool: futures = [] for agent, prompt in agent_prompts: - if hasattr(agent, "llm") and hasattr(agent.llm, "preload_probe"): + if isinstance(agent.llm, BaseLLM): futures.append(pool.submit(agent.llm.preload_probe, prompt)) for f in futures: f.result() elif strategy == "sequential": for agent, prompt in agent_prompts: - if hasattr(agent, "llm") and hasattr(agent.llm, "preload_probe"): + if isinstance(agent.llm, BaseLLM): agent.llm.preload_probe(prompt) self._logger.log("info", "Cache preload: done") diff --git a/lib/crewai/src/crewai/llms/base_llm.py b/lib/crewai/src/crewai/llms/base_llm.py index 681ad3ec6..8f483afeb 100644 --- a/lib/crewai/src/crewai/llms/base_llm.py +++ b/lib/crewai/src/crewai/llms/base_llm.py @@ -389,10 +389,14 @@ class BaseLLM(BaseModel, ABC): Args: system_prompt: The full system prompt to warm. """ - original_max_tokens = getattr(self, "max_tokens", None) + original_max_tokens: int | float | None = getattr(self, "max_tokens", None) original_temperature = self.temperature try: - self.max_tokens = 1 + # Temporarily override for the probe call. We go through the + # custom __setattr__ that BaseLLM already provides so that + # subclass fields (max_tokens, temperature) are set correctly + # even if they are not declared on BaseLLM itself. + self.__setattr__("max_tokens", 1) self.temperature = 0 self.call( messages=[ @@ -403,7 +407,7 @@ class BaseLLM(BaseModel, ABC): except Exception as exc: logger.warning("Cache preload probe failed: %s", exc) finally: - self.max_tokens = original_max_tokens + self.__setattr__("max_tokens", original_max_tokens) self.temperature = original_temperature def _supports_stop_words_implementation(self) -> bool: