diff --git a/python/packages/anthropic/agent_framework_anthropic/_chat_client.py b/python/packages/anthropic/agent_framework_anthropic/_chat_client.py index 146b9f6a998..01cd6b021dc 100644 --- a/python/packages/anthropic/agent_framework_anthropic/_chat_client.py +++ b/python/packages/anthropic/agent_framework_anthropic/_chat_client.py @@ -1527,7 +1527,7 @@ def _parse_contents_from_anthropic( ) ) for bash_file_content in content_block.content.content: - contents.append( + shell_outputs.append( Content.from_hosted_file( file_id=bash_file_content.file_id, raw_representation=bash_file_content, diff --git a/python/packages/anthropic/tests/test_anthropic_client.py b/python/packages/anthropic/tests/test_anthropic_client.py index c91a37765ed..48e2a8917b3 100644 --- a/python/packages/anthropic/tests/test_anthropic_client.py +++ b/python/packages/anthropic/tests/test_anthropic_client.py @@ -4251,3 +4251,33 @@ def test_option_translations_single_stop_string_becomes_list() -> None: options = {"stop": ["END", "STOP"]} _apply_option_translations(options) assert options == {"stop_sequences": ["END", "STOP"]} + + +def test_parse_bash_execution_result_with_files( + mock_anthropic_client: MagicMock, +) -> None: + """Bash result files stay nested in the shell tool result outputs (#9342).""" + client = create_test_anthropic_client(mock_anthropic_client) + + mock_file = MagicMock() + mock_file.file_id = "file_456" + + mock_content = MagicMock() + mock_content.stdout = "Output text" + mock_content.stderr = None + mock_content.return_code = 0 + mock_content.content = [mock_file] + + mock_block = MagicMock() + mock_block.type = "bash_code_execution_tool_result" + mock_block.tool_use_id = "call_bash4" + mock_block.content = mock_content + + result = client._parse_contents_from_anthropic([mock_block]) + + assert len(result) == 1 + assert result[0].type == "shell_tool_result" + assert result[0].outputs is not None + hosted_files = [output for output in result[0].outputs if output.type == "hosted_file"] + assert len(hosted_files) == 1 + assert hosted_files[0].file_id == "file_456"