-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Python: fix anthropic code interpreter tool repr #2244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
eavanvalkenburg
merged 6 commits into
microsoft:main
from
eavanvalkenburg:fix_anthropic_code
Nov 17, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2044ade
fix anthropic code interpreter tool repr
eavanvalkenburg eeb3222
fixes
eavanvalkenburg 0cb9f0a
added skills and sample
eavanvalkenburg 697bef5
test fix
eavanvalkenburg 3717c38
add new sample to readme
eavanvalkenburg 6fbecaf
fixes tests
eavanvalkenburg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
88 changes: 88 additions & 0 deletions
88
python/samples/getting_started/agents/anthropic/anthropic_skills.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| import asyncio | ||
| import logging | ||
| from pathlib import Path | ||
|
|
||
| from agent_framework import HostedCodeInterpreterTool, HostedFileContent | ||
| from agent_framework.anthropic import AnthropicClient | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
| """ | ||
| Anthropic Skills Agent Example | ||
|
|
||
| This sample demonstrates using Anthropic with: | ||
| - Listing and using Anthropic-managed Skills. | ||
| - One approach to add additional beta flags. | ||
| You can also set additonal_chat_options with "additional_beta_flags" per request. | ||
| - Creating an agent with the Code Interpreter tool and a Skill. | ||
| - Catching and downloading generated files from the agent. | ||
| """ | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| """Example of streaming response (get results as they are generated).""" | ||
| client = AnthropicClient(additional_beta_flags=["skills-2025-10-02"]) | ||
|
|
||
| # List Anthropic-managed Skills | ||
| skills = await client.anthropic_client.beta.skills.list(source="anthropic", betas=["skills-2025-10-02"]) | ||
| for skill in skills.data: | ||
| print(f"{skill.source}: {skill.id} (version: {skill.latest_version})") | ||
|
|
||
| # Create a agent with the pptx skill enabled | ||
| # Skills also need the code interpreter tool to function | ||
| agent = client.create_agent( | ||
| name="DocsAgent", | ||
| instructions="You are a helpful agent for creating powerpoint presentations.", | ||
| tools=HostedCodeInterpreterTool(), | ||
| max_tokens=20000, | ||
| additional_chat_options={ | ||
| "thinking": {"type": "enabled", "budget_tokens": 10000}, | ||
| "container": {"skills": [{"type": "anthropic", "skill_id": "pptx", "version": "latest"}]}, | ||
| }, | ||
| ) | ||
|
|
||
| print( | ||
| "The agent output will use the following colors:\n" | ||
| "\033[0mUser: (default)\033[0m\n" | ||
| "\033[0mAgent: (default)\033[0m\n" | ||
| "\033[32mAgent Reasoning: (green)\033[0m\n" | ||
| "\033[34mUsage: (blue)\033[0m\n" | ||
| ) | ||
| query = "Create a presentation about renewable energy with 5 slides" | ||
| print(f"User: {query}") | ||
| print("Agent: ", end="", flush=True) | ||
| files: list[HostedFileContent] = [] | ||
| async for chunk in agent.run_stream(query): | ||
| for content in chunk.contents: | ||
| match content.type: | ||
| case "text": | ||
| print(content.text, end="", flush=True) | ||
| case "text_reasoning": | ||
| print(f"\033[32m{content.text}\033[0m", end="", flush=True) | ||
| case "usage": | ||
| print(f"\n\033[34m[Usage so far: {content.details}]\033[0m\n", end="", flush=True) | ||
| case "hosted_file": | ||
| # Catch generated files | ||
| files.append(content) | ||
| case _: | ||
| logger.debug("Unhandled content type: %s", content.type) | ||
| pass | ||
|
|
||
| print("\n") | ||
| if files: | ||
| # Save to a new file (will be in the folder where you are running this script) | ||
| # When running this sample multiple times, the files will be overritten | ||
| # Since I'm using the pptx skill, the files will be PowerPoint presentations | ||
| print("Generated files:") | ||
| for idx, file in enumerate(files): | ||
| file_content = await client.anthropic_client.beta.files.download( | ||
| file_id=file.file_id, betas=["files-api-2025-04-14"] | ||
| ) | ||
| with open(Path(__file__).parent / f"renewable_energy-{idx}.pptx", "wb") as f: | ||
| await file_content.write_to_file(f.name) | ||
| print(f"File {idx}: renewable_energy-{idx}.pptx saved to disk.") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
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.
Uh oh!
There was an error while loading. Please reload this page.