Skip to content

Commit 3443f73

Browse files
fix: harden agent invoke execution path and logger init order
Agent-Logs-Url: https://github.com/MervinPraison/PraisonAI/sessions/d404f2be-8ead-4751-95b1-4525d1d1186b Co-authored-by: MervinPraison <454862+MervinPraison@users.noreply.github.com>
1 parent 0ca1929 commit 3443f73

3 files changed

Lines changed: 23 additions & 12 deletions

File tree

src/praisonai/praisonai/api/agent_invoke.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77

88
from typing import Any, Dict, Optional, Union
9+
import inspect
910
import logging
1011

1112
try:
@@ -94,6 +95,12 @@ def list_registered_agents() -> list:
9495
return list(_agent_registry.keys())
9596

9697

98+
def _supports_async_start(agent: Any) -> bool:
99+
"""Return True when agent exposes a real async astart method."""
100+
astart = getattr(agent, "astart", None)
101+
return callable(astart) and inspect.iscoroutinefunction(astart)
102+
103+
97104
# FastAPI Router (if FastAPI is available)
98105
if FASTAPI_AVAILABLE and APIRouter is not None:
99106
router = APIRouter(prefix="/api/v1", tags=["agents"])
@@ -151,12 +158,14 @@ async def invoke_agent(
151158
logger.debug(f"Agent config overrides provided: {request.agent_config}")
152159

153160
# Invoke agent (handle both sync and async agents)
154-
if hasattr(agent, 'astart'):
161+
if _supports_async_start(agent):
155162
# Async agent
156163
result = await agent.astart(request.message)
157-
else:
164+
elif hasattr(agent, "start") and callable(agent.start):
158165
# Sync agent (use start method)
159166
result = agent.start(request.message)
167+
else:
168+
raise AttributeError("Agent must provide start() or async astart()")
160169

161170
logger.info(f"Agent {agent_id} invoked successfully")
162171

@@ -287,10 +296,12 @@ async def invoke_agent_standalone(
287296
# Invoke agent
288297
session_id = session_id or "default"
289298

290-
if hasattr(agent, 'astart'):
299+
if _supports_async_start(agent):
291300
result = await agent.astart(message)
292-
else:
301+
elif hasattr(agent, "start") and callable(agent.start):
293302
result = agent.start(message)
303+
else:
304+
raise AttributeError("Agent must provide start() or async astart()")
294305

295306
return {
296307
"result": str(result),
@@ -363,4 +374,4 @@ async def test_standalone():
363374
print(f"Agent response: {result}")
364375

365376
# asyncio.run(test_standalone())
366-
print("Agent Invoke API module loaded successfully")
377+
print("Agent Invoke API module loaded successfully")

src/praisonai/praisonai/api/call.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@
5050

5151
app = FastAPI()
5252

53+
# Set up logging
54+
logger = logging.getLogger(__name__)
55+
log_level = os.getenv("LOGLEVEL", "INFO").upper()
56+
logger.handlers = []
57+
5358
# Include agent invoke router for n8n integration
5459
try:
5560
from .agent_invoke import router as agent_invoke_router
@@ -58,11 +63,6 @@
5863
except ImportError as e:
5964
logger.warning(f"Could not load agent invoke router: {e}")
6065

61-
# Set up logging
62-
logger = logging.getLogger(__name__)
63-
log_level = os.getenv("LOGLEVEL", "INFO").upper()
64-
logger.handlers = []
65-
6666
# Try to import tools from the root directory
6767
tools = []
6868
tools_path = os.path.join(os.getcwd(), 'tools.py')

src/praisonai/tests/test_n8n_agent_invoke.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def client(self):
189189
app = FastAPI()
190190
app.include_router(router)
191191
return TestClient(app)
192-
except ImportError:
192+
except (ImportError, RuntimeError):
193193
pytest.skip("FastAPI test dependencies not available")
194194

195195
def test_list_agents_endpoint(self, client):
@@ -363,4 +363,4 @@ def test_agent_invoke_smoke_test():
363363
if __name__ == "__main__":
364364
# Run smoke test when executed directly
365365
test_agent_invoke_smoke_test()
366-
print("✅ Agent invoke API smoke test passed")
366+
print("✅ Agent invoke API smoke test passed")

0 commit comments

Comments
 (0)