Description
Is your feature request related to a problem? Please describe.
As most agents are expected to be run as a Chat application, returning exactly one answer per interaction/request/pipeline.run seems to be a defacto pattern.
The current Agent
always returns all messages that have been accumulated during the agent invocation. To render a final answer (e.g. for end users), in most cases, we are only interested in the last message. Currently we need to use an OutputAdapter for this, which makes standard Agentic pipelines more complex than necessary.
Describe the solution you'd like
Add a flag to AnswerBuilder
which allows to select only the last message to be converted as Answer.
Optional: Set any other messages to Answer's meta (e.g. for tool call inspections)
Describe alternatives you've considered
Adding the flag to Agent
would come with the downside that any tool call / iteration messages will be lost.
Additional context
Add any other context or screenshots about the feature request here.
Activity
sjrl commentedon May 5, 2025
Maybe an alternative or in addition to this could be to add an additional output to the Agent called final_message of type ChatMessage which always contains the last message?
@bilgeyucel also mentioned for standalone uses of Agent that this would be helpful.
bilgeyucel commentedon May 5, 2025
Agree with @sjrl here. Additionally, Haystack users don't use
AnswerBuilder
as they get a similar benefit through Pipeline'sinclude_outputs_from
parameter. Handling this inAnswerBuilder
would force users to add AnswerBuilder to their pipelinetstadel commentedon May 6, 2025
Interesting, I thought about this briefly too, but rejected it because
sjrl commentedon May 6, 2025
That's fair! I think it might be worth doing all of the following:
final_message
of type ChatMessage (orfinal_answer
of type string) output to Agent. This should be helpful for users who want to 1) use the component standalone 2) easily connect the final_message as input to another component other than AnswerBuilder in a Pipeline and 3) use Agent as a tool in ComponentTool where usually you'd want just the final answer as the tool output.vblagoje commentedon May 9, 2025
Hey @tstadel @sjrl, I sketched out what needs to be done in AnswerBuilder and it’s turning it into a complicated beast—right now AnswerBuilder verifies that every chat message contains valid text and throws a ValueError when it doesn’t and this is going to be problematic for agent messages that have tool calls and responses. We are mixing a lot of domain logic here, all the way from RAG, pattern matching, reference creation, and now on top of this we are pulling in agent logic into AnswerBuilder. We’d be better off spinning up a separate AgentAnswerBuilder that can look at the agent’s state and handle everything needed to produce the answer, however it needs to look. LMK