Skip to content

Conversation

@suryabdev
Copy link
Contributor

@suryabdev suryabdev commented Nov 21, 2025

Fix for #1879

There was a request to add FinalAnswerStep to the possible step_callbacks
We have final_answer_checks today that run at the end of a run (Which also receive agent memory as args), But the user said adding non validation logic to a final_answer_check overloaded the function
They also mentioned other reasons for adding it to the step_callbacks (See this issue comment for more details)

Reproduction code

from smolagents import CodeAgent, FinalAnswerStep, ActionStep, InferenceClientModel

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
model = InferenceClientModel()
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?")
print(f"Agent run result: {result}") 

Output

╭─────────────────────────────────────────────────────────────────────────────── New run ───────────────────────────────────────────────────────────────────────────────╮
│                                                                                                                                                                       │
│ What is 2+2?                                                                                                                                                          │
│                                                                                                                                                                       │
╰─ InferenceClientModel - Qwen/Qwen3-Next-80B-A3B-Thinking ─────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ Step 1 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
 ─ Executing parsed code: ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
  result = 2 + 2                                                                                                                                                         
  final_answer(result)                                                                                                                                                   
 ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 
Final answer: 4
[Step 1: Duration 4.42 seconds| Input tokens: 2,031 | Output tokens: 401]
Callback triggered for FinalAnswerStep: 4
Agent run result: MODIFIED BY CALLBACK

@victorlearned
Copy link

Any traction here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants