Skip to content

Commit fc9e410

Browse files
fix: improve handoff robustness and fix test import
- Enhanced callback invocation logic to handle keyword-only args and defaults - Added error handling for input_type instantiation failures - Fixed remove_all_tools filter to check truthy values instead of key existence - Corrected line continuation indentation in test file Co-authored-by: Mervin Praison <[email protected]>
1 parent b84f985 commit fc9e410

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

src/praisonai-agents/praisonaiagents/agent/handoff.py

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -102,26 +102,39 @@ def handoff_tool(**kwargs):
102102
try:
103103
# Execute on_handoff callback if provided
104104
if self.on_handoff:
105-
sig = inspect.signature(self.on_handoff)
106-
num_params = len(sig.parameters)
107-
108-
if num_params == 0:
109-
self.on_handoff()
110-
elif num_params == 1:
111-
self.on_handoff(source_agent)
112-
elif num_params == 2:
113-
if self.input_type:
114-
input_data = self.input_type(**kwargs) if kwargs else self.input_type()
115-
self.on_handoff(source_agent, input_data)
105+
try:
106+
sig = inspect.signature(self.on_handoff)
107+
# Get parameters excluding those with defaults and varargs/varkwargs
108+
required_params = [
109+
p for p in sig.parameters.values()
110+
if p.default == inspect.Parameter.empty
111+
and p.kind not in (p.VAR_POSITIONAL, p.VAR_KEYWORD)
112+
]
113+
num_required = len(required_params)
114+
115+
if num_required == 0:
116+
self.on_handoff()
117+
elif num_required == 1:
118+
self.on_handoff(source_agent)
119+
elif num_required == 2:
120+
if self.input_type and kwargs:
121+
try:
122+
input_data = self.input_type(**kwargs)
123+
self.on_handoff(source_agent, input_data)
124+
except TypeError as e:
125+
logger.error(f"Failed to create input_type instance: {e}")
126+
self.on_handoff(source_agent, kwargs)
127+
else:
128+
# No input_type or no kwargs: pass raw kwargs or empty dict
129+
self.on_handoff(source_agent, kwargs or {})
116130
else:
117-
# Callback expects 2 params but no input_type provided
118-
logger.warning(f"Callback {self.on_handoff.__name__} expects 2 parameters but no input_type was provided")
119-
self.on_handoff(source_agent, None)
120-
else:
121-
# Callback has unexpected number of parameters
122-
logger.warning(f"Callback {self.on_handoff.__name__} has {num_params} parameters, expected 0, 1, or 2")
123-
# Try calling with source_agent only
124-
self.on_handoff(source_agent)
131+
raise ValueError(
132+
f"Callback {self.on_handoff.__name__} requires {num_required} parameters, "
133+
"but only 0-2 are supported"
134+
)
135+
except Exception as e:
136+
logger.error(f"Error invoking callback {self.on_handoff.__name__}: {e}")
137+
# Continue with handoff even if callback fails
125138

126139
# Prepare handoff data
127140
handoff_data = HandoffInputData(
@@ -243,7 +256,7 @@ def remove_all_tools(data: HandoffInputData) -> HandoffInputData:
243256
"""Remove all tool calls from the message history."""
244257
filtered_messages = []
245258
for msg in data.messages:
246-
if isinstance(msg, dict) and ('tool_calls' in msg or msg.get('role') == 'tool'):
259+
if isinstance(msg, dict) and (msg.get('tool_calls') or msg.get('role') == 'tool'):
247260
# Skip messages with tool calls
248261
continue
249262
filtered_messages.append(msg)

src/praisonai-agents/tests/test_handoff_compatibility.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ def test_handoff_filters():
139139
data = HandoffInputData(messages=list(messages))
140140
filtered = handoff_filters.remove_all_tools(data)
141141
assert len(filtered.messages) == 3
142-
assert all(msg.get("role") not in ["tool"] and "tool_calls" not in msg
143-
for msg in filtered.messages if isinstance(msg, dict))
142+
assert all(msg.get("role") not in ["tool"] and "tool_calls" not in msg
143+
for msg in filtered.messages if isinstance(msg, dict))
144144

145145
# Test keep_last_n_messages filter
146146
data = HandoffInputData(messages=list(messages))

0 commit comments

Comments
 (0)