fix: normalize empty tool results for LlamaIndex to prevent loop #464
+38
−2
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.
TL;DR:
Adapts AG-UI tool result messages for LlamaIndex by converting empty results to a canonical value ("ok").
Prevents unnecessary re-invocation of tools in LlamaIndex workflows when tools return no meaningful payload.
Explanation
I did some testing and found that whenever I returned a value from the useCopilotAction hook's handler, the big wouldn't occur (had to do a ton of testing to avoid false positives).
This leads me to think that LlamaIndex handles empty tool result values as falsy in a sense, or a failure, and reattempts the request, tool call and all. Returning a value prevents this.
To patch this in @ag-ui/llamaindex, I reconfigured the LlamaIndexAgent class to intercept those empty values and return content: "ok" in place of the empty or undefined value.
Possible gotcha⚠️
If there are cases where a failed tool call would result in an undefined/empty value, we would need to be able to distinguish them from these successful tool calls that return the same value.