You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The CodeAgent sometimes generates content for the final_answer tool call directly within a code block, without strictly adhering to the intended workflow of calculating results, printing them, and then assembling the final answer based only on the resulting Observation history from previous steps. This leads to hallucinated or unverified final results, even when strong prompt rules are in place.
Scenario:
A task is given to CodeAgent that requires multi-step data loading, calculation, or analysis.
The expected final output is a structured dictionary or formatted string passed to final_answer.
The CodeAgent's system prompt has been customized with rules explicitly requiring (see "Attempts Made" below):
Mandatory use of print() for all intermediate results needed for subsequent steps or the final answer (Rule 11/13).
Strict separation of the final_answer code block from calculation blocks (Rule 12/14).
Requirement that the final_answer block only assembles results using data explicitly printed in previous steps and available in Observation history (Rule 14).
Suggestion/requirement to print() complex final structures first, then call final_answer in a subsequent step (Rule 15).
Potentially a mandatory validation step before final_answer (Rule 16).
Planning is enabled (planning_interval is set).
Observed Behavior:
Instead of following the strict cycle, the agent might:
Example (Explanation then Jump): Generate a natural language explanation of the steps it intends to take, followed immediately by a final_answer call containing a fully formed, complex result structure with numerical values that were never calculated/printed in preceding Code blocks.
# Agent output might look like this:# Thought: I will load data, validate, calculate metrics, and summarize.# (Natural language explanation of steps 1, 2, 3...)## final_answer({# "summary": "Analysis complete.",# "details": {# "metric_A": 123.45, # Value seemingly fabricated# "metric_B": "High", # Value seemingly fabricated# # ... other fabricated results# }# })
Expected Behavior:
The agent should strictly adhere to the cycle:
Thought: Plan the calculation/action.
Code: Perform calculations, construct necessary intermediate or final structures, and print() any results/structures needed later.
Observation: Shows the exact output from print().
Thought: Acknowledge the observed results. If preparing the final answer, validate the observed structure against earlier observations if needed (per Rule 16).
Code: If validation passes (or if it's an intermediate step), either perform the next calculation OR call final_answer using only variables/structures that were present in the preceding Observation.
Attempts Made:
Added multiple strict rules to the CodeAgent system prompt (via prompt_templates) emphasizing the use of print(), separation of final_answer, reliance on Observation history, printing complex structures first, and pre-final answer validation.
13. You MUST use `print()` within your `Code:` block to output the results of any file reads, data loading, calculations, or analyses (e.g., print dataframes, statistical summaries, variable values) that are necessary inputs for subsequent steps or for constructing the final answer. These outputs are crucial as they form the 'Observation:' for the next step.
14. The code block that calls `final_answer` MUST be separate from the main calculation/analysis code blocks. This final block MUST ONLY assemble the answer using data, variables, or structures that were explicitly output using `print()` in PREVIOUS steps and are therefore visible in the preceding 'Observation:' history. Do NOT perform calculations, string formatting (like f-strings), list creation, or complex logic within the final `final_answer` block itself; ensure all results and structures needed for the final answer are calculated and `print`ed in earlier steps.
15. If the final answer requires a complex structure (like a formatted summary string, a list of recommendations, or a nested dictionary), construct this structure in a variable within a calculation step and use `print()` to output the complete structure. Then, in the *next* separate step, call `final_answer` using only the variable name that holds the structure observed from the previous step's `print` output.
16. After printing the proposed final answer structure (as required by Rule 15), you MUST dedicate the next step to validation. In the 'Thought:' of this validation step, explicitly compare the printed proposed structure (from the latest 'Observation:') against the raw calculation results printed in earlier 'Observation:' sections. State whether the structure accurately reflects the calculated data. Only if validation passes, proceed to call `final_answer` in the *subsequent* step. If validation fails, explain the discrepancy and generate code to correct the structure or perform necessary recalculations.
Enabled planning via planning_interval.
Question:
Is this a known limitation or behavior pattern? Are there further prompt engineering strategies, agent configurations, or alternative approaches within smolagents recommended to more reliably prevent the agent from bypassing the observation cycle and hallucinating the final_answer content, especially for complex outputs? Could explicit validation steps be made more robust?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Description:
The
CodeAgent
sometimes generates content for thefinal_answer
tool call directly within a code block, without strictly adhering to the intended workflow of calculating results, printing them, and then assembling the final answer based only on the resultingObservation
history from previous steps. This leads to hallucinated or unverified final results, even when strong prompt rules are in place.Scenario:
CodeAgent
that requires multi-step data loading, calculation, or analysis.final_answer
.CodeAgent
's system prompt has been customized with rules explicitly requiring (see "Attempts Made" below):print()
for all intermediate results needed for subsequent steps or the final answer (Rule 11/13).final_answer
code block from calculation blocks (Rule 12/14).final_answer
block only assembles results using data explicitly printed in previous steps and available inObservation
history (Rule 14).print()
complex final structures first, then callfinal_answer
in a subsequent step (Rule 15).final_answer
(Rule 16).planning_interval
is set).Observed Behavior:
Instead of following the strict cycle, the agent might:
final_answer
call containing a fully formed, complex result structure with numerical values that were never calculated/printed in precedingCode
blocks.Expected Behavior:
The agent should strictly adhere to the cycle:
print()
any results/structures needed later.print()
.final_answer
using only variables/structures that were present in the precedingObservation
.Attempts Made:
CodeAgent
system prompt (viaprompt_templates
) emphasizing the use ofprint()
, separation offinal_answer
, reliance onObservation
history, printing complex structures first, and pre-final answer validation.planning_interval
.Question:
Is this a known limitation or behavior pattern? Are there further prompt engineering strategies, agent configurations, or alternative approaches within
smolagents
recommended to more reliably prevent the agent from bypassing the observation cycle and hallucinating thefinal_answer
content, especially for complex outputs? Could explicit validation steps be made more robust?Environment:
CodeAgent
LocalPythonExecutor
Beta Was this translation helpful? Give feedback.
All reactions