Skip to content

fix: handle dangling tool results when history is cleared due to size limits #1527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2025

Conversation

tsmithsz
Copy link
Contributor

@tsmithsz tsmithsz commented Jun 5, 2025

Problem

We receive Improperly formed requests failures due to: Message with ToolResults must be preceded by AssistantResponseMessage

There is an edge case where when the context size is exceeded, we remove the history entirely if we cannot find a valid userInputMessage without toolResults to trim up to. This results in validation issues on the backend if the new UserInputMessage contains toolResults (there is no existing previous message with toolUses to pair with the toolResult).

Solution

Short-term solution: Abandon the toolResult for this case and inform the LLM that the context limit was execeeded.

This is the current approach done by CLI: https://github.com/aws/amazon-q-developer-cli/blob/main/crates/cli/src/cli/chat/conversation_state.rs#L327

TODO:
Long-term: Come up with a way to preserve the history while fitting it in the context char limit

Testing

Reproduced the issue. Now instead of returning an Improperly formed request, the user is able to continue:

Screen.Recording.2025-06-04.at.9.41.35.PM.mov

License

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@tsmithsz tsmithsz requested a review from a team as a code owner June 5, 2025 04:14
@tsmithsz tsmithsz changed the title fix: handle dangling tool results when history is cleared due to size… fix: handle dangling tool results when history is cleared due to size limits Jun 5, 2025
@tsmithsz tsmithsz marked this pull request as draft June 5, 2025 04:35
@tsmithsz tsmithsz marked this pull request as ready for review June 5, 2025 23:32
@tsmithsz tsmithsz merged commit 9082323 into aws:main Jun 5, 2025
6 checks passed
@tsmithsz tsmithsz deleted the agentic-main branch June 10, 2025 17:02
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.

5 participants