fix: Prioritize message history over structured output in LangGraph agent #113
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes issue a2aproject/a2a-samples#117 where there's a mismatch between the AI response in the message history and the structured output in the LangGraph example.
Problem
When using
create_react_agentwith a structured output format, LangGraph runs the model twice:bind_toolsto get a response that knows about the tools (stored in message history)with_structured_outputto get a structured response (stored in structured_response)These two responses can be inconsistent, as mentioned in issue a2aproject/a2a-samples#117. The first response (with tools) appears in the message history, while the second response (structured) appears in the
structured_responsefield.Solution
The fix modifies the
get_agent_responsemethod to prioritize the message from the message history over the structured output when they conflict:Testing
I created a simple test script to verify that the fix works as expected. The test confirms that:
This fix ensures that the response shown to the user is consistent with the tool usage and reasoning shown in the message history, which is important for a good user experience.