docs(examples): add code_execution_file_download example#1373
Open
ayeshakhalid192007-dev wants to merge 1 commit intoanthropics:mainfrom
Open
Conversation
Adds a runnable example showing the correct block type to inspect when extracting file_id from a code execution response. The bash_code_execution_tool_result block (not tool_use) holds the file output at block.content.content[i].file_id. Includes an inline comment explaining why the tool_use pattern fails.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
examples/code_execution_file_download.py, a runnable example demonstrating the correct way to extractfile_idfrom a code execution response and download the generated file usingclient.beta.files.download().Problem
Some documentation shows the following pattern for extracting
file_idafter code execution:This condition is never satisfied because server-side tools (
code_execution,web_search, etc.) produce a"server_tool_use"block for the invocation — not"tool_use". The generated file arrives in a separate block of type"bash_code_execution_tool_result". Users following this pattern waste API credits debugging a loop that never fires (ref #1360).Changes
examples/code_execution_file_download.py(new file, 79 lines)The example:
messages.create()request with thecode_execution_20250825server toolbash_code_execution_tool_resultfile_id:Why this is safe
examples/is changed — a directory explicitly excluded from generator overwrites perCONTRIBUTING.mdmessages.create,beta.files.download)Testing
Syntax check:
Lint (ruff, pyright, mypy):
Full test suite:
The 1 xfailed is a pre-existing
@pytest.mark.xfailontest_runners.py::test_custom_message_handlingunrelated to this change.Files changed
examples/code_execution_file_download.pyRelated issues
Closes #1360