Skip to content

BUG: FinalAnswerStep callbacks not executed #1879

@victorlearned

Description

@victorlearned

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:

  1. _finalize_step() method signature only accepts ActionStep | PlanningStep, not FinalAnswerStep
  2. In _run_stream(), FinalAnswerStep is created and yielded directly without calling _finalize_step() on it

Steps to reproduce

  1. Create a CodeAgent with a callback registered for FinalAnswerStep
  2. Call agent.run() (non-streaming mode)
  3. 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 callback

Expected 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 smolagents

Additional context (optional)

Root Cause Analysis:

  1. Location 1: _finalize_step() method (line 572 in agents.py)

    • Current signature: def _finalize_step(self, memory_step: ActionStep | PlanningStep)
    • Issue: FinalAnswerStep is not included in the type hint, though the method would work if called
  2. Location 2: _run_stream() method (line 563 in agents.py)

    • Current code:
      yield FinalAnswerStep(handle_agent_output_types(final_answer))
    • Issue: FinalAnswerStep is created and yielded directly without calling _finalize_step()
  3. Comparison with other step types:

    • ActionStep is finalized at line 555: self._finalize_step(action_step)
    • PlanningStep is finalized at line 520: self._finalize_step(planning_step)
    • FinalAnswerStep is never finalized

Proposed Fix:

  1. Update _finalize_step() signature to accept FinalAnswerStep:

    def _finalize_step(self, memory_step: ActionStep | PlanningStep | FinalAnswerStep):
  2. Finalize FinalAnswerStep before 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:563
  • run() 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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions