fix: resolve type-checker errors in cache preload code

- Use explicit type annotation for original_max_tokens in preload_probe
- Use self.__setattr__ to avoid type mismatch with subclass fields
- Replace hasattr checks with isinstance(agent.llm, BaseLLM) for proper
  type narrowing
- Ensure _get_agent_system_prompt returns str without Any leak

Co-Authored-By: João <joao@crewai.com>
This commit is contained in:
Devin AI
2026-05-25 07:10:03 +00:00
parent 2b60f3df16
commit 7df2e54749
2 changed files with 16 additions and 10 deletions

View File

@@ -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")

View File

@@ -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: