Skip to content

Fix: Ensure proper format for Bedrock deepseek tool result #3933

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

@pyek-bot pyek-bot commented Jun 20, 2025

Description

3.1 introduced function calling support via interface implementations.

Bedrock deepseek does not naturally support function calling and is simulated via prompts.

In order to support this, we need to ensure that the tool result supplied to the model is a json escaped string rather than a proper json itself.

Current (trimmed payload to show what the change achieves)

{
        "content": [{
            "text": {
                "tool_call_id": "tool_use_5b3f7d8e-7a2b-4ec5-9c1a-0d9e8f7a6b3c",
                "tool_result": {
                    "text": "row,health,status,index,uuid,pri(number of primary shards),rep(number of replica shards),docs.count(number of available documents),docs.deleted(number of deleted documents),store.size(store size of primary and replica shards),pri.store.size(store size of primary shards)\n1,green,open,.plugins-ml-model-group,vcEmjDKdQS23WKLR1IhG7A,1,0,1,1,12.6kb,12.6kb\n2,green,open,.plugins-ml-memory-message,MtrQ8tyBScOLf7ZrcmBg0Q,1,0,2,0,8.9kb,8.9kb\n3,green,open,.plugins-ml-memory-meta,lxlfL1QdQ-mSeOxunbMO6Q,1,0,2,2,23kb,23kb\n4,green,open,.plugins-ml-config,YYKDrHwjR3mQ6-rL0AFkZQ,1,0,1,0,3.9kb,3.9kb\n5,green,open,.plugins-ml-model,81ngSS54SvGCeSVCWrAygg,1,0,1,3,162.4kb,162.4kb\n6,green,open,.plugins-ml-agent,5xgc0Yy_Syi7oyygZuE6Vg,1,0,2,0,40.4kb,40.4kb\n7,green,open,.plugins-ml-task,JEs_Iyk2QCicZRLQeIK9Hw,1,0,3,3,44.2kb,44.2kb\n"
                }
            }
        }],
        "role": "user"
    }]
}

Expected

"content": [{
            "text": "\n\n{\"stop_reason\": \"tool_use\", \"tool_calls\": [{\"id\":\"tool_use_7Y9vBq3sTdG2fLpQxRzK1w\",\"tool_name\":\"ListIndexTool\",\"input\": {\"indices\":[\"*\"]}}]}"
        }],
        "role": "assistant"
    }, {
        "role": "user",
        "content": [{
            "text": "{\"tool_call_id\":\"tool_use_7Y9vBq3sTdG2fLpQxRzK1w\",\"tool_result\": \"row,health,status,index,uuid,pri(number of primary shards),rep(number of replica shards),docs.count(number of available documents),docs.deleted(number of deleted documents),store.size(store size of primary and replica shards),pri.store.size(store size of primary shards)\n1,green,open,.plugins-ml-memory-message,GOQ9B7C_Tum4NqbiqJ71yQ,1,0,2,0,8.9kb,8.9kb\n2,green,open,.plugins-ml-model-group,cOQq57ouSH2MEtd_dYHn5A,1,0,1,1,12.6kb,12.6kb\n3,green,open,.plugins-ml-memory-meta,LOj8ujo4Qk254gEea192ZQ,1,0,2,2,23kb,23kb\n4,green,open,.plugins-ml-config,YFIs4Kj3Rvy42cXG8jsYCw,1,0,1,0,3.9kb,3.9kb\n5,green,open,.plugins-ml-model,kDJ5x0ubQiyfp6CpxC2aGg,1,0,1,3,162.5kb,162.5kb\n6,green,open,.plugins-ml-agent,svtGNlUXReamTs7jlO2QZg,1,0,2,0,40.4kb,40.4kb\n7,green,open,.plugins-ml-task,XaIDoRazR8akCuoFP-yrHw,1,0,3,3,43.8kb,43.8kb\n\""
        }]
    }]
}

Related Issues

Resolves #[Issue number to be closed when this PR is merged]

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.

Signed-off-by: Pavan Yekbote <pybot@amazon.com>
@pyek-bot pyek-bot had a problem deploying to ml-commons-cicd-env-require-approval June 20, 2025 08:40 — with GitHub Actions Failure
@pyek-bot pyek-bot had a problem deploying to ml-commons-cicd-env-require-approval June 20, 2025 08:40 — with GitHub Actions Error
@pyek-bot pyek-bot had a problem deploying to ml-commons-cicd-env-require-approval June 20, 2025 08:40 — with GitHub Actions Error
@pyek-bot pyek-bot had a problem deploying to ml-commons-cicd-env-require-approval June 20, 2025 08:40 — with GitHub Actions Failure
toolMessage.getContent().add(Map.of("text", Map.of(TOOL_CALL_ID, toolUseId, TOOL_RESULT, toolResult.get(TOOL_RESULT))));

String textJson = StringUtils.toJson(Map.of(TOOL_CALL_ID, toolUseId, TOOL_RESULT, toolResult.get(TOOL_RESULT)));
toolMessage.getContent().add(Map.of("text", textJson));
Copy link
Collaborator

Choose a reason for hiding this comment

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

No unit test?

ylwu-amzn
ylwu-amzn previously approved these changes Jun 20, 2025
@pyek-bot pyek-bot had a problem deploying to ml-commons-cicd-env-require-approval June 20, 2025 16:40 — with GitHub Actions Error
@pyek-bot pyek-bot had a problem deploying to ml-commons-cicd-env-require-approval June 20, 2025 16:40 — with GitHub Actions Error
@pyek-bot pyek-bot had a problem deploying to ml-commons-cicd-env-require-approval June 20, 2025 16:40 — with GitHub Actions Failure
@pyek-bot pyek-bot had a problem deploying to ml-commons-cicd-env-require-approval June 20, 2025 16:40 — with GitHub Actions Failure
@mingshl
Copy link
Collaborator

mingshl commented Jun 20, 2025

unit test is failing, check this out

> Task :opensearch-ml-algorithms:test

BedrockConverseDeepseekR1FunctionCallingTests > supply FAILED
    java.lang.ClassCastException at BedrockConverseDeepseekR1FunctionCallingTests.java:81

toolMessage.getContent().add(Map.of("text", Map.of(TOOL_CALL_ID, toolUseId, TOOL_RESULT, toolResult.get(TOOL_RESULT))));

String textJson = StringUtils.toJson(Map.of(TOOL_CALL_ID, toolUseId, TOOL_RESULT, toolResult.get(TOOL_RESULT)));
toolMessage.getContent().add(Map.of("text", textJson));
Copy link
Collaborator

@mingshl mingshl Jun 20, 2025

Choose a reason for hiding this comment

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

does the value of 'text' has to be string? I saw the return allowing List<Map<String, Object>> toolResults

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Supply method returns List<LLMMessage>

This is how the supply result is used

List<LLMMessage> llmMessages = functionCalling.supply(toolResults);
interactions.add(llmMessages.getFirst().getResponse());

This is the getResponse method in Bedrock message (which is an implemention of LLMMessage)

    public String getResponse() {
        return StringUtils.toJson(Map.of("role", role, "content", content));
    }

At the end we are converting back to a string, so it's okay

@ylwu-amzn
Copy link
Collaborator

CI failed,

BedrockConverseDeepseekR1FunctionCallingTests > supply FAILED
java.lang.ClassCastException at BedrockConverseDeepseekR1FunctionCallingTests.java:81

@pyek-bot
Copy link
Contributor Author

I fixed the failing test, build is running on my local. I'll push the changes once it builds successfully.

Signed-off-by: Pavan Yekbote <pybot@amazon.com>
@pyek-bot pyek-bot temporarily deployed to ml-commons-cicd-env-require-approval June 20, 2025 18:05 — with GitHub Actions Inactive
@pyek-bot pyek-bot temporarily deployed to ml-commons-cicd-env-require-approval June 20, 2025 18:05 — with GitHub Actions Inactive
@pyek-bot pyek-bot temporarily deployed to ml-commons-cicd-env-require-approval June 20, 2025 18:05 — with GitHub Actions Inactive
@pyek-bot pyek-bot temporarily deployed to ml-commons-cicd-env-require-approval June 20, 2025 18:05 — with GitHub Actions Inactive
Copy link

codecov bot commented Jun 20, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.36%. Comparing base (ab5f064) to head (1bba233).
Report is 1 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #3933      +/-   ##
============================================
- Coverage     80.37%   80.36%   -0.01%     
+ Complexity     7917     7915       -2     
============================================
  Files           693      693              
  Lines         34864    34865       +1     
  Branches       3878     3878              
============================================
- Hits          28021    28019       -2     
- Misses         5103     5104       +1     
- Partials       1740     1742       +2     
Flag Coverage Δ
ml-commons 80.36% <100.00%> (-0.01%) ⬇️

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 pyek-bot had a problem deploying to ml-commons-cicd-env-require-approval June 20, 2025 19:08 — with GitHub Actions Failure
@pyek-bot pyek-bot had a problem deploying to ml-commons-cicd-env-require-approval June 20, 2025 19:08 — with GitHub Actions Failure
@pyek-bot pyek-bot temporarily deployed to ml-commons-cicd-env-require-approval June 20, 2025 20:06 — with GitHub Actions Inactive
@pyek-bot pyek-bot temporarily deployed to ml-commons-cicd-env-require-approval June 20, 2025 20:06 — with GitHub Actions Inactive
@pyek-bot pyek-bot temporarily deployed to ml-commons-cicd-env-require-approval June 20, 2025 20:06 — with GitHub Actions Inactive
@pyek-bot pyek-bot temporarily deployed to ml-commons-cicd-env-require-approval June 20, 2025 20:06 — with GitHub Actions Inactive
@ylwu-amzn ylwu-amzn merged commit 3322ac5 into opensearch-project:main Jun 20, 2025
9 checks passed
opensearch-trigger-bot bot pushed a commit that referenced this pull request Jun 20, 2025
* fix: deepseek function calling tool result

Signed-off-by: Pavan Yekbote <pybot@amazon.com>

* fix: unit test

Signed-off-by: Pavan Yekbote <pybot@amazon.com>

---------

Signed-off-by: Pavan Yekbote <pybot@amazon.com>
(cherry picked from commit 3322ac5)
@pyek-bot pyek-bot had a problem deploying to ml-commons-cicd-env-require-approval June 20, 2025 21:11 — with GitHub Actions Failure
@pyek-bot pyek-bot had a problem deploying to ml-commons-cicd-env-require-approval June 20, 2025 21:11 — with GitHub Actions Failure
dhrubo-os pushed a commit that referenced this pull request Jun 20, 2025
…3937)

* fix: deepseek function calling tool result



* fix: unit test



---------


(cherry picked from commit 3322ac5)

Signed-off-by: Pavan Yekbote <pybot@amazon.com>
Co-authored-by: Pavan Yekbote <pybot@amazon.com>
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.

5 participants