Skip to content

Commit 8c6cb18

Browse files
authored
Merge pull request #13 from e2b-dev/serialization-of-execution-object
Allow serialization of Execution object
2 parents 815eed0 + 9efb303 commit 8c6cb18

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

js/src/messaging.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class ExecutionError {
2020
* The raw traceback of the error.
2121
**/
2222
public tracebackRaw: string[]
23-
) {}
23+
) { }
2424

2525
/**
2626
* Returns the traceback of the error as a string.
@@ -175,6 +175,25 @@ export class Result {
175175

176176
return formats
177177
}
178+
179+
/**
180+
* Returns the serializable representation of the result.
181+
*/
182+
toJSON() {
183+
return {
184+
text: this.text,
185+
html: this.html,
186+
markdown: this.markdown,
187+
svg: this.svg,
188+
png: this.png,
189+
jpeg: this.jpeg,
190+
pdf: this.pdf,
191+
latex: this.latex,
192+
json: this.json,
193+
javascript: this.javascript,
194+
...(Object.keys(this.extra).length > 0 ? { extra: this.extra } : {})
195+
}
196+
}
178197
}
179198

180199
/**
@@ -208,7 +227,7 @@ export class Execution {
208227
* An Error object if an error occurred, null otherwise.
209228
*/
210229
public error?: ExecutionError
211-
) {}
230+
) { }
212231

213232
/**
214233
* Returns the text representation of the main result of the cell.
@@ -220,6 +239,17 @@ export class Execution {
220239
}
221240
}
222241
}
242+
243+
/**
244+
* Returns the serializable representation of the execution result.
245+
*/
246+
toJSON() {
247+
return {
248+
results: this.results,
249+
logs: this.logs,
250+
error: this.error
251+
}
252+
}
223253
}
224254

225255
/**
@@ -275,7 +305,7 @@ export class JupyterKernelWebSocket {
275305
* Does not start WebSocket connection!
276306
* You need to call connect() method first.
277307
*/
278-
constructor(private readonly url: string) {}
308+
constructor(private readonly url: string) { }
279309

280310
// public
281311
/**

python/e2b_code_interpreter/models.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import copy
22
from typing import List, Optional, Iterable, Dict
3-
from pydantic import BaseModel
3+
from pydantic import BaseModel, field_serializer
44

55

66
class Error(BaseModel):
@@ -234,6 +234,25 @@ def text(self) -> Optional[str]:
234234
if d.is_main_result:
235235
return d.text
236236

237+
def to_json(self) -> str:
238+
"""
239+
Returns the JSON representation of the Execution object.
240+
"""
241+
return self.model_dump_json(exclude_none=True)
242+
243+
@field_serializer("results", when_used="json")
244+
def serialize_results(results: List[Result]) -> List[Dict[str, str]]:
245+
"""
246+
Serializes the results to JSON.
247+
This method is used by the Pydantic JSON encoder.
248+
"""
249+
serialized = []
250+
for result in results:
251+
serialized_dict = {key: result[key] for key in result.formats()}
252+
serialized_dict['text'] = result.text
253+
serialized.append(serialized_dict)
254+
return serialized
255+
237256

238257
class KernelException(Exception):
239258
"""

0 commit comments

Comments
 (0)