11import logging
22import httpx
33
4- from typing import Optional , Dict , overload
4+ from typing import Optional , Dict , overload , Union , Literal
55from httpx import AsyncClient
66
77from e2b import (
3434
3535
3636class AsyncSandbox (BaseAsyncSandbox ):
37+ """
38+ E2B cloud sandbox is a secure and isolated cloud environment.
39+
40+ The sandbox allows you to:
41+ - Access Linux OS
42+ - Create, list, and delete files and directories
43+ - Run commands
44+ - Run isolated code
45+ - Access the internet
46+
47+ Check docs [here](https://e2b.dev/docs).
48+
49+ Use the `AsyncSandbox.create()` to create a new sandbox.
50+
51+ Example:
52+ ```python
53+ from e2b_code_interpreter import AsyncSandbox
54+ sandbox = await AsyncSandbox.create()
55+ ```
56+ """
57+
3758 default_template = DEFAULT_TEMPLATE
3859
3960 def __init__ (self , sandbox_id : str , connection_config : ConnectionConfig ):
@@ -51,34 +72,75 @@ def _client(self) -> AsyncClient:
5172 async def run_code (
5273 self ,
5374 code : str ,
54- language : Optional [ str ] = None ,
75+ language : Union [ Literal [ "python" ], None ] = None ,
5576 on_stdout : Optional [OutputHandler [OutputMessage ]] = None ,
5677 on_stderr : Optional [OutputHandler [OutputMessage ]] = None ,
5778 on_result : Optional [OutputHandler [Result ]] = None ,
5879 on_error : Optional [OutputHandler [ExecutionError ]] = None ,
5980 envs : Optional [Dict [str , str ]] = None ,
6081 timeout : Optional [float ] = None ,
6182 request_timeout : Optional [float ] = None ,
62- ) -> Execution : ...
83+ ) -> Execution :
84+ """
85+ Runs the code as Python.
86+
87+ Specify the `language` or `context` option to run the code as a different language or in a different `Context`.
88+
89+ You can reference previously defined variables, imports, and functions in the code.
90+
91+ :param code: Code to execute
92+ :param language: Language to use for code execution. If not defined, the default Python context is used.
93+ :param on_stdout: Callback for stdout messages
94+ :param on_stderr: Callback for stderr messages
95+ :param on_result: Callback for the `Result` object
96+ :param on_error: Callback for the `ExecutionError` object
97+ :param envs: Custom environment variables
98+ :param timeout: Timeout for the code execution in **seconds**
99+ :param request_timeout: Timeout for the request in **seconds**
100+
101+ :return: `Execution` result object
102+ """
103+ ...
63104
64105 @overload
65106 async def run_code (
66107 self ,
67108 code : str ,
68- context : Optional [Context ] = None ,
109+ language : Optional [str ] = None ,
69110 on_stdout : Optional [OutputHandler [OutputMessage ]] = None ,
70111 on_stderr : Optional [OutputHandler [OutputMessage ]] = None ,
71112 on_result : Optional [OutputHandler [Result ]] = None ,
72113 on_error : Optional [OutputHandler [ExecutionError ]] = None ,
73114 envs : Optional [Dict [str , str ]] = None ,
74115 timeout : Optional [float ] = None ,
75116 request_timeout : Optional [float ] = None ,
76- ) -> Execution : ...
117+ ) -> Execution :
118+ """
119+ Runs the code for the specified language.
120+
121+ Specify the `language` or `context` option to run the code as a different language or in a different `Context`.
122+ If no language is specified, Python is used.
123+
124+ You can reference previously defined variables, imports, and functions in the code.
77125
126+ :param code: Code to execute
127+ :param language: Language to use for code execution. If not defined, the default Python context is used.
128+ :param on_stdout: Callback for stdout messages
129+ :param on_stderr: Callback for stderr messages
130+ :param on_result: Callback for the `Result` object
131+ :param on_error: Callback for the `ExecutionError` object
132+ :param envs: Custom environment variables
133+ :param timeout: Timeout for the code execution in **seconds**
134+ :param request_timeout: Timeout for the request in **seconds**
135+
136+ :return: `Execution` result object
137+ """
138+ ...
139+
140+ @overload
78141 async def run_code (
79142 self ,
80143 code : str ,
81- language : Optional [str ] = None ,
82144 context : Optional [Context ] = None ,
83145 on_stdout : Optional [OutputHandler [OutputMessage ]] = None ,
84146 on_stderr : Optional [OutputHandler [OutputMessage ]] = None ,
@@ -89,21 +151,39 @@ async def run_code(
89151 request_timeout : Optional [float ] = None ,
90152 ) -> Execution :
91153 """
92- Runs the code in the specified language/context, if not specified, the default context is used.
154+ Runs the code in the specified context, if not specified, the default context is used.
155+
156+ Specify the `language` or `context` option to run the code as a different language or in a different `Context`.
157+
93158 You can reference previously defined variables, imports, and functions in the code.
94159
95- :param code: The code to execute
96- :param language Based on the value, a default context for the language is used. If not defined and no context is provided, the default Python context is used.
97- :param context Concrete context to run the code in. If not specified, the default context for the language is used. It's mutually exclusive with the language.
160+ :param code: Code to execute
161+ :param context: Concrete context to run the code in. If not specified, the default context for the language is used. It's mutually exclusive with the language.
98162 :param on_stdout: Callback for stdout messages
99163 :param on_stderr: Callback for stderr messages
100164 :param on_result: Callback for the `Result` object
101165 :param on_error: Callback for the `ExecutionError` object
102- :param envs: Environment variables
103- :param timeout: Max time to wait for the execution to finish
104- :param request_timeout: Max time to wait for the request to finish
105- :return: Execution object
166+ :param envs: Custom environment variables
167+ :param timeout: Timeout for the code execution in **seconds**
168+ :param request_timeout: Timeout for the request in **seconds**
169+
170+ :return: `Execution` result object
106171 """
172+ ...
173+
174+ async def run_code (
175+ self ,
176+ code : str ,
177+ language : Optional [str ] = None ,
178+ context : Optional [Context ] = None ,
179+ on_stdout : Optional [OutputHandler [OutputMessage ]] = None ,
180+ on_stderr : Optional [OutputHandler [OutputMessage ]] = None ,
181+ on_result : Optional [OutputHandler [Result ]] = None ,
182+ on_error : Optional [OutputHandler [ExecutionError ]] = None ,
183+ envs : Optional [Dict [str , str ]] = None ,
184+ timeout : Optional [float ] = None ,
185+ request_timeout : Optional [float ] = None ,
186+ ) -> Execution :
107187 logger .debug (f"Executing code { code } " )
108188
109189 if context and language :
@@ -154,17 +234,16 @@ async def create_code_context(
154234 self ,
155235 cwd : Optional [str ] = None ,
156236 language : Optional [str ] = None ,
157- envs : Optional [Dict [str , str ]] = None ,
158237 request_timeout : Optional [float ] = None ,
159238 ) -> Context :
160239 """
161240 Creates a new context to run code in.
162241
163- :param cwd: Set the current working directory for the context
164- :param language: Language of the context. If not specified, the default Python context is used.
165- :param envs: Environment variables
166- :param request_timeout: Max time to wait for the request to finish
167- :return: Context id
242+ :param cwd: Set the current working directory for the context, defaults to `/home/user`
243+ :param language: Language of the context. If not specified, defaults to Python
244+ :param request_timeout: Timeout for the request in **milliseconds**
245+
246+ :return: Context object
168247 """
169248 logger .debug (f"Creating new { language } context" )
170249
@@ -173,8 +252,6 @@ async def create_code_context(
173252 data ["language" ] = language
174253 if cwd :
175254 data ["cwd" ] = cwd
176- if envs :
177- data ["env_vars" ] = envs
178255
179256 try :
180257 response = await self ._client .post (
0 commit comments