Skip to content

feat: generate final answer when max_agent_steps reached#10011

Closed
dcondrey wants to merge 4 commits into
deepset-ai:mainfrom
dcondrey:final_answer_on_max_steps
Closed

feat: generate final answer when max_agent_steps reached#10011
dcondrey wants to merge 4 commits into
deepset-ai:mainfrom
dcondrey:final_answer_on_max_steps

Conversation

@dcondrey

@dcondrey dcondrey commented Nov 5, 2025

Copy link
Copy Markdown

Related Issues

Proposed Changes:

Previously, if step_countmax_agent_steps and last_msgToolResult, output = ToolResult (no final text).

Now, if step_countmax_agent_steps and last_msg ∈ ToolResult, optionally perform 1 extra LLM call → final_text_answer.

How did you test it?

  • Files compile without Python syntax errors
  • Fixed critical async bug
  • Two new tests in test/components/agents/test_agent.py

Notes for the reviewer

hatch run test:unit test/components/agents/test_agent.py::TestAgent::test_final_answer_on_max_steps_enabled
hatch run test:unit test/components/agents/test_agent.py::TestAgent::test_final_answer_on_max_steps_disabled
hatch run test:unit test/components/agents/test_agent.py::TestAgent::test_exceed_max_steps

Checklist

✓ I have read the contributors guidelines and the code of conduct

  • I have updated the related issue with new insights and changes
    ✓ I added unit tests and updated the docstrings
    ✓ I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
    ✓ I documented my code
    ✓✓ I ran pre-commit hooks and fixed any issue

@dcondrey dcondrey requested a review from a team as a code owner November 5, 2025 02:10
@dcondrey dcondrey requested review from vblagoje and removed request for a team November 5, 2025 02:10
@vercel

vercel Bot commented Nov 5, 2025

Copy link
Copy Markdown

@dcondrey is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@CLAassistant

CLAassistant commented Nov 5, 2025

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions Bot added topic:tests type:documentation Improvements on the docs labels Nov 5, 2025
@sjrl sjrl requested review from sjrl and removed request for vblagoje November 5, 2025 07:17
@dcondrey

Copy link
Copy Markdown
Author

This fixes a contract break in Agent.run() when max_agent_steps is hit on a tool call.

Previously the agent could exit with a raw ToolResult as last_message.

With final_answer_on_max_steps=True (default), the agent makes one extra, tool-disabled LLM call only in that case to produce a proper final ChatMessage.

Setting it to False preserves the old behavior. Normal runs and step limits are otherwise unchanged.

@sjrl

sjrl commented Jan 26, 2026

Copy link
Copy Markdown
Contributor

@dcondrey thanks for working on this so quickly but we are still discussing internally if this is a pattern we we want to support. For example, an alternative is to pipe the output of the Agent to other downstream components like a ChatGenerator component to generate a final answer instead of adding this complexity inside of the Agent component.

@sjrl

sjrl commented Feb 25, 2026

Copy link
Copy Markdown
Contributor

Closing, you can find more context about the decision here #10610 (comment)

@sjrl sjrl closed this Feb 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Agent can exit on tool call when max_agent_steps is reached, not guaranteeing a final text answer

3 participants