Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/fix-result-data-serialization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@e2b/code-interpreter": patch
"@e2b/code-interpreter-python": patch
---

Fix `Result.toJSON()` and `Execution.to_json()` to preserve the `data` field when present, including empty objects. Previously, the JS SDK omitted `data` entirely from serialization, and the Python SDK dropped empty `data` objects due to a truthiness check.
1 change: 1 addition & 0 deletions packages/code-interpreter-js/src/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ export class Result {
latex: this.latex,
json: this.json,
javascript: this.javascript,
...(this.data !== undefined ? { data: this.data } : {}),
...(Object.keys(this.extra).length > 0 ? { extra: this.extra } : {}),
}
}
Expand Down
41 changes: 41 additions & 0 deletions packages/code-interpreter-js/tests/messaging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect, test } from 'vitest'

import { SandboxError } from '../src'
import { extractError } from '../src/messaging'
import { Result } from '../src/messaging'

test('includes the response body in server errors', async () => {
const error = await extractError(
Expand All @@ -25,3 +26,43 @@ test('falls back to the status text for an empty body', async () => {

expect(error).toEqual(new SandboxError('500 Internal Server Error'))
})

test('Result.toJSON includes data field when present', () => {
const result = new Result(
{
text: 'table',
data: { columns: ['answer'], rows: [[42]] },
},
true
)

const json = result.toJSON()
expect(json).toHaveProperty('data')
expect(json.data).toEqual({ columns: ['answer'], rows: [[42]] })
})

test('Result.toJSON includes empty data field', () => {
const result = new Result(
{
text: 'table',
data: {},
},
true
)

const json = result.toJSON()
expect(json).toHaveProperty('data')
expect(json.data).toEqual({})
})

test('Result.toJSON omits data field when undefined', () => {
const result = new Result(
{
text: 'table',
},
true
)

const json = result.toJSON()
expect(json).not.toHaveProperty('data')
})
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def formats(self) -> Iterable[str]:
formats.append("json")
if self.javascript:
formats.append("javascript")
if self.data:
if self.data is not None:
formats.append("data")
if self.chart:
formats.append("chart")
Expand Down
30 changes: 30 additions & 0 deletions packages/code-interpreter-python/tests/charts/test_json.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json

from e2b_code_interpreter.code_interpreter_async import AsyncSandbox
from e2b_code_interpreter.models import Result, Execution

code = """
import matplotlib.pyplot as plt
Expand All @@ -25,3 +26,32 @@ async def test_scatter_chart(async_sandbox: AsyncSandbox):
assert isinstance(serialized, str)

assert json.loads(serialized)["results"][0]["chart"]["type"] == "scatter"


def test_result_to_json_includes_data_field():
"""Test that Result.to_json includes data field when present"""
result = Result(text="table", data={"columns": ["answer"], "rows": [[42]]})
execution = Execution(results=[result])
serialized = execution.to_json()
data = json.loads(serialized)
assert "data" in data["results"][0]
assert data["results"][0]["data"] == {"columns": ["answer"], "rows": [[42]]}


def test_result_to_json_includes_empty_data_field():
"""Test that Result.to_json includes empty data field"""
result = Result(text="table", data={})
execution = Execution(results=[result])
serialized = execution.to_json()
data = json.loads(serialized)
assert "data" in data["results"][0]
assert data["results"][0]["data"] == {}


def test_result_to_json_omits_data_field_when_none():
"""Test that Result.to_json omits data field when None"""
result = Result(text="table", data=None)
execution = Execution(results=[result])
serialized = execution.to_json()
data = json.loads(serialized)
assert "data" not in data["results"][0]