Skip to content

Conversation

@mingshl
Copy link
Collaborator

@mingshl mingshl commented Nov 10, 2025

Description

Fixes JSON parsing errors in ML agent error handling by properly escaping special characters in exception messages using StringUtils.processTextDoc().

What is the problem?

When ML agents encounter tool execution failures with complex error messages (containing newlines, quotes, etc.), the unescaped exception text breaks JSON structure in agent responses, causing parsing failures like:

Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $

How does this PR solve it?

  • Wraps e.getMessage() with StringUtils.processTextDoc() in MLChatAgentRunner.java line 628
  • Ensures exception messages are properly escaped before inclusion in JSON responses
  • Prevents JSON structure corruption from special characters in error messages

Testing

Added unit tests in MLChatAgentRunnerTest.java to verify:

  • Complex exception messages with quotes and newlines are properly escaped
  • Gson parsing error messages are handled correctly
  • Normal error messages continue to work as expected

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.

Assert.assertTrue(result.containsKey(AgentUtils.TOOL_RESPONSE));
}

@Test
Copy link
Contributor

Choose a reason for hiding this comment

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

These tests well suit to the test class StringUtilsTest. Basically these are testing the API StringUtils.processTextDoc(p with different inputs. Nothing doing with APIs in MLChatAgentRunner.

TOOL_CALL_ID,
toolCallId,
"tool_response",
"Tool " + action + " failed: " + StringUtils.processTextDoc(e.getMessage())
Copy link
Contributor

Choose a reason for hiding this comment

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

Here StringUtils class name is not required. Static import of processTextDoc API is there . "import static org.opensearch.ml.common.utils.StringUtils.processTextDoc;"

substitute(
tmpParameters.get(INTERACTION_TEMPLATE_TOOL_RESPONSE),
Map.of(TOOL_CALL_ID, toolCallId, "tool_response", "Tool " + action + " failed: " + e.getMessage()),
Map
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need to fix other agent runners?

@rithin-pullela-aws
Copy link
Contributor

Can you rebase with current main?
I believe JP's latest commit fixes this error

[Incubating] Problems report is available at: file:///__w/ml-commons/ml-commons/build/reports/problems/problems-report.html
FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':opensearch-ml-plugin:dependencyLicenses'.
> Failed to query the value of task ':opensearch-ml-plugin:dependencyLicenses' property 'dependencies'.

   > Could not resolve all dependencies for configuration ':opensearch-ml-plugin:runtimeClasspath'.
      > Could not resolve software.amazon.awssdk:kms:2.32.29.
        Required by:
            project :opensearch-ml-plugin > project :opensearch-ml-algorithms > software.amazon.awssdk:bom:2.32.29
            project :opensearch-ml-plugin > project :opensearch-ml-algorithms > org.opensearch:opensearch-remote-metadata-sdk-ddb-client:3.4.0.0-SNAPSHOT:20251106.022320-9
         > Conflict found for module 'software.amazon.awssdk:kms': between versions 2.32.29 and 2.26.3
      > Could not resolve software.amazon.awssdk:dynamodb:2.32.29.
        Required by:
            project :opensearch-ml-plugin > project :opensearch-ml-algorithms > software.amazon.awssdk:bom:2.32.29
            project :opensearch-ml-plugin > project :opensearch-ml-algorithms > software.amazon.awssdk:bom:2.32.29 > software.amazon.awssdk:dynamodb-enhanced:2.32.29
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/8.14.3/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
         > Conflict found for module 'software.amazon.awssdk:dynamodb': between versions 2.32.29 and 2.26.3
      > Could not resolve org.dafny:DafnyRuntime:4.9.0.
        Required by:
            project :opensearch-ml-plugin > project :opensearch-ml-algorithms > org.opensearch:opensearch-remote-metadata-sdk-ddb-client:3.4.0.0-SNAPSHOT:20251106.022320-9 > software.amazon.cryptography:aws-database-encryption-sdk-dynamodb:3.9.0
            project :opensearch-ml-plugin > project :opensearch-ml-algorithms > org.opensearch:opensearch-remote-metadata-sdk-ddb-client:3.4.0.0-SNAPSHOT:20251106.022320-9 > software.amazon.cryptography:aws-cryptographic-material-providers:1.11.0
         > Conflict found for module 'org.dafny:DafnyRuntime': between versions 4.9.0 and 4.1.0
> There are 5 more failures with identical causes.

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