Skip to content

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.

perf: optimize string operations with partition() over split()[0] (#3255)

633WHU7dc86dc79a51
diff --git a/src/crewai/llm.py b/src/crewai/llm.py
index 42c56f6e7..9296c26de 100644
--- a/src/crewai/llm.py
+++ b/src/crewai/llm.py
@@ -1100,7 +1100,7 @@ class LLM(BaseLLM):
- If there is no '/', defaults to "openai".
"""
if "/" in self.model:
- return self.model.split("/")[0]
+ return self.model.partition("/")[0]
return None
def _validate_call_params(self) -> None:
diff --git a/src/crewai/utilities/agent_utils.py b/src/crewai/utilities/agent_utils.py
index 40fa5ea07..4289e85a8 100644
--- a/src/crewai/utilities/agent_utils.py
+++ b/src/crewai/utilities/agent_utils.py
@@ -400,7 +400,7 @@ def show_agent_logs(
if not verbose:
return
- agent_role = agent_role.split("\n")[0]
+ agent_role = agent_role.partition("\n")[0]
if formatted_answer is None:
# Start logs
diff --git a/src/crewai/utilities/events/utils/console_formatter.py b/src/crewai/utilities/events/utils/console_formatter.py
index 3088603bb..5039aa1be 100644
--- a/src/crewai/utilities/events/utils/console_formatter.py
+++ b/src/crewai/utilities/events/utils/console_formatter.py
@@ -1321,7 +1321,7 @@ class ConsoleFormatter:
if not verbose:
return
- agent_role = agent_role.split("\n")[0]
+ agent_role = agent_role.partition("\n")[0]
# Create panel content
content = Text()
@@ -1356,7 +1356,7 @@ class ConsoleFormatter:
import json
import re
- agent_role = agent_role.split("\n")[0]
+ agent_role = agent_role.partition("\n")[0]
if isinstance(formatted_answer, AgentAction):
thought = re.sub(r"\n+", "\n", formatted_answer.thought)
diff --git a/src/crewai/utilities/llm_utils.py b/src/crewai/utilities/llm_utils.py
index 1eb0a4693..3998a9bce 100644
--- a/src/crewai/utilities/llm_utils.py
+++ b/src/crewai/utilities/llm_utils.py
@@ -148,7 +148,7 @@ def _llm_via_environment_or_fallback() -> Optional[LLM]:
"AWS_SECRET_ACCESS_KEY",
"AWS_REGION_NAME",
]
- set_provider = model_name.split("/")[0] if "/" in model_name else "openai"
+ set_provider = model_name.partition("/")[0] if "/" in model_name else "openai"
if set_provider in ENV_VARS:
env_vars_for_provider = ENV_VARS[set_provider]