[PR #11181] fix: convert JSON string tool outputs to objects for Bedrock compatibility #13680

Closed
opened 2026-02-16 18:18:31 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/11181

State: closed
Merged: No


Summary

AWS Bedrock rejects tool results with string content, requiring JSON objects instead. This fix detects when a tool output is a valid JSON string (like the output from todowrite tool) and parses it into an object before sending to the model provider.

Problem

When tool results contain JSON strings (e.g., JSON.stringify(todos, null, 2)), AWS Bedrock returns the error:

"The format of the value at messages.X.content.0.toolResult.content.0.json is invalid. Provide a json object for the field and try again."

This is because Bedrock requires tool result content to be a JSON object, not a string containing JSON.

Solution

Modified the toModelOutput function in packages/opencode/src/session/message-v2.ts to:

  1. Attempt to parse string outputs as JSON
  2. If successful and the result is an object/array, return as { type: "json", value: parsed }
  3. Otherwise, fall back to { type: "text", value: output } for plain text

Changes

  • packages/opencode/src/session/message-v2.ts: Added JSON parsing logic to toModelOutput
  • packages/opencode/test/session/message-v2.test.ts: Added tests for JSON string and plain text tool outputs

Test Results

All 15 tests pass:

  • 13 existing tests continue to pass
  • 2 new tests verify JSON string → object conversion and plain text handling

Related

🤖 Generated with Claude Code

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/11181 **State:** closed **Merged:** No --- ## Summary AWS Bedrock rejects tool results with string content, requiring JSON objects instead. This fix detects when a tool output is a valid JSON string (like the output from `todowrite` tool) and parses it into an object before sending to the model provider. ## Problem When tool results contain JSON strings (e.g., `JSON.stringify(todos, null, 2)`), AWS Bedrock returns the error: ``` "The format of the value at messages.X.content.0.toolResult.content.0.json is invalid. Provide a json object for the field and try again." ``` This is because Bedrock requires tool result content to be a JSON object, not a string containing JSON. ## Solution Modified the `toModelOutput` function in `packages/opencode/src/session/message-v2.ts` to: 1. Attempt to parse string outputs as JSON 2. If successful and the result is an object/array, return as `{ type: "json", value: parsed }` 3. Otherwise, fall back to `{ type: "text", value: output }` for plain text ## Changes - `packages/opencode/src/session/message-v2.ts`: Added JSON parsing logic to `toModelOutput` - `packages/opencode/test/session/message-v2.test.ts`: Added tests for JSON string and plain text tool outputs ## Test Results All 15 tests pass: - 13 existing tests continue to pass - 2 new tests verify JSON string → object conversion and plain text handling ## Related - Fixes #11035 - Related to AWS SDK issue: https://github.com/aws/aws-sdk-js-v3/issues/7330 🤖 Generated with [Claude Code](https://claude.com/claude-code)
yindo added the pull-request label 2026-02-16 18:18:31 -05:00
yindo closed this issue 2026-02-16 18:18:31 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#13680