aw.tools¶
Tools for executing code and other agent actions.
Provides code execution capabilities with safe defaults and extensibility for robust backends like LangChain’s CodeInterpreter.
Functions
Create an executor using DSPy's code execution (if available). |
|
Create an executor using LangChain's CodeInterpreter (if available). |
Classes
|
Tool for executing Python code in a controlled environment. |
|
Result of code execution. |
|
Extra-safe code interpreter with restricted operations. |
- class aw.tools.CodeInterpreterTool(allowed_modules=None, global_context=None, executor=None)[source]¶
Bases:
objectTool for executing Python code in a controlled environment.
Provides a safe default implementation using exec() with limited namespace, and allows injection of more robust backends.
Example
>>> tool = CodeInterpreterTool() >>> result = tool("x = 5; y = x * 2; print(y)") >>> result.success True >>> result.output.strip() '10'
- class aw.tools.ExecutionResult(success, output='', error='', traceback_str='', result=None, locals_dict=None)[source]¶
Bases:
objectResult of code execution.
- success¶
Whether execution succeeded without exceptions
- output¶
Standard output captured during execution
- error¶
Error message if execution failed
- traceback¶
Full traceback if execution failed
- result¶
The return value or final expression value
- locals¶
Local variables after execution
- class aw.tools.SafeCodeInterpreter(**kwargs)[source]¶
Bases:
CodeInterpreterToolExtra-safe code interpreter with restricted operations.
Disallows file I/O, network access, and other potentially dangerous operations.
- aw.tools.create_dspy_executor()[source]¶
Create an executor using DSPy’s code execution (if available).
- Return type:
Callable[[str,dict],ExecutionResult]- Returns:
Executor function compatible with CodeInterpreterTool
- aw.tools.create_langchain_executor()[source]¶
Create an executor using LangChain’s CodeInterpreter (if available).
- Return type:
Callable[[str,dict],ExecutionResult]- Returns:
Executor function compatible with CodeInterpreterTool
Example
>>> try: ... executor = create_langchain_executor() ... tool = CodeInterpreterTool(executor=executor) ... except ImportError: ... # LangChain not available - use default ... tool = CodeInterpreterTool()