🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
fix(bedrock): wrap list-typed tool results so toolResult.content[].json is an object (#4027)
Alvin Tangf380bdfaf4b9
diff --git a/camel/models/aws_bedrock_converse_model.py b/camel/models/aws_bedrock_converse_model.pyindex 9c9cf608a..5aa015197 100644--- a/camel/models/aws_bedrock_converse_model.py+++ b/camel/models/aws_bedrock_converse_model.py@@ -224,18 +224,21 @@ class AWSBedrockConverseModel(BaseModelBackend):@staticmethoddef _parse_json_or_text(value: Any) -> Dict[str, Any]:+ # Bedrock Converse requires `toolResult.content[].json` objects.if isinstance(value, dict):return {"json": value}if isinstance(value, list):- return {"json": value}+ return {"json": {"__camel_tool_result__": value}}if isinstance(value, str):stripped = value.strip()if not stripped:return {"text": ""}try:parsed = json.loads(stripped)- if isinstance(parsed, (dict, list)):+ if isinstance(parsed, dict):return {"json": parsed}+ if isinstance(parsed, list):+ return {"json": {"__camel_tool_result__": parsed}}return {"text": stripped}except Exception:return {"text": value}diff --git a/test/models/test_aws_bedrock_converse_model.py b/test/models/test_aws_bedrock_converse_model.pyindex 01b58b89f..4a25d0489 100644--- a/test/models/test_aws_bedrock_converse_model.py+++ b/test/models/test_aws_bedrock_converse_model.py@@ -164,6 +164,70 @@ def test_parse_json_or_text_scalar_json_is_text():assert model._parse_json_or_text("true") == {"text": "true"}[email protected]_backend+def test_parse_json_or_text_top_level_list_is_wrapped_in_object():+ """Bedrock Converse requires an object at `toolResult.content[].json`."""+ model = _make_model(bedrock_client=object())++ assert model._parse_json_or_text([]) == {+ "json": {"__camel_tool_result__": []}+ }+ assert model._parse_json_or_text("[]") == {+ "json": {"__camel_tool_result__": []}+ }+ assert model._parse_json_or_text([{"id": 1}, {"id": 2}]) == {+ "json": {"__camel_tool_result__": [{"id": 1}, {"id": 2}]}+ }++[email protected]_backend+def test_converse_tool_message_with_list_payload_produces_object():+ """End-to-end check: a `tool` message whose content is a JSON list+ must produce a Bedrock `toolResult.content[0].json` whose value is a+ JSON object (not a list). See issue #3962.+ """+ model = _make_model(bedrock_client=object())++ request = model._build_converse_request(+ messages=[+ {"role": "user", "content": "find papers"},+ {+ "role": "assistant",+ "content": "",+ "tool_calls": [+ {+ "id": "call_1",+ "type": "function",+ "function": {+ "name": "search_papers",+ "arguments": "{}",+ },+ }+ ],+ },+ {+ "role": "tool",+ "tool_call_id": "call_1",+ "content": "[]",+ },+ ],+ tools=[+ {+ "type": "function",+ "function": {+ "name": "search_papers",+ "parameters": {"type": "object"},+ },+ }+ ],+ )++ tool_result_block = request["messages"][2]["content"][0]+ assert "toolResult" in tool_result_block+ payload = tool_result_block["toolResult"]["content"][0]+ assert payload == {"json": {"__camel_tool_result__": []}}++@pytest.mark.model_backenddef test_converse_stream_is_supported():class DummyEventStream: