Skip to content

Python: [Bug]: anthropic bash tool result files leak out of the shell tool result #9342

Closed

Description

Observed Behavior

When the Anthropic Messages API returns a bash_code_execution_tool_result block that carries hosted output files, _parse_contents_from_anthropic appends the files to the message-level contents list instead of the shell result's outputs. The files end up as bare top-level hosted_file contents next to the shell_tool_result, instead of being nested inside it.

The twin code_execution_tool_result case does it right: its files go into code_outputs and end up inside from_code_interpreter_tool_result(outputs=...). Only the bash branch writes to the outer contents list.

Expected Behavior

Bash tool result files should be nested in the shell tool result's outputs, matching the code interpreter branch.

Steps to Reproduce

  1. Build a BetaBashCodeExecutionToolResultBlock whose content has one bash_code_execution_output with a file_id.
  2. Call RawAnthropicClient._parse_contents_from_anthropic with it.
  3. Inspect the returned contents.

Minimal Reproduction

from anthropic.types.beta.beta_bash_code_execution_output_block import BetaBashCodeExecutionOutputBlock
from anthropic.types.beta.beta_bash_code_execution_result_block import BetaBashCodeExecutionResultBlock
from anthropic.types.beta.beta_bash_code_execution_tool_result_block import BetaBashCodeExecutionToolResultBlock
from agent_framework_anthropic._chat_client import RawAnthropicClient

inner = BetaBashCodeExecutionResultBlock(
    content=[BetaBashCodeExecutionOutputBlock(file_id="file-123", type="bash_code_execution_output")],
    return_code=0, stderr="", stdout="ls -la", type="bash_code_execution_result",
)
block = BetaBashCodeExecutionToolResultBlock(content=inner, tool_use_id="toolu_1", type="bash_code_execution_tool_result")

contents = RawAnthropicClient(api_key="test-key")._parse_contents_from_anthropic([block])
# contents == [hosted_file, shell_tool_result]  -> the file escaped the tool result
# expected: one shell_tool_result whose outputs contain the hosted file

Error Messages and Stack Traces

No error. The file silently lands in the wrong place.

Package Versions

agent-framework-anthropic built from main (dcbbf79)

Python Version

Python 3.14.8

Operating System

macOS

Regression

Unknown

Additional Context

Looks like a copy-paste slip when the bash branch was written off the code interpreter branch. Anything that iterates tool result outputs (transports, ag-ui, replay) loses these files. Happy to send a PR.

Acknowledgements

  • I searched existing issues and did not find a duplicate.

Metadata

Metadata

Labels

pythonUsage: [Issues, PRs], Target: Python

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions