Skip to content

Conversation

@binbandit
Copy link
Contributor

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_agent with a structured output format, LangGraph runs the model twice:

  1. First with bind_tools to get a response that knows about the tools (stored in message history)
  2. Then with with_structured_output to 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_response field.

Solution

The fix modifies the get_agent_response method to prioritize the message from the message history over the structured output when they conflict:

# Get the last message from the message history
# This is the response from the first LLM call with bind_tools
messages = current_state.values.get('messages', [])
last_message_content = None
if messages and len(messages) > 0:
    last_message = messages[-1]
    if isinstance(last_message, AIMessage) and last_message.content:
        last_message_content = last_message.content

# Use the message from the message history if available, otherwise use the structured response
message_content = last_message_content or structured_response.message

Testing

I created a simple test script to verify that the fix works as expected. The test confirms that:

  1. The original implementation uses only the structured content, which can be inconsistent with the message history
  2. The fixed implementation prioritizes the message content from the message history, which is more likely to be correct

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.

@binbandit
Copy link
Contributor Author

bump

1 similar comment
@binbandit
Copy link
Contributor Author

bump

@binbandit
Copy link
Contributor Author

@kthota-g Could I please get a review?

@holtskinner
Copy link
Member

Please move this PR to the new samples repo here https://github.com/google-a2a/a2a-samples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants