diff --git a/src/lib/components/chat/Messages/CodeBlock.svelte b/src/lib/components/chat/Messages/CodeBlock.svelte
index 2ac9a335e..202e4f458 100644
--- a/src/lib/components/chat/Messages/CodeBlock.svelte
+++ b/src/lib/components/chat/Messages/CodeBlock.svelte
@@ -396,7 +396,7 @@ __builtins__.input = input`);
{#if result}
RESULT
-
{`${result}`}
+
{`${JSON.stringify(result)}`}
{/if}
{/if}
diff --git a/src/lib/workers/pyodide.worker.ts b/src/lib/workers/pyodide.worker.ts
index 4c0e9d4ed..92f5258e2 100644
--- a/src/lib/workers/pyodide.worker.ts
+++ b/src/lib/workers/pyodide.worker.ts
@@ -62,11 +62,10 @@ self.onmessage = async (event) => {
try {
self.result = await self.pyodide.runPythonAsync(code);
- try {
- self.result = self.result.toJSON();
- } catch (error) {
- console.error(error);
- }
+ // Safely process and recursively serialize the result
+ self.result = processResult(self.result);
+
+ console.log('Python result:', self.result);
} catch (error) {
self.stderr = error.toString();
}
@@ -74,4 +73,45 @@ self.onmessage = async (event) => {
self.postMessage({ id, result: self.result, stdout: self.stdout, stderr: self.stderr });
};
+function processResult(result: any): any {
+ // Handle null and undefined
+ if (result == null) {
+ return result;
+ }
+
+ // Handle primitive types
+ if (typeof result !== 'object') {
+ return result;
+ }
+
+ // Handle Date objects
+ if (result instanceof Date) {
+ return result.toISOString();
+ }
+
+ // Handle Arrays
+ if (Array.isArray(result)) {
+ return result.map((item) => processResult(item));
+ }
+
+ // Handle Proxy objects (assuming they're from Pyodide)
+ if (typeof result.toJs === 'function') {
+ return processResult(result.toJs());
+ }
+
+ // Handle regular objects
+ if (typeof result === 'object') {
+ const processedObject: { [key: string]: any } = {};
+ for (const key in result) {
+ if (Object.prototype.hasOwnProperty.call(result, key)) {
+ processedObject[key] = processResult(result[key]);
+ }
+ }
+ return processedObject;
+ }
+
+ // If we can't process it, return null or a placeholder
+ return null;
+}
+
export default {};