-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Description
Problem
When using CodeAgent.run() with step_callbacks registered for FinalAnswerStep, the callbacks are never executed. This occurs because FinalAnswerStep is never passed to _finalize_step(), which is responsible for invoking step callbacks.
The issue is in two places:
_finalize_step()method signature only acceptsActionStep | PlanningStep, notFinalAnswerStep- In
_run_stream(),FinalAnswerStepis created and yielded directly without calling_finalize_step()on it
Steps to reproduce
- Create a
CodeAgentwith a callback registered forFinalAnswerStep - Call
agent.run()(non-streaming mode) - Observe that the callback is never executed
from smolagents import CodeAgent, FinalAnswerStep, ActionStep
def test_final_answer_callback(step: FinalAnswerStep, agent):
"""Callback that should be triggered when FinalAnswerStep is created."""
print(f"Callback triggered for FinalAnswerStep: {step.output}")
step.output = "MODIFIED BY CALLBACK"
# Create agent with step_callbacks
agent = CodeAgent(
tools=[],
model=model, # your model instance
step_callbacks={FinalAnswerStep: test_final_answer_callback},
)
# Run the agent
result = agent.run("What is 2+2?")
# The callback should have modified the output, but it doesn't
# result will be the original output, not "MODIFIED BY CALLBACK"Actual behavior and error logs
The callback registered for FinalAnswerStep is never executed. No errors are raised, but the callback simply doesn't run.
# No errors, but callback is silent
# The output remains unchanged by the callbackExpected behavior
When FinalAnswerStep is created in _run_stream(), it should be passed to _finalize_step() before being yielded, which would trigger any callbacks registered for FinalAnswerStep. The callback should execute and be able to modify the step if needed.
Environment:
Please complete the following information:
- OS: macOS (darwin 24.6.0)
- Python version: 3.11
- Package version: (output of
pip show smolagents)
# Please run: pip show smolagentsAdditional context (optional)
Root Cause Analysis:
-
Location 1:
_finalize_step()method (line 572 inagents.py)- Current signature:
def _finalize_step(self, memory_step: ActionStep | PlanningStep) - Issue:
FinalAnswerStepis not included in the type hint, though the method would work if called
- Current signature:
-
Location 2:
_run_stream()method (line 563 inagents.py)- Current code:
yield FinalAnswerStep(handle_agent_output_types(final_answer))
- Issue:
FinalAnswerStepis created and yielded directly without calling_finalize_step()
- Current code:
-
Comparison with other step types:
ActionStepis finalized at line 555:self._finalize_step(action_step)PlanningStepis finalized at line 520:self._finalize_step(planning_step)FinalAnswerStepis never finalized
Proposed Fix:
-
Update
_finalize_step()signature to acceptFinalAnswerStep:def _finalize_step(self, memory_step: ActionStep | PlanningStep | FinalAnswerStep):
-
Finalize
FinalAnswerStepbefore yielding in_run_stream():final_answer_step = FinalAnswerStep(handle_agent_output_types(final_answer)) self._finalize_step(final_answer_step) yield final_answer_step
Code References:
_finalize_step():.venv/lib/python3.11/site-packages/smolagents/agents.py:572-574_run_stream()FinalAnswerStep creation:.venv/lib/python3.11/site-packages/smolagents/agents.py:563run()method that calls_run_stream():.venv/lib/python3.11/site-packages/smolagents/agents.py:456-492
Checklist
- I have searched the existing issues and have not found a similar bug report.
- I have provided a minimal, reproducible example.
- I have provided the full traceback of the error.
- I have provided my environment details.
- I am willing to work on this issue and submit a pull request. (optional)