Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

Fix AI chat/generation showing false error when streaming response (#3978)

devin-ai-integration[bot]d5c504ada343
diff --git a/marimo/_server/api/endpoints/ai.py b/marimo/_server/api/endpoints/ai.py
index a9e5377675..a8335a1c45 100644
--- a/marimo/_server/api/endpoints/ai.py
+++ b/marimo/_server/api/endpoints/ai.py
@@ -171,7 +171,11 @@ def get_content(
RawMessageStreamEvent | ChatCompletionChunk | GenerateContentResponse
),
) -> str | None:
- if hasattr(response, "choices") and response.choices:
+ if (
+ hasattr(response, "choices")
+ and response.choices
+ and response.choices[0].delta
+ ):
return response.choices[0].delta.content # type: ignore
if hasattr(response, "text"):
diff --git a/tests/_server/api/endpoints/test_ai.py b/tests/_server/api/endpoints/test_ai.py
index 500dbcb7cb..09b82fef88 100644
--- a/tests/_server/api/endpoints/test_ai.py
+++ b/tests/_server/api/endpoints/test_ai.py
@@ -5,13 +5,13 @@ import unittest
from contextlib import contextmanager
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any
-from unittest.mock import MagicMock, patch
+from unittest.mock import MagicMock, Mock, patch
import pytest
from marimo._config.manager import UserConfigManager
from marimo._dependencies.dependencies import DependencyManager
-from marimo._server.api.endpoints.ai import make_stream_response
+from marimo._server.api.endpoints.ai import get_content, make_stream_response
from tests._server.conftest import get_session_config_manager
from tests._server.mocks import token_header, with_session
@@ -634,3 +634,33 @@ class TestStreamResponse(unittest.TestCase):
"messages"
][1]["content"]
assert prompt == "Help me create a dataframe"
+
+
+class TestGetContent(unittest.TestCase):
+ def test_get_content_with_none_delta(self) -> None:
+ # Create a mock response with choices but delta is None
+ mock_response = Mock()
+ mock_response.choices = [Mock()]
+ mock_response.choices[0].delta = None
+
+ # Ensure text attribute doesn't exist to avoid fallback
+ type(mock_response).text = property(lambda _: None)
+
+ # Call get_content with the mock response
+ result = get_content(mock_response)
+
+ # Assert that the result is None
+ assert result is None
+
+ def test_get_content_with_delta_content(self) -> None:
+ # Create a mock response with choices and delta.content
+ mock_response = Mock()
+ mock_response.choices = [Mock()]
+ mock_response.choices[0].delta = Mock()
+ mock_response.choices[0].delta.content = "Test content"
+
+ # Call get_content with the mock response
+ result = get_content(mock_response)
+
+ # Assert that the result is the expected content
+ assert result == "Test content"