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.

refactor: update step callback methods to support asynchronous invocation (#4633)

João Moura1bdb9496a32d
diff --git a/lib/crewai-files/src/crewai_files/__init__.py b/lib/crewai-files/src/crewai_files/__init__.py
index c86fc7ba5..24f34e510 100644
--- a/lib/crewai-files/src/crewai_files/__init__.py
+++ b/lib/crewai-files/src/crewai_files/__init__.py
@@ -152,4 +152,4 @@ __all__ = [
"wrap_file_source",
]
-__version__ = "1.10.1b"
+__version__ = "1.10.1b1"
diff --git a/lib/crewai-tools/pyproject.toml b/lib/crewai-tools/pyproject.toml
index cea7986aa..caf460410 100644
--- a/lib/crewai-tools/pyproject.toml
+++ b/lib/crewai-tools/pyproject.toml
@@ -11,7 +11,7 @@ dependencies = [
"pytube~=15.0.0",
"requests~=2.32.5",
"docker~=7.1.0",
- "crewai==1.10.1b",
+ "crewai==1.10.1b1",
"tiktoken~=0.8.0",
"beautifulsoup4~=4.13.4",
"python-docx~=1.2.0",
diff --git a/lib/crewai-tools/src/crewai_tools/__init__.py b/lib/crewai-tools/src/crewai_tools/__init__.py
index 3d176a921..a3c978230 100644
--- a/lib/crewai-tools/src/crewai_tools/__init__.py
+++ b/lib/crewai-tools/src/crewai_tools/__init__.py
@@ -291,4 +291,4 @@ __all__ = [
"ZapierActionTools",
]
-__version__ = "1.10.1b"
+__version__ = "1.10.1b1"
diff --git a/lib/crewai/pyproject.toml b/lib/crewai/pyproject.toml
index a168ee6d9..0401ef193 100644
--- a/lib/crewai/pyproject.toml
+++ b/lib/crewai/pyproject.toml
@@ -53,7 +53,7 @@ Repository = "https://github.com/crewAIInc/crewAI"
[project.optional-dependencies]
tools = [
- "crewai-tools==1.10.1b",
+ "crewai-tools==1.10.1b1",
]
embeddings = [
"tiktoken~=0.8.0"
diff --git a/lib/crewai/src/crewai/__init__.py b/lib/crewai/src/crewai/__init__.py
index 06cb42cf5..da79887a6 100644
--- a/lib/crewai/src/crewai/__init__.py
+++ b/lib/crewai/src/crewai/__init__.py
@@ -40,7 +40,7 @@ def _suppress_pydantic_deprecation_warnings() -> None:
_suppress_pydantic_deprecation_warnings()
-__version__ = "1.10.1b"
+__version__ = "1.10.1b1"
_telemetry_submitted = False
diff --git a/lib/crewai/src/crewai/agents/crew_agent_executor.py b/lib/crewai/src/crewai/agents/crew_agent_executor.py
index 56abaae02..292e9a2d4 100644
--- a/lib/crewai/src/crewai/agents/crew_agent_executor.py
+++ b/lib/crewai/src/crewai/agents/crew_agent_executor.py
@@ -1259,7 +1259,7 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
formatted_answer, tool_result
)
- self._invoke_step_callback(formatted_answer) # type: ignore[arg-type]
+ await self._ainvoke_step_callback(formatted_answer) # type: ignore[arg-type]
self._append_message(formatted_answer.text) # type: ignore[union-attr]
except OutputParserError as e:
@@ -1374,7 +1374,7 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
output=answer,
text=answer,
)
- self._invoke_step_callback(formatted_answer)
+ await self._ainvoke_step_callback(formatted_answer)
self._append_message(answer) # Save final answer to messages
self._show_logs(formatted_answer)
return formatted_answer
@@ -1386,7 +1386,7 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
output=answer,
text=output_json,
)
- self._invoke_step_callback(formatted_answer)
+ await self._ainvoke_step_callback(formatted_answer)
self._append_message(output_json)
self._show_logs(formatted_answer)
return formatted_answer
@@ -1397,7 +1397,7 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
output=str(answer),
text=str(answer),
)
- self._invoke_step_callback(formatted_answer)
+ await self._ainvoke_step_callback(formatted_answer)
self._append_message(str(answer)) # Save final answer to messages
self._show_logs(formatted_answer)
return formatted_answer
@@ -1491,7 +1491,7 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
def _invoke_step_callback(
self, formatted_answer: AgentAction | AgentFinish
) -> None:
- """Invoke step callback.
+ """Invoke step callback (sync context).
Args:
formatted_answer: Current agent response.
@@ -1501,6 +1501,19 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
if inspect.iscoroutine(cb_result):
asyncio.run(cb_result)
+ async def _ainvoke_step_callback(
+ self, formatted_answer: AgentAction | AgentFinish
+ ) -> None:
+ """Invoke step callback (async context).
+
+ Args:
+ formatted_answer: Current agent response.
+ """
+ if self.step_callback:
+ cb_result = self.step_callback(formatted_answer)
+ if inspect.iscoroutine(cb_result):
+ await cb_result
+
def _append_message(
self, text: str, role: Literal["user", "assistant", "system"] = "assistant"
) -> None:
diff --git a/lib/crewai/src/crewai/cli/templates/crew/pyproject.toml b/lib/crewai/src/crewai/cli/templates/crew/pyproject.toml
index a16cb5b36..d06e60cb3 100644
--- a/lib/crewai/src/crewai/cli/templates/crew/pyproject.toml
+++ b/lib/crewai/src/crewai/cli/templates/crew/pyproject.toml
@@ -5,7 +5,7 @@ description = "{{name}} using crewAI"
authors = [{ name = "Your Name", email = "[email protected]" }]
requires-python = ">=3.10,<3.14"
dependencies = [
- "crewai[tools]==1.10.1b"
+ "crewai[tools]==1.10.1b1"
]
[project.scripts]
diff --git a/lib/crewai/src/crewai/cli/templates/flow/pyproject.toml b/lib/crewai/src/crewai/cli/templates/flow/pyproject.toml
index 7ea312ff7..2b1259e79 100644
--- a/lib/crewai/src/crewai/cli/templates/flow/pyproject.toml
+++ b/lib/crewai/src/crewai/cli/templates/flow/pyproject.toml
@@ -5,7 +5,7 @@ description = "{{name}} using crewAI"
authors = [{ name = "Your Name", email = "[email protected]" }]
requires-python = ">=3.10,<3.14"
dependencies = [
- "crewai[tools]==1.10.1b"
+ "crewai[tools]==1.10.1b1"
]
[project.scripts]
diff --git a/lib/crewai/src/crewai/cli/templates/tool/pyproject.toml b/lib/crewai/src/crewai/cli/templates/tool/pyproject.toml
index 84e763056..8f8194e7b 100644
--- a/lib/crewai/src/crewai/cli/templates/tool/pyproject.toml
+++ b/lib/crewai/src/crewai/cli/templates/tool/pyproject.toml
@@ -5,7 +5,7 @@ description = "Power up your crews with {{folder_name}}"
readme = "README.md"
requires-python = ">=3.10,<3.14"
dependencies = [
- "crewai[tools]==1.10.1b"
+ "crewai[tools]==1.10.1b1"
]
[tool.crewai]
diff --git a/lib/crewai/src/crewai/flow/flow.py b/lib/crewai/src/crewai/flow/flow.py
index fe31b46d5..868f52632 100644
--- a/lib/crewai/src/crewai/flow/flow.py
+++ b/lib/crewai/src/crewai/flow/flow.py
@@ -16,7 +16,7 @@ from collections.abc import (
Sequence,
ValuesView,
)
-from concurrent.futures import Future
+from concurrent.futures import Future, ThreadPoolExecutor
import copy
import enum
import inspect
@@ -1739,7 +1739,12 @@ class Flow(Generic[T], metaclass=FlowMeta):
async def _run_flow() -> Any:
return await self.kickoff_async(inputs, input_files)
- return asyncio.run(_run_flow())
+ try:
+ asyncio.get_running_loop()
+ with ThreadPoolExecutor(max_workers=1) as pool:
+ return pool.submit(asyncio.run, _run_flow()).result()
+ except RuntimeError:
+ return asyncio.run(_run_flow())
async def kickoff_async(
self,
diff --git a/lib/devtools/src/crewai_devtools/__init__.py b/lib/devtools/src/crewai_devtools/__init__.py
index 3420c787d..cc024e7b0 100644
--- a/lib/devtools/src/crewai_devtools/__init__.py
+++ b/lib/devtools/src/crewai_devtools/__init__.py
@@ -1,3 +1,3 @@
"""CrewAI development tools."""
-__version__ = "1.10.1b"
+__version__ = "1.10.1b1"
diff --git a/lib/devtools/src/crewai_devtools/cli.py b/lib/devtools/src/crewai_devtools/cli.py
index 9a8330446..32950c39f 100644
--- a/lib/devtools/src/crewai_devtools/cli.py
+++ b/lib/devtools/src/crewai_devtools/cli.py
@@ -200,7 +200,7 @@ def add_docs_version(docs_json_path: Path, version: str) -> bool:
Args:
docs_json_path: Path to docs/docs.json.
- version: Version string (e.g., "1.10.1b").
+ version: Version string (e.g., "1.10.1b1").
Returns:
True if docs.json was updated, False otherwise.