Skip to content

Support context managers for agent cleanup #1422

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
merged 5 commits into from
Jun 23, 2025

Conversation

tobiasofsn
Copy link
Contributor

Support context managers for cleanup of resources used by agents.

  • Support context managers like with statements.
  • Support calling the agent's cleanup method directly.
  • Implement cleanup for the E2B executor. The E2B sandbox would previously stay alive for up to the default timeout of 5 minutes as per the E2B documentation.

Intended usage patterns

With

agent = CodeAgent(
    model=model,
    tools=[],
    executor_type="docker"
)

with agent:
    task = "... some task ..."
    agent.run(task)

With-As

with CodeAgent(model=model,
    tools=[],
    executor_type="docker"
) as agent:
    task = "... some task ..."
    agent.run(task)

Try-Finally

agent = CodeAgent(
    model=model,
    tools=[],
    executor_type="docker"
)

try:
    task = "... some task ..."
    agent.run(task)
finally:
    agent.cleanup()

Manual

agent = CodeAgent(
    model=model,
    tools=[],
    executor_type="docker"
)

task = "... some task ..."
agent.run(task)

agent.cleanup()

@albertvillanova albertvillanova requested a review from Copilot June 18, 2025 19:05
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for context managers to handle resource cleanup in agents. Key changes include:

  • Adding enter and exit methods to enable usage of the with statement for agents.
  • Introducing cleanup methods in remote executors (E2B and Docker) and a stub in the local Python executor.
  • Updating documentation to demonstrate proper context manager usage for both E2B and Docker executors.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/smolagents/remote_executors.py Added cleanup methods for sandbox shutdown and Docker container removal.
src/smolagents/local_python_executor.py Added a cleanup stub with documentation for the executor.
src/smolagents/agents.py Introduced context manager methods and cleanup methods for agent resource management.
docs/source/en/tutorials/secure_code_execution.mdx Updated documentation with examples using the with statement for agents.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Member

@albertvillanova albertvillanova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Useful enhancement!

Just made some slight modifications...

@albertvillanova albertvillanova merged commit 8ae9c79 into huggingface:main Jun 23, 2025
4 checks passed
@tobiasofsn tobiasofsn deleted the agent-cleanup branch June 26, 2025 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants