Skip to content

[BUG] Agent Framework: Handle model response when toolUse is not accompanied by text #3755

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

Conversation

pyek-bot
Copy link
Contributor

Description

Fix for when model doesn't return a text along with a toolUse request.

Example failure payload:

{"metrics":{"latencyMs":2875},"output":{"message":{"content":[{"toolUse":{"input":{"index":"ss4o_logs-2025.04.18","query":{"size":10,"_source":["body","time","severityText","log.attributes","resource.attributes.service.name"],"query":{"bool":{"must":[{"wildcard":{"body":"*cart*"}},{"term":{"severityText":"ERROR"}}]}}}},"name":"SearchIndexTool","toolUseId":"tooluse_c33B609fSbCsgDeTTj8VmA"}}],"role":"assistant"}},"stopReason":"tool_use","usage":{"cacheReadInputTokenCount":0,"cacheReadInputTokens":0,"cacheWriteInputTokenCount":0,"cacheWriteInputTokens":0,"inputTokens":9000,"outputTokens":152,"totalTokens":9152}}

Related Issues

Resolves #3751

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff.
  • Public documentation issue/PR created.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@pyek-bot pyek-bot had a problem deploying to ml-commons-cicd-env-require-approval April 23, 2025 00:00 — with GitHub Actions Failure
@pyek-bot pyek-bot had a problem deploying to ml-commons-cicd-env-require-approval April 23, 2025 00:00 — with GitHub Actions Error
@pyek-bot pyek-bot had a problem deploying to ml-commons-cicd-env-require-approval April 23, 2025 00:00 — with GitHub Actions Failure
@pyek-bot pyek-bot had a problem deploying to ml-commons-cicd-env-require-approval April 23, 2025 00:00 — with GitHub Actions Error
Object response;
boolean isToolUseResponse = false;
try {
response = JsonPath.read(dataAsMap, parameters.get(LLM_RESPONSE_FILTER));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it easier just to check if LLM_RESPONSE_FILTER in parameters.keys then get the response, else you read from the TOOL_CALLS_PATH? this exception is not necessary

Copy link
Collaborator

@mingshl mingshl Apr 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

boolean hasResponseFilter = parameters.containsKey(LLM_RESPONSE_FILTER);

if (hasResponseFilter) {
    response = JsonPath.read(dataAsMap, parameters.get(LLM_RESPONSE_FILTER));
} else {
    response = JsonPath.read(dataAsMap, parameters.get(TOOL_CALLS_PATH));
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both of the keys will be present in the map for all models supporting function calling:

LLM_RESPONSE_FILTER points to where we can fetch the text returned by the model. Once we fetch the text, the logic checks for whether there is a toolUse block after or not.

TOOL_CALLS_PATH points to where we can fetch a toolUse block returned by the model.

For example, these are values for claude
LLM_RESPONSE_FILTER: $.choices[0].message.content
TOOL_CALLS_PATH: $.choices[0].message.tool_calls

Before this fix, we would always look for LLM_RESPONSE_FILTER and in the case of this payload:

{"metrics":{"latencyMs":2875},"output":{"message":{"content":[{"toolUse":{"input":{"index":"ss4o_logs-2025.04.18","query":{"size":10,"_source":["body","time","severityText","log.attributes","resource.attributes.service.name"],"query":{"bool":{"must":[{"wildcard":{"body":"*cart*"}},{"term":{"severityText":"ERROR"}}]}}}},"name":"SearchIndexTool","toolUseId":"tooluse_c33B609fSbCsgDeTTj8VmA"}}],"role":"assistant"}},"stopReason":"tool_use","usage":{"cacheReadInputTokenCount":0,"cacheReadInputTokens":0,"cacheWriteInputTokenCount":0,"cacheWriteInputTokens":0,"inputTokens":9000,"outputTokens":152,"totalTokens":9152}}

It would lead to an error since there is no text field. Therefore, we then check for toolUse path. This is required as different models have a different response format. A better way to handle this would be to first check for a parent path like $.choices[0].message which might contain both the text and toolUse as children. However, this is not guaranteed as the response heavily depends on the model. Hence, the try-catch.

@pyek-bot pyek-bot force-pushed the bug_fix_no_text_with_tooluse branch 2 times, most recently from f5b7674 to 22dc728 Compare April 25, 2025 18:17
@pyek-bot pyek-bot temporarily deployed to ml-commons-cicd-env-require-approval April 25, 2025 18:19 — with GitHub Actions Inactive
@pyek-bot pyek-bot temporarily deployed to ml-commons-cicd-env-require-approval April 25, 2025 18:19 — with GitHub Actions Inactive
@pyek-bot pyek-bot had a problem deploying to ml-commons-cicd-env-require-approval April 25, 2025 18:19 — with GitHub Actions Failure
@pyek-bot pyek-bot temporarily deployed to ml-commons-cicd-env-require-approval April 25, 2025 18:19 — with GitHub Actions Inactive
ylwu-amzn
ylwu-amzn previously approved these changes Apr 25, 2025
Copy link

codecov bot commented Apr 25, 2025

Codecov Report

Attention: Patch coverage is 85.71429% with 1 line in your changes missing coverage. Please review.

Project coverage is 78.24%. Comparing base (9e417e6) to head (0b565a3).
Report is 5 commits behind head on main.

Files with missing lines Patch % Lines
...nsearch/ml/engine/algorithms/agent/AgentUtils.java 85.71% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #3755      +/-   ##
============================================
+ Coverage     78.12%   78.24%   +0.12%     
- Complexity     7156     7161       +5     
============================================
  Files           631      631              
  Lines         32211    32216       +5     
  Branches       3660     3660              
============================================
+ Hits          25164    25207      +43     
+ Misses         5492     5452      -40     
- Partials       1555     1557       +2     
Flag Coverage Δ
ml-commons 78.24% <85.71%> (+0.12%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@pyek-bot
Copy link
Contributor Author

Added a testcase to validate 3 cases for the 3 models:

  1. Only tooluse
  2. Only text
  3. Both tooluse and text

@ylwu-amzn can you review again please? thanks!

@pyek-bot pyek-bot temporarily deployed to ml-commons-cicd-env-require-approval April 25, 2025 20:55 — with GitHub Actions Inactive
@pyek-bot pyek-bot temporarily deployed to ml-commons-cicd-env-require-approval April 25, 2025 20:55 — with GitHub Actions Inactive
@pyek-bot pyek-bot temporarily deployed to ml-commons-cicd-env-require-approval April 25, 2025 20:55 — with GitHub Actions Inactive
@pyek-bot pyek-bot temporarily deployed to ml-commons-cicd-env-require-approval April 25, 2025 20:55 — with GitHub Actions Inactive
@pyek-bot
Copy link
Contributor Author

Please add 3.0 backport label!

@pyek-bot pyek-bot temporarily deployed to ml-commons-cicd-env-require-approval April 25, 2025 23:22 — with GitHub Actions Inactive
@pyek-bot pyek-bot temporarily deployed to ml-commons-cicd-env-require-approval April 25, 2025 23:22 — with GitHub Actions Inactive
@ylwu-amzn ylwu-amzn merged commit 736bf10 into opensearch-project:main Apr 26, 2025
13 checks passed
opensearch-trigger-bot bot pushed a commit that referenced this pull request Apr 26, 2025
…mpanied by text (#3755)

* fix: handle model response when toolUse is not accompanied by text

Signed-off-by: Pavan Yekbote <[email protected]>

* feat: add test case for parseLLMOutput

Signed-off-by: Pavan Yekbote <[email protected]>

---------

Signed-off-by: Pavan Yekbote <[email protected]>
(cherry picked from commit 736bf10)
dhrubo-os pushed a commit that referenced this pull request Apr 28, 2025
…mpanied by text (#3755) (#3783)

* fix: handle model response when toolUse is not accompanied by text



* feat: add test case for parseLLMOutput



---------


(cherry picked from commit 736bf10)

Signed-off-by: Pavan Yekbote <[email protected]>
Co-authored-by: Pavan Yekbote <[email protected]>
dhrubo-os added a commit that referenced this pull request May 8, 2025
* [BUG] Agent Framework: Handle model response when toolUse is not accompanied by text (#3755)

* fix: handle model response when toolUse is not accompanied by text

Signed-off-by: Pavan Yekbote <[email protected]>

* feat: add test case for parseLLMOutput

Signed-off-by: Pavan Yekbote <[email protected]>

---------

Signed-off-by: Pavan Yekbote <[email protected]>

* [BUG] Allow user to control react agent max_interations value to prevent empty response (#3756)

* fix: expose max_iteration for react

Signed-off-by: Pavan Yekbote <[email protected]>

* fix: defaults for agent execution and differentiate between step and step result

Signed-off-by: Pavan Yekbote <[email protected]>

* fix: return react agent id in agent response to expose more details

Signed-off-by: Pavan Yekbote <[email protected]>

* spotless

Signed-off-by: Pavan Yekbote <[email protected]>

* fix: remove test prompt from react system prompt

Signed-off-by: Pavan Yekbote <[email protected]>

* refactor: rename parameters exposed to user to executor

Signed-off-by: Pavan Yekbote <[email protected]>

* fix: give user complete control over planner system prompt

Signed-off-by: Pavan Yekbote <[email protected]>

---------

Signed-off-by: Pavan Yekbote <[email protected]>

* Clean up JSM from MCP (#3773)

Signed-off-by: rithin-pullela-aws <[email protected]>

* [Bug] ListTools call does not return tool attributes (#3785)

* initial commit for MCP server in OpenSearch (#3781)

* initial commit for MCP server in OpenSearch

Signed-off-by: zane-neo <[email protected]>

* Make change to support register or remove tools across cluster

Signed-off-by: zane-neo <[email protected]>

* format code

Signed-off-by: zane-neo <[email protected]>

* fix UT failure caused by code change

Signed-off-by: zane-neo <[email protected]>

* format code

Signed-off-by: zane-neo <[email protected]>

* format code

Signed-off-by: zane-neo <[email protected]>

* add license header

Signed-off-by: zane-neo <[email protected]>

* fix notifications initialized not respond issue

Signed-off-by: zane-neo <[email protected]>

* fix minor issues and add UTs

Signed-off-by: zane-neo <[email protected]>

* Add more UTs

Signed-off-by: zane-neo <[email protected]>

---------

Signed-off-by: zane-neo <[email protected]>

* Remove beta1 qualifier (#3794) (#3795)

(cherry picked from commit 3f503f1)

Signed-off-by: Peter Zhu <[email protected]>
Co-authored-by: Peter Zhu <[email protected]>

* [AUTO] Increment version to 3.1.0-SNAPSHOT (#3789)

* Increment version to 3.1.0-SNAPSHOT

Signed-off-by: opensearch-ci-bot <[email protected]>

* Update build.gradle

Signed-off-by: Peter Zhu <[email protected]>

---------

Signed-off-by: opensearch-ci-bot <[email protected]>
Signed-off-by: Peter Zhu <[email protected]>
Co-authored-by: opensearch-ci-bot <[email protected]>
Co-authored-by: Peter Zhu <[email protected]>

* add release note for 3.0 (#3792)

Signed-off-by: Mingshi Liu <[email protected]>

* support MCP session management (#3803)

* support MCP session management

Signed-off-by: zane-neo <[email protected]>

* Addressing comments

Signed-off-by: zane-neo <[email protected]>

* add feature flag for mcp server and renaming mcp connector feature flag

Signed-off-by: zane-neo <[email protected]>

* Address critical comments in #3781

Signed-off-by: zane-neo <[email protected]>

---------

Signed-off-by: zane-neo <[email protected]>

* upgrade http client to version align with core (#3809)

* upgrade http client to versoin align with core

Signed-off-by: zane-neo <[email protected]>

* upgrade httpclient-h2 to correct versiono

Signed-off-by: zane-neo <[email protected]>

* use placeholder approach

Signed-off-by: zane-neo <[email protected]>

---------

Signed-off-by: zane-neo <[email protected]>

* support customized message endpoint and addressing comments (#3810)

* support customized message endpoint and addressing comments

Signed-off-by: zane-neo <[email protected]>

* fix UT failures

Signed-off-by: zane-neo <[email protected]>

* add files to jacoco exception

Signed-off-by: zane-neo <[email protected]>

* fix tool name issue and optimize register tool api

Signed-off-by: zane-neo <[email protected]>

* fix schema not parsed correctly issue and NPE when parameters is null

Signed-off-by: zane-neo <[email protected]>

* fix failure UT

Signed-off-by: zane-neo <[email protected]>

---------

Signed-off-by: zane-neo <[email protected]>

* excluding circuit breaker for Agent (#3814)

Signed-off-by: Dhrubo Saha <[email protected]>

* change release note (#3811)

* change release note

Signed-off-by: zane-neo <[email protected]>

* Update opensearch-ml-common.release-notes-3.0.0.0.md

* Update opensearch-ml-common.release-notes-3.0.0.0.md

* Update opensearch-ml-common.release-notes-3.0.0.0.md

---------

Signed-off-by: zane-neo <[email protected]>
Co-authored-by: Peter Zhu <[email protected]>

* Downgrade MCP version to 0.9 (#3821)

Signed-off-by: rithin-pullela-aws <[email protected]>

* remove libs folder (#3824)

Signed-off-by: Yaliang Wu <[email protected]>

* add more logging to deploy/undeploy flows for better debugging (#3825)

* add more logging to deploy/undeploy flows for better debugging

Signed-off-by: Bhavana Goud Ramaram <[email protected]>

* Fix python client not able to connect to MCP server issue (#3822)

Signed-off-by: zane-neo <[email protected]>
Co-authored-by: Dhrubo Saha <[email protected]>

* exclude trusted connector check for hidden model (#3838)

Signed-off-by: Dhrubo Saha <[email protected]>

* adding tenantId to the connector executor when this is inline connector (#3837)

* adding tenantId to the connector executor when this is inline connector

Signed-off-by: Dhrubo Saha <[email protected]>

* added more unit tests

Signed-off-by: Dhrubo Saha <[email protected]>

---------

Signed-off-by: Dhrubo Saha <[email protected]>

---------

Signed-off-by: Pavan Yekbote <[email protected]>
Signed-off-by: rithin-pullela-aws <[email protected]>
Signed-off-by: zane-neo <[email protected]>
Signed-off-by: Peter Zhu <[email protected]>
Signed-off-by: opensearch-ci-bot <[email protected]>
Signed-off-by: Mingshi Liu <[email protected]>
Signed-off-by: Dhrubo Saha <[email protected]>
Signed-off-by: Yaliang Wu <[email protected]>
Signed-off-by: Bhavana Goud Ramaram <[email protected]>
Co-authored-by: Pavan Yekbote <[email protected]>
Co-authored-by: Rithin Pullela <[email protected]>
Co-authored-by: zane-neo <[email protected]>
Co-authored-by: opensearch-trigger-bot[bot] <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com>
Co-authored-by: Peter Zhu <[email protected]>
Co-authored-by: opensearch-ci-bot <[email protected]>
Co-authored-by: Mingshi Liu <[email protected]>
Co-authored-by: Yaliang Wu <[email protected]>
Co-authored-by: Bhavana Goud Ramaram <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Agent Framework: When model responds only with a toolUse block without any text agent execution fails.
4 participants