Skip to content

[BUG] CodeAgent doesn't work in non-main threads. #1339

@MaksimMisin

Description

@MaksimMisin

Describe the bug
The latest code agent version no longer works in a thread due to the use of signals. We are trying to use smolagents in a fastapi service, so having it run in a non-main thread is a pretty hard requirement (otherwise it blocks streaming for extended periods of time).

Making timeout decorator optional in local_python_executor fixes the issue. I'm happy to contribute a PR if you want.

Code to reproduce the error

import threading
from smolagents import CodeAgent, WebSearchTool, InferenceClientModel

model = InferenceClientModel()
agent = CodeAgent(tools=[WebSearchTool()], model=model, stream_outputs=True)


def run_agent():
    agent.run(
        "How many seconds would it take for a leopard at full speed to run through Pont des Arts?"
    )


thread = threading.Thread(target=run_agent)
thread.start()
thread.join()

Error logs (if any)
Interpreter fails with:

signal only works in main thread of the main interpreter

Packages version:
smolagents==1.16.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions