Skip to content

Commit 32ade47

Browse files
committed
Make pydantic v1 compatible
1 parent ed315f0 commit 32ade47

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

python/e2b_code_interpreter/models.py

Lines changed: 17 additions & 14 deletions
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, field_serializer
3+
from pydantic import BaseModel
44

55

66
class Error(BaseModel):
@@ -211,13 +211,29 @@ class Logs(BaseModel):
211211
"List of strings printed to stderr by prints, subprocesses, etc."
212212

213213

214+
def serialize_results(results: List[Result]) -> List[Dict[str, str]]:
215+
"""
216+
Serializes the results to JSON.
217+
This method is used by the Pydantic JSON encoder.
218+
"""
219+
serialized = []
220+
for result in results:
221+
serialized_dict = {key: result[key] for key in result.formats()}
222+
serialized_dict["text"] = result.text
223+
serialized.append(serialized_dict)
224+
return serialized
225+
226+
214227
class Execution(BaseModel):
215228
"""
216229
Represents the result of a cell execution.
217230
"""
218231

219232
class Config:
220233
arbitrary_types_allowed = True
234+
json_encoders = {
235+
List[Result]: serialize_results,
236+
}
221237

222238
results: List[Result] = []
223239
"List of the result of the cell (interactively interpreted last line), display calls (e.g. matplotlib plots)."
@@ -245,19 +261,6 @@ def to_json(self) -> str:
245261
"""
246262
return self.model_dump_json(exclude_none=True)
247263

248-
@field_serializer("results", when_used="json")
249-
def serialize_results(results: List[Result]) -> List[Dict[str, str]]:
250-
"""
251-
Serializes the results to JSON.
252-
This method is used by the Pydantic JSON encoder.
253-
"""
254-
serialized = []
255-
for result in results:
256-
serialized_dict = {key: result[key] for key in result.formats()}
257-
serialized_dict["text"] = result.text
258-
serialized.append(serialized_dict)
259-
return serialized
260-
261264

262265
class KernelException(Exception):
263266
"""

python/poetry.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ packages = [{ include = "e2b_code_interpreter" }]
1212
[tool.poetry.dependencies]
1313
python = "^3.8"
1414

15-
pydantic = ">1, <3"
15+
pydantic = "*"
1616
websocket-client = "^1.7.0"
1717
e2b = ">=0.17.1"
1818

0 commit comments

Comments
 (0)