Skip to content

Commit 8500241

Browse files
committed
Allow serialization of Execution model in python
1 parent 8eac304 commit 8500241

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

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)