Skip to content

Commit d95defa

Browse files
Python: fix(anthropic): nest bash tool result files inside the shell tool result (microsoft#9343)
* Python: fix(anthropic): nest bash tool result files inside the shell tool result The bash branch appended hosted output files to the message-level contents list instead of the shell result outputs, so the files leaked out as bare top-level hosted_file contents. The code interpreter twin branch already nested them correctly. Fixes microsoft#9342 Signed-off-by: Manohar Paturi <[email protected]> * Python: guard the bash files test against the optional outputs list Signed-off-by: Manohar Paturi <[email protected]> --------- Signed-off-by: Manohar Paturi <[email protected]> Co-authored-by: Manohar Paturi <[email protected]>
1 parent f34d6d6 commit d95defa

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

‎python/packages/anthropic/agent_framework_anthropic/_chat_client.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1563,7 +1563,7 @@ def _parse_contents_from_anthropic(
15631563
)
15641564
)
15651565
for bash_file_content in content_block.content.content:
1566-
contents.append(
1566+
shell_outputs.append(
15671567
Content.from_hosted_file(
15681568
file_id=bash_file_content.file_id,
15691569
raw_representation=bash_file_content,

‎python/packages/anthropic/tests/test_anthropic_client.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4269,3 +4269,33 @@ def test_option_translations_single_stop_string_becomes_list() -> None:
42694269
options = {"stop": ["END", "STOP"]}
42704270
_apply_option_translations(options)
42714271
assert options == {"stop_sequences": ["END", "STOP"]}
4272+
4273+
4274+
def test_parse_bash_execution_result_with_files(
4275+
mock_anthropic_client: MagicMock,
4276+
) -> None:
4277+
"""Bash result files stay nested in the shell tool result outputs (#9342)."""
4278+
client = create_test_anthropic_client(mock_anthropic_client)
4279+
4280+
mock_file = MagicMock()
4281+
mock_file.file_id = "file_456"
4282+
4283+
mock_content = MagicMock()
4284+
mock_content.stdout = "Output text"
4285+
mock_content.stderr = None
4286+
mock_content.return_code = 0
4287+
mock_content.content = [mock_file]
4288+
4289+
mock_block = MagicMock()
4290+
mock_block.type = "bash_code_execution_tool_result"
4291+
mock_block.tool_use_id = "call_bash4"
4292+
mock_block.content = mock_content
4293+
4294+
result = client._parse_contents_from_anthropic([mock_block])
4295+
4296+
assert len(result) == 1
4297+
assert result[0].type == "shell_tool_result"
4298+
assert result[0].outputs is not None
4299+
hosted_files = [output for output in result[0].outputs if output.type == "hosted_file"]
4300+
assert len(hosted_files) == 1
4301+
assert hosted_files[0].file_id == "file_456"

0 commit comments

Comments
 (0)