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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: ai endpoints from msgspec migration (more fixes) (#6278)
Myles Scolnickdf7e06726e99
diff --git a/marimo/_ai/_types.py b/marimo/_ai/_types.pyindex a7e207bcb9..96bf00ca91 100644--- a/marimo/_ai/_types.py+++ b/marimo/_ai/_types.py@@ -2,12 +2,23 @@from __future__ import annotationsimport abc+import dataclassesimport mimetypesfrom dataclasses import dataclass-from typing import TYPE_CHECKING, Any, Literal, Optional, TypedDict, Union+from typing import (+ TYPE_CHECKING,+ Any,+ Literal,+ Optional,+ TypedDict,+ Union,+ cast,+)import msgspec+from marimo._utils.parse_dataclass import parse_raw+class ChatAttachmentDict(TypedDict):url: str@@ -123,7 +134,7 @@ class ToolInvocationPart:tool_call_id: strstate: Union[str, Literal["output-available"]]input: dict[str, Any]- output: Optional[Any]+ output: Optional[Any] = None@propertydef tool_name(self) -> str:@@ -136,8 +147,8 @@ class FilePart:type: Literal["file"]media_type: str- filename: Optional[str]url: str+ filename: Optional[str] = Noneif TYPE_CHECKING:@@ -145,6 +156,8 @@ if TYPE_CHECKING:else:ChatPart = dict[str, Any]+PART_TYPES = [TextPart, ReasoningPart, ToolInvocationPart, FilePart]+class ChatMessage(msgspec.Struct):"""@@ -165,6 +178,25 @@ class ChatMessage(msgspec.Struct):# TODO: Make this requiredparts: Optional[list[ChatPart]] = None+ def __post_init__(self) -> None:+ # Hack: msgspec only supports discriminated unions. This is a hack to just+ # iterate through possible part variants and decode until one works.+ if self.parts:+ self.parts = [self._convert_part(part) for part in self.parts]++ def _convert_part(self, part: Any) -> ChatPart:+ for PartType in PART_TYPES:+ try:+ if dataclasses.is_dataclass(part):+ return cast(ChatPart, part)+ return parse_raw(part, cls=PartType, allow_unknown_keys=True)+ except Exception:+ continue++ raise msgspec.DecodeError(+ f"Could not decode part as any of {PART_TYPES}"+ )+@dataclassclass ChatModelConfig:diff --git a/tests/_server/api/endpoints/test_ai.py b/tests/_server/api/endpoints/test_ai.pyindex 6caee24f5d..47bbd28e61 100644--- a/tests/_server/api/endpoints/test_ai.py+++ b/tests/_server/api/endpoints/test_ai.py@@ -68,6 +68,23 @@ class FakeChoices:choices: list[Choice]+def _create_messages(prompt: str) -> list[dict[str, Any]]:+ return [+ {+ "role": "user",+ "content": prompt,+ "parts": [+ {"type": "text", "text": prompt},+ {+ "type": "file",+ "mediaType": "text/csv",+ "url": "data:text/csv;base64,R29vZGJ5ZQ==",+ },+ ],+ },+ ]++@pytest.mark.skipif(not HAS_OPEN_AI_DEPS, reason="optional dependencies not installed")@@ -769,7 +786,7 @@ def test_chat_without_code(client: TestClient) -> None:"/api/ai/chat",headers=HEADERS,json={- "messages": [{"role": "user", "content": "Hello"}],+ "messages": _create_messages("Hello"),"model": "gpt-4-turbo","variables": [],"includeOtherCode": "",@@ -810,12 +827,7 @@ def test_chat_with_code(client: TestClient) -> None:"/api/ai/chat",headers=HEADERS,json={- "messages": [- {- "role": "user",- "content": "Help me create a dataframe",- }- ],+ "messages": _create_messages("Help me create a dataframe"),"model": "gpt-4-turbo","variables": [],"includeOtherCode": "import pandas as pd",