All Agent Engineering

smolagents final-answer checks: accept against a known task contract

Define a boolean callback receiving answer, memory and agent, inspect synthetic counterexamples, then integrate it into an existing Agent.

AINavWritten / Updated
On this page

At a glance

smolagents final_answer_checks validates before final-answer acceptance. Each callback receives the answer, memory and Agent and returns a boolean. For synthetic quantities 7 and 11, this task requires JSON text with only total_units, an integer equal to 18. It delivers a deterministic check, integration settings and counterexample sheet. This validates known criteria for this task; it is not general factual review. No Agent or model was run.

Runtime and applicability

Article type
Engineering practice
Verified
Applies to
  • smolagents Final acceptance against known task evidence

Execution locations and responsibilities

  • Local validator in the Python caller Compute ground truth and check types, fields and values.
  • Configured smolagents Agent and downstream gate Integrate checks, record actual failures and block invalid results.

When to use

  • An application has independently computable acceptance evidence and needs final output to meet explicit types and values before handoff.

When not to use

  • Research without reliable ground truth cannot use a fixed-number check. Valid JSON alone does not establish facts.
  • Final checks cannot reverse earlier external writes or replace tool permissions and execution isolation.

Derive acceptance from the task input

Input is the synthetic list [7, 11]. The application computes the expected total rather than adopting the model’s claim as ground truth. Output must be JSON text containing only integer total_units. Python bool subclasses int, so type(...) is int excludes booleans. memory and agent preserve the documented signature but are unused here. Memory-based acceptance needs its own reliable record source.

Example: local validator without a model call
python
import json

EXPECTED_TOTAL = sum([7, 11])


def check_total(answer, memory, agent) -> bool:
    if not isinstance(answer, str):
        return False
    try:
        value = json.loads(answer)
    except (TypeError, ValueError):
        return False
    return (
        isinstance(value, dict)
        and set(value) == {"total_units"}
        and type(value["total_units"]) is int
        and value["total_units"] == EXPECTED_TOTAL
    )

Inspect valid cases and counterexamples

Local callback acceptance
Synthetic answerExpected booleanReason
{"total_units":18}trueOnly field, integer, equal to the input sum
{"total_units":19}falseWrong known total
{"total_units":"18"}falseString is not integer
{"total_units":18,"ok":true}falseExtra field
{"total_units":true}falseBoolean is rejected
Plain text or non-string objectfalseWrong JSON-text contract

Call check_total directly with these fixtures, using None for memory and agent in local checks. That exercises only the owned function, not smolagents or models. Expectations come from the contract. Verify it before integration rather than after tools have written to external systems.

Configure an existing caller

The execution location is an existing Python application with model, tools and check_total already defined. This is an integration snippet, not an installation script. ToolCallingAgent accepts the base final_answer_checks parameter documented with its three-argument boolean contract. max_steps=4 is a teaching cap, not a guarantee of completion or an automatic retry promise. Checking the local function does not require running a model.

Example: integration with existing model and tools
python
from smolagents import ToolCallingAgent

# model and tools are already configured by the calling application.
agent = ToolCallingAgent(
    model=model,
    tools=tools,
    max_steps=4,
    final_answer_checks=[check_total],
)

Check the downstream exit too

The reference places checks before acceptance but does not fully define version-specific behavior after false returns or exceptions. Do not assume an error code, retry count or tool-stop behavior. Confirm it in the actual version and record run status, final answer and validation result. Failed content must not enter downstream consumption. Missing returns, exceptions and step-limit outcomes need separate branches.

Separate validation status from business success

  • A false result records a type, field or value mismatch and blocks acceptance.
  • A callback or Agent exception retains the error and existing memory; a log excerpt is not a final answer.
  • At max_steps, inspect actual version behavior rather than fabricate a pass.
  • Cancellation or observation timeout ends waiting and consumption, retaining available records. External tool effects need separate handling.
  • Local callback success without Agent verification remains a local contract check, not a model or product test.

Verification and extension scope

The official reference was read for final_answer_checks, run and Agent parameters. Code and fixtures are original materials without real accounts or business data. smolagents was not installed and no model or tool was executed. Extend acceptance only with reliable task evidence; citations, dates and sources need their own inputs rather than a prompt promising correctness.

Sources

All links are official pages; verification dates are listed per source, and links point to the version checked.

  1. Hugging Face · Agents reference — final_answer_checks

Found an error? Tell us →