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
implement pydantic-ai provider for mo.ui.chat (#7636)
Shahmir Varqha53221f364667
diff --git a/examples/ai/chat/pydantic-ai-chat.py b/examples/ai/chat/pydantic-ai-chat.pynew file mode 100644index 0000000000..3e197e2917--- /dev/null+++ b/examples/ai/chat/pydantic-ai-chat.py@@ -0,0 +1,194 @@+import marimo++__generated_with = "0.18.4"+app = marimo.App(width="medium")++with app.setup(hide_code=True):+ import marimo as mo+ import os+ import httpx++ from pydantic_ai import Agent, RunContext, BinaryImage+ from pydantic_ai.models.google import GoogleModel, GoogleModelSettings+ from pydantic_ai.providers.google import GoogleProvider+ from pydantic_ai.models.anthropic import AnthropicModel, AnthropicModelSettings+ from pydantic_ai.providers.anthropic import AnthropicProvider+ from pydantic_ai.providers.openai import OpenAIProvider+ from pydantic_ai.models.openai import (+ OpenAIResponsesModel,+ OpenAIResponsesModelSettings,+ )+ from pydantic import BaseModel+ from pydantic_ai.models import Model+ from pydantic_ai.settings import ModelSettings++[email protected](hide_code=True)+def _():+ mo.md(r"""+ # Pydantic-AI 🤖++ [Pydantic AI](https://ai.pydantic.dev/) is a modern framework to build applications that interact with LLMs. Key features include++ * ✨ **Structured Outputs:** Force LLMs to return clean, structured data (like JSON) that conforms to your Pydantic models.+ * ✅ **Validation & Type-Safety:** Use Pydantic's validation and Python's type hints to ensure data integrity and make your code robust.+ * 🧠 **Reasoning & Tool Use:** Define output models for complex reasoning tasks and reliable function calling (tool use).++ The following example uses [`mo.ui.chat`](https://docs.marimo.io/api/inputs/chat.html#marimo.ui.chat) to build a chatbot backed by Pydantic-AI.+ """)+ return++[email protected](hide_code=True)+def _():+ structured = mo.ui.checkbox(label="Structured outputs")+ thinking = mo.ui.checkbox(label="Reasoning")+ fetch_dog_tool = mo.ui.checkbox(label="Fetch dog pics tool")++ models = mo.ui.dropdown(+ options={+ "Gemini 2.5 Flash": "gemini-2.5-flash",+ "Claude Haiku 4.5": "claude-haiku-4-5",+ "GPT 5 Nano": "gpt-5-nano",+ "GPT 5 (multimodal)": "gpt-5",+ },+ value="Gemini 2.5 Flash",+ label="Choose a model",+ )++ mo.vstack([models, structured, thinking, fetch_dog_tool])+ return fetch_dog_tool, models, structured, thinking++[email protected](hide_code=True)+def _(models):+ model_name = models.value+ if model_name.startswith("gemini"):+ env_key = "GOOGLE_AI_API_KEY"+ elif model_name.startswith("claude"):+ env_key = "ANTHROPIC_API_KEY"+ elif model_name.startswith("gpt"):+ env_key = "OPENAI_API_KEY"+ else:+ raise NotImplementedError++ os_key = os.environ.get(env_key)+ input_key = mo.ui.text(label="API key", kind="password")+ input_key if not os_key else None+ return input_key, model_name, os_key++[email protected](hide_code=True)+def get_model(+ model_name: str, thinking: bool, api_key: str+) -> tuple[Model, ModelSettings]:+ model_name = model_name.lower()++ if model_name.startswith("gemini"):+ provider = GoogleProvider(api_key=api_key)+ model = GoogleModel(model_name, provider=provider)+ settings = GoogleModelSettings(+ google_thinking_config={+ "include_thoughts": True if thinking else False+ }+ )+ elif model_name.startswith("claude"):+ model = AnthropicModel(+ model_name, provider=AnthropicProvider(api_key=api_key)+ )+ settings = AnthropicModelSettings(+ anthropic_thinking={"type": "enabled", "budget_tokens": 1024}+ if thinking+ else {"type": "disabled"},+ )+ elif model_name.startswith("gpt"):+ model = OpenAIResponsesModel(+ model_name, provider=OpenAIProvider(api_key=api_key)+ )+ settings = (+ OpenAIResponsesModelSettings(+ openai_reasoning_effort="low",+ openai_reasoning_summary="detailed",+ )+ if thinking+ else OpenAIResponsesModelSettings()+ )+ else:+ raise NotImplementedError++ return model, settings++[email protected](hide_code=True)+def _(+ fetch_dog_tool,+ input_key,+ model_name,+ models,+ os_key,+ structured,+ thinking,+):+ class CodeOutput(BaseModel):+ code: str+ time_complexity: str+ memory_complexity: str+ algorithm_complexity: int+++ api_key = input_key.value or os_key+ model, settings = get_model(models.value, thinking.value, api_key)++ output_type = str+ if "image" in model_name or model_name == "gpt-5":+ output_type = BinaryImage | str+ elif structured.value:+ output_type = [CodeOutput, str]++ agent = Agent(+ model,+ output_type=output_type,+ instructions="You are a senior software engineer experienced in Python, React and Typescript.",+ model_settings=settings,+ )++ if fetch_dog_tool.value:++ @agent.tool+ def fetch_dog_picture_url(ctx: RunContext[str]) -> str:+ """Returns URL of dog picture"""+ response_json = httpx.get(+ "https://dog.ceo/api/breeds/image/random"+ ).json()+ if "message" in response_json:+ return response_json["message"]+ else:+ return "Error fetching dog URL"+ return (agent,)+++def _(agent):+ chatbot = mo.ui.chat(+ mo.ai.llm.pydantic_ai(agent),+ prompts=[+ "Write the fibonacci function in Python",+ "Who is Ada Lovelace?",+ "What is marimo?",+ "I need dogs",+ ],+ allow_attachments=True,+ show_configuration_controls=True,+ )+ chatbot+ return (chatbot,)+++def _(chatbot):+ chatbot.value+ return+++if __name__ == "__main__":+ app.run()diff --git a/frontend/src/components/chat/chat-components.tsx b/frontend/src/components/chat/chat-components.tsxindex f541e99d78..8e8cdf285a 100644--- a/frontend/src/components/chat/chat-components.tsx+++ b/frontend/src/components/chat/chat-components.tsx@@ -14,7 +14,7 @@ export const AttachmentRenderer = ({return (<imgsrc={attachment.url}- alt={attachment.filename}+ alt={attachment.filename || "Attachment"}className="max-h-[100px] max-w-[100px] object-contain mb-1.5"/>);@@ -23,7 +23,7 @@ export const AttachmentRenderer = ({return (<div className="flex flex-row gap-1 items-center text-xs"><FileIcon className="h-3 w-3 mt-0.5" />- {attachment.filename}+ {attachment.filename || "Attachment"}</div>);};diff --git a/frontend/src/components/chat/chat-display.tsx b/frontend/src/components/chat/chat-display.tsxnew file mode 100644index 0000000000..c485f8dac0--- /dev/null+++ b/frontend/src/components/chat/chat-display.tsx@@ -0,0 +1,96 @@+/* Copyright 2026 Marimo. All rights reserved. */++import type { DataUIPart, ToolUIPart, UIMessage } from "ai";+import { logNever } from "@/utils/assertNever";+import { Logger } from "@/utils/Logger";+import { MarkdownRenderer } from "../markdown/markdown-renderer";+import { AttachmentRenderer } from "./chat-components";+import { ReasoningAccordion } from "./reasoning-accordion";+import { ToolCallAccordion } from "./tool-call-accordion";++export const renderUIMessage = ({+ message,+ isStreamingReasoning,+ isLast,+}: {+ message: UIMessage;+ isStreamingReasoning: boolean;+ isLast: boolean;+}) => {+ return (+ <>{message.parts.map((part, index) => renderUIMessagePart(part, index))}</>+ );++ function renderUIMessagePart(+ part: UIMessage["parts"][number],+ index: number,+ ) {+ if (isToolPart(part)) {+ return (+ <ToolCallAccordion+ key={index}+ index={index}+ toolName={part.type}+ result={part.output}+ className="my-2"+ state={part.state}+ input={part.input}+ />+ );+ }++ if (isDataPart(part)) {+ Logger.debug("Found data part", part);+ return null;+ }++ switch (part.type) {+ case "text":+ return <MarkdownRenderer key={index} content={part.text} />;+ case "reasoning":+ return (+ <ReasoningAccordion+ key={index}+ reasoning={part.text}+ index={index}+ isStreaming={+ isStreamingReasoning &&+ isLast &&+ // If there are multiple reasoning parts, only show the last one+ index === (message.parts.length || 0) - 1+ }+ />+ );+ case "file":+ return <AttachmentRenderer attachment={part} key={index} />;+ case "dynamic-tool":+ return (+ <ToolCallAccordion+ key={index}+ toolName={part.toolName}+ result={part.output}+ state={part.state}+ input={part.input}+ />+ );+ case "source-document":+ case "source-url":+ case "step-start":+ Logger.debug("Found non-renderable part", part);+ return null;+ default:+ logNever(part);+ return null;+ }+ }+};++function isToolPart(part: UIMessage["parts"][number]): part is ToolUIPart {+ return part.type.startsWith("tool-");+}++function isDataPart(+ part: UIMessage["parts"][number],+): part is DataUIPart<Record<string, unknown>> {+ return part.type.startsWith("data-");+}diff --git a/frontend/src/components/chat/chat-panel.tsx b/frontend/src/components/chat/chat-panel.tsxindex 359465136c..1cdc70021f 100644--- a/frontend/src/components/chat/chat-panel.tsx+++ b/frontend/src/components/chat/chat-panel.tsx@@ -4,12 +4,7 @@ import type { UIMessage } from "@ai-sdk/react";import { useChat } from "@ai-sdk/react";import { storePrompt } from "@marimo-team/codemirror-ai";import type { ReactCodeMirrorRef } from "@uiw/react-codemirror";-import {- DefaultChatTransport,- type FileUIPart,- type TextUIPart,- type ToolUIPart,-} from "ai";+import { DefaultChatTransport, type FileUIPart, type TextUIPart } from "ai";import { useAtom, useAtomValue, useSetAtom, useStore } from "jotai";import {AtSignIcon,@@ -23,7 +18,6 @@ import {} from "lucide-react";import { memo, useEffect, useRef, useState } from "react";import useEvent from "react-use-event-hook";-import { MarkdownRenderer } from "@/components/markdown/markdown-renderer";import { Button } from "@/components/ui/button";import {Select,@@ -71,6 +65,7 @@ import { Input } from "../ui/input";import { Tooltip, TooltipProvider } from "../ui/tooltip";import { toast } from "../ui/use-toast";import { AttachmentRenderer, FileAttachmentPill } from "./chat-components";+import { renderUIMessage } from "./chat-display";import { ChatHistoryPopover } from "./chat-history-popover";import {buildCompletionRequestBody,@@ -80,8 +75,6 @@ import {hasPendingToolCalls,isLastMessageReasoning,} from "./chat-utils";-import { ReasoningAccordion } from "./reasoning-accordion";-import { ToolCallAccordion } from "./tool-call-accordion";// Default mode for the AIconst DEFAULT_MODE = "manual";@@ -145,10 +138,6 @@ interface ChatMessageProps {isLast: boolean;}-function isToolPart(part: UIMessage["parts"][number]): part is ToolUIPart {- return part.type.startsWith("tool-");-}-const ChatMessageDisplay: React.FC<ChatMessageProps> = memo(({ message, index, onEdit, isStreamingReasoning, isLast }) => {const renderUserMessage = (message: UIMessage) => {@@ -197,84 +186,7 @@ const ChatMessageDisplay: React.FC<ChatMessageProps> = memo(<div className="absolute right-1 top-1 opacity-0 group-hover:opacity-100 transition-opacity"><CopyClipboardIcon className="h-3 w-3" value={content || ""} /></div>- {message.parts.map((part, i) => {- if (isToolPart(part)) {- return (- <ToolCallAccordion- key={i}- index={i}- toolName={part.type}- result={part.output}- className="my-2"- state={part.state}- input={part.input}- />- );- }-- switch (part.type) {- case "text":- return <MarkdownRenderer key={i} content={part.text} />;-- case "reasoning":- return (- <ReasoningAccordion- reasoning={part.text}- key={i}- index={i}- isStreaming={- isLast &&- isStreamingReasoning &&- // If there are multiple reasoning parts, only show the last one- i === (message.parts.length || 0) - 1- }- />- );-- case "dynamic-tool":- return (- <ToolCallAccordion- key={i}- index={i}- toolName={part.type}- result={part.output}- state={part.state}- input={part.input}- className="my-2"- />- );-- // These are cryptographic signatures, so we don't need to render them- case "data-reasoning-signature":- return null;-- // TODO: We could render lines, for now don't render them. They do not have information.- case "step-start":- Logger.debug("Found step-start part", part);- return null;-- /* handle other part types … */- default:- if (part.type.startsWith("data-")) {- Logger.log("Found data part", part);- return null;- }-- Logger.error("Unhandled part type:", part.type);- try {- return (- <div className="text-xs text-muted-foreground" key={i}>- <MarkdownRenderer- content={JSON.stringify(part, null, 2)}- />- </div>- );- } catch (error) {- Logger.error("Error rendering part:", part.type, error);- return null;- }- }- })}+ {renderUIMessage({ message, isStreamingReasoning, isLast })}</div>);};diff --git a/frontend/src/components/chat/chat-utils.ts b/frontend/src/components/chat/chat-utils.tsindex 0bd4593ff2..b08a5d5042 100644--- a/frontend/src/components/chat/chat-utils.ts+++ b/frontend/src/components/chat/chat-utils.ts@@ -98,6 +98,7 @@ export async function buildCompletionRequestBody(parts.push(...completionBody.attachments);}return {+ id: message.id,role: message.role,content: stringifyTextParts(message.parts), // This is no longer used in the backendparts,diff --git a/frontend/src/plugins/impl/chat/ChatPlugin.tsx b/frontend/src/plugins/impl/chat/ChatPlugin.tsxindex 212b788b4a..7454bb28c6 100644--- a/frontend/src/plugins/impl/chat/ChatPlugin.tsx+++ b/frontend/src/plugins/impl/chat/ChatPlugin.tsx@@ -1,5 +1,6 @@/* Copyright 2026 Marimo. All rights reserved. */+import type { UIMessage } from "ai";import { Suspense } from "react";import { z } from "zod";import { TooltipProvider } from "@/components/ui/tooltip";@@ -7,17 +8,36 @@ import { createPlugin } from "@/plugins/core/builder";import { rpc } from "@/plugins/core/rpc";import { Arrays } from "@/utils/arrays";import { Chatbot } from "./chat-ui";-import type { ChatMessage, SendMessageRequest } from "./types";+import type { SendMessageRequest } from "./types";// eslint-disable-next-line @typescript-eslint/consistent-type-definitionsexport type PluginFunctions = {- get_chat_history: (req: {}) => Promise<{ messages: ChatMessage[] }>;+ get_chat_history: (req: {}) => Promise<{ messages: UIMessage[] }>;delete_chat_history: (req: {}) => Promise<null>;delete_chat_message: (req: { index: number }) => Promise<null>;- send_prompt: (req: SendMessageRequest) => Promise<string>;+ send_prompt: (req: SendMessageRequest) => Promise<string | null>;};-export const ChatPlugin = createPlugin<{ messages: ChatMessage[] }>(+const messageSchema = z.array(+ z.object({+ id: z.string(),+ role: z.enum(["system", "user", "assistant"]),+ content: z.string().nullable(),+ parts: z.array(z.any()),+ metadata: z.any().nullable(),+ }),+);++const configSchema = z.object({+ max_tokens: z.number().nullable(),+ temperature: z.number().nullable(),+ top_p: z.number().nullable(),+ top_k: z.number().nullable(),+ frequency_penalty: z.number().nullable(),+ presence_penalty: z.number().nullable(),+});++export const ChatPlugin = createPlugin<{ messages: UIMessage[] }>("marimo-chatbot",).withData(@@ -25,27 +45,15 @@ export const ChatPlugin = createPlugin<{ messages: ChatMessage[] }>(prompts: z.array(z.string()).default(Arrays.EMPTY),showConfigurationControls: z.boolean(),maxHeight: z.number().optional(),- config: z.object({- max_tokens: z.number(),- temperature: z.number(),- top_p: z.number(),- top_k: z.number(),- frequency_penalty: z.number(),- presence_penalty: z.number(),- }),+ config: configSchema,allowAttachments: z.union([z.boolean(), z.string().array()]),+ frontendManaged: z.boolean(),}),).withFunctions<PluginFunctions>({get_chat_history: rpc.input(z.object({})).output(z.object({- messages: z.array(- z.object({- role: z.enum(["system", "user", "assistant"]),- content: z.string(),- parts: z.array(z.any()).nullable(),- }),- ),+ messages: messageSchema,}),),delete_chat_history: rpc.input(z.object({})).output(z.null()),@@ -55,24 +63,11 @@ export const ChatPlugin = createPlugin<{ messages: ChatMessage[] }>(send_prompt: rpc.input(z.object({- messages: z.array(- z.object({- role: z.enum(["system", "user", "assistant"]),- content: z.string(),- parts: z.array(z.any()),- }),- ),- config: z.object({- max_tokens: z.number(),- temperature: z.number(),- top_p: z.number(),- top_k: z.number(),- frequency_penalty: z.number(),- presence_penalty: z.number(),- }),+ messages: messageSchema,+ config: configSchema,}),)- .output(z.string()),+ .output(z.union([z.string(), z.null()])),}).renderer((props) => (<TooltipProvider>@@ -82,6 +77,7 @@ export const ChatPlugin = createPlugin<{ messages: ChatMessage[] }>(showConfigurationControls={props.data.showConfigurationControls}maxHeight={props.data.maxHeight}allowAttachments={props.data.allowAttachments}+ frontendManaged={props.data.frontendManaged}config={props.data.config}get_chat_history={props.functions.get_chat_history}delete_chat_history={props.functions.delete_chat_history}diff --git a/frontend/src/plugins/impl/chat/chat-ui.tsx b/frontend/src/plugins/impl/chat/chat-ui.tsxindex c70b7a327f..505b3e12f0 100644--- a/frontend/src/plugins/impl/chat/chat-ui.tsx+++ b/frontend/src/plugins/impl/chat/chat-ui.tsx@@ -4,12 +4,15 @@ import { type UIMessage, useChat } from "@ai-sdk/react";import { ChatBubbleIcon } from "@radix-ui/react-icons";import { PopoverAnchor } from "@radix-ui/react-popover";import type { ReactCodeMirrorRef } from "@uiw/react-codemirror";-import { DefaultChatTransport, type FileUIPart } from "ai";+import {+ createUIMessageStreamResponse,+ DefaultChatTransport,+ type TextUIPart,+ type UIMessageChunk,+} from "ai";import { startCase } from "lodash-es";import {BotMessageSquareIcon,- ClipboardIcon,- DownloadIcon,HelpCircleIcon,PaperclipIcon,RotateCwIcon,@@ -18,14 +21,16 @@ import {Trash2Icon,X,} from "lucide-react";-import React, { lazy, useEffect, useRef, useState } from "react";+import React, { useEffect, useRef, useState } from "react";+import { renderUIMessage } from "@/components/chat/chat-display";import { convertToFileUIPart } from "@/components/chat/chat-utils";import {type AdditionalCompletions,PromptInput,} from "@/components/editor/ai/add-cell-with-ai";+import { CopyClipboardIcon } from "@/components/icons/copy-icon";import { Spinner } from "@/components/icons/spinner";-import { Button, buttonVariants } from "@/components/ui/button";+import { Button } from "@/components/ui/button";import {DropdownMenu,DropdownMenuContent,@@ -41,7 +46,6 @@ import {PopoverTrigger,} from "@/components/ui/popover";import { Tooltip } from "@/components/ui/tooltip";-import { toast } from "@/components/ui/use-toast";import { moveToEndOfEditor } from "@/core/codemirror/utils";import { MarimoIncomingMessageEvent } from "@/core/dom/events";import { useAsyncData } from "@/hooks/useAsyncData";@@ -50,16 +54,11 @@ import {useEventListener,} from "@/hooks/useEventListener";import { cn } from "@/utils/cn";-import { copyToClipboard } from "@/utils/copy";import { Logger } from "@/utils/Logger";import { Objects } from "@/utils/objects";import { ErrorBanner } from "../common/error-banner";import type { PluginFunctions } from "./ChatPlugin";-import type { ChatConfig, ChatMessage } from "./types";--const LazyStreamdown = lazy(() =>- import("streamdown").then((module) => ({ default: module.Streamdown })),-);+import type { ChatConfig } from "./types";interface Props extends PluginFunctions {prompts: string[];@@ -67,8 +66,9 @@ interface Props extends PluginFunctions {showConfigurationControls: boolean;maxHeight: number | undefined;allowAttachments: boolean | string[];- value: ChatMessage[];- setValue: (messages: ChatMessage[]) => void;+ frontendManaged: boolean;+ value: UIMessage[];+ setValue: (messages: UIMessage[]) => void;host: HTMLElement;}@@ -81,22 +81,31 @@ export const Chatbot: React.FC<Props> = (props) => {const codeMirrorInputRef = useRef<ReactCodeMirrorRef>(null);const scrollContainerRef = useRef<HTMLDivElement>(null);+ // Use a ref to avoid stale closure in the fetch callback+ const configRef = useRef<ChatConfig>(config);+ configRef.current = config;+// Track streaming state - maps backend message_id to frontend message indexconst streamingStateRef = useRef<{backendMessageId: string | null;frontendMessageIndex: number | null;}>({ backendMessageId: null, frontendMessageIndex: null });- const { data: initialMessages } = useAsyncData(async () => {- const chatMessages = await props.get_chat_history({});- const messages: UIMessage[] = chatMessages.messages.map((message, idx) => ({- id: idx.toString(),- role: message.role,- parts: message.parts ?? [],- }));- return messages;+ // For frontend-managed streaming, create a controller to enqueue chunks to.+ const frontendStreamControllerRef =+ useRef<ReadableStreamDefaultController<UIMessageChunk> | null>(null);++ const { data: backendMessages } = useAsyncData(async () => {+ const response = await props.get_chat_history({});+ return response.messages;}, []);+ // Use props.value (persisted plugin state) if available,+ // otherwise fall back to backend messages.+ // This ensures messages persist when switching between edit/app views.+ const initialMessages =+ props.value.length > 0 ? props.value : backendMessages;+const {messages,sendMessage,@@ -105,6 +114,7 @@ export const Chatbot: React.FC<Props> = (props) => {stop,error,regenerate,+ clearError,} = useChat({transport: new DefaultChatTransport({fetch: async (@@ -118,16 +128,73 @@ export const Chatbot: React.FC<Props> = (props) => {const body = JSON.parse(init.body as unknown as string) as {messages: UIMessage[];};++ // Catch signals like abort to stop the stream (when stop function is called from useChat)+ const signal = init.signal;++ const chatConfig: ChatConfig = {+ max_tokens: configRef.current.max_tokens,+ temperature: configRef.current.temperature,+ top_p: configRef.current.top_p,+ top_k: configRef.current.top_k,+ frequency_penalty: configRef.current.frequency_penalty,+ presence_penalty: configRef.current.presence_penalty,+ };+try {- const messages = body.messages.map((m) => ({- role: m.role,- content: m.parts- ?.map((p) => ("text" in p ? p.text : ""))- .join("\n"),- parts: m.parts,- }));-- // Create a placeholder message for streaming+ // Content is added for backwards compatibility+ const messages = body.messages.map((m) => {+ return {+ ...m,+ content: m.parts+ ?.map((p) => ("text" in p ? p.text : ""))+ .join("\n"),+ };+ });++ if (props.frontendManaged) {+ const stream = new ReadableStream<UIMessageChunk>({+ start(controller) {+ frontendStreamControllerRef.current = controller;++ const abortHandler = () => {+ try {+ controller.close();+ } catch (error) {+ Logger.debug("Controller may already be closed", { error });+ }+ frontendStreamControllerRef.current = null;+ };+ signal?.addEventListener("abort", abortHandler);++ return () => {+ signal?.removeEventListener("abort", abortHandler);+ };+ },+ cancel() {+ frontendStreamControllerRef.current = null;+ },+ });++ // Start the prompt, chunks will be sent via events+ props+ .send_prompt({+ messages: messages,+ config: chatConfig,+ })+ .catch((error: Error) => {+ frontendStreamControllerRef.current?.error(error);+ frontendStreamControllerRef.current = null;+ });++ return createUIMessageStreamResponse({ stream });+ }++ if (signal?.aborted) {+ return new Response("Aborted", { status: 499 });+ }++ // Create a placeholder message for streaming (backend-managed)const messageId = Date.now().toString();setMessages((prev) => [@@ -139,18 +206,37 @@ export const Chatbot: React.FC<Props> = (props) => {},]);- const response = await props.send_prompt({+ // Create an abort-aware promise for the send_prompt call+ const sendPromptPromise = props.send_prompt({messages: messages,- config: {- max_tokens: config.max_tokens,- temperature: config.temperature,- top_p: config.top_p,- top_k: config.top_k,- frequency_penalty: config.frequency_penalty,- presence_penalty: config.presence_penalty,- },+ config: chatConfig,});+ // Race the send_prompt with an abort signal+ const response = await new Promise<string | null>(+ (resolve, reject) => {+ // Listen for abort+ const abortHandler = () => {+ reject(new DOMException("Aborted", "AbortError"));+ };+ signal?.addEventListener("abort", abortHandler);++ sendPromptPromise+ .then(resolve)+ .catch(reject)+ .finally(() => {+ signal?.removeEventListener("abort", abortHandler);+ });+ },+ );++ if (response === null) {+ Logger.error("Non-frontend-managed response is null", {+ response,+ });+ return new Response("Internal server error", { status: 500 });+ }+// If streaming didn't happen (non-generator response), update the message// Check if streaming state is still set (meaning no chunks were received)if (@@ -179,6 +265,11 @@ export const Chatbot: React.FC<Props> = (props) => {frontendMessageIndex: null,};+ // Handle abort gracefully without showing an error+ if (error.name === "AbortError") {+ return new Response("Aborted", { status: 499 });+ }+// HACK: strip the error message to clean up the responseconst strippedError = error.message.split("failed with exception ")@@ -201,6 +292,12 @@ export const Chatbot: React.FC<Props> = (props) => {backendMessageId: null,frontendMessageIndex: null,};++ // For frontend-managed streaming, we set the value directly from the frontend.+ // Because useChat creates the proper message structure for us.+ if (props.frontendManaged) {+ props.setValue(message.messages);+ }},onError: (error) => {Logger.error("An error occurred:", error);@@ -219,69 +316,96 @@ export const Chatbot: React.FC<Props> = (props) => {(e) => {const message = e.detail.message;if (- typeof message === "object" &&- message !== null &&- "type" in message &&- message.type === "stream_chunk"+ typeof message !== "object" ||+ message === null ||+ !("type" in message) ||+ message.type !== "stream_chunk") {- const chunkMessage = message as {+ return;+ }++ if (props.frontendManaged) {+ // Push to the stream for useChat to process+ const controller = frontendStreamControllerRef.current;+ if (!controller) {+ return;+ }++ const frontendMessage = message as {type: string;message_id: string;- content: string;- is_final: boolean;+ content?: UIMessageChunk;+ is_final?: boolean;};- // Initialize streaming state on first chunk if not already set- if (streamingStateRef.current.backendMessageId === null) {- // Find the last assistant message (which should be the placeholder we created)- setMessages((prev) => {- const updated = [...prev];- // Find the last assistant message- for (let i = updated.length - 1; i >= 0; i--) {- if (updated[i].role === "assistant") {- streamingStateRef.current = {- backendMessageId: chunkMessage.message_id,- frontendMessageIndex: i,- };- break;- }- }- return updated;- });+ if (frontendMessage.content) {+ controller.enqueue(frontendMessage.content);}+ if (frontendMessage.is_final) {+ controller.close();+ frontendStreamControllerRef.current = null;+ }+ return;+ }- // Only process chunks for the current streaming message- const frontendIndex = streamingStateRef.current.frontendMessageIndex;- if (- streamingStateRef.current.backendMessageId ===- chunkMessage.message_id &&- frontendIndex !== null- ) {- setMessages((prev) => {- const updated = [...prev];- const index = frontendIndex;-- // Update the message at the tracked index- if (index < updated.length) {- const messageToUpdate = updated[index];- if (messageToUpdate.role === "assistant") {- updated[index] = {- ...messageToUpdate,- parts: [{ type: "text", text: chunkMessage.content }],- };- }- }+ // Handle regular text streaming chunks+ const chunkMessage = message as {+ type: string;+ message_id: string;+ content: string;+ is_final: boolean;+ };- return updated;- });+ // Initialize streaming state on first chunk if not already set+ if (streamingStateRef.current.backendMessageId === null) {+ // Find the last assistant message (which should be the placeholder we created)+ setMessages((prev) => {+ const updated = [...prev];+ // Find the last assistant message+ for (let i = updated.length - 1; i >= 0; i--) {+ if (updated[i].role === "assistant") {+ streamingStateRef.current = {+ backendMessageId: chunkMessage.message_id,+ frontendMessageIndex: i,+ };+ break;+ }+ }+ return updated;+ });+ }- // Clear streaming state when final chunk arrives- if (chunkMessage.is_final) {- streamingStateRef.current = {- backendMessageId: null,- frontendMessageIndex: null,- };+ // Only process chunks for the current streaming message+ const frontendIndex = streamingStateRef.current.frontendMessageIndex;+ if (+ streamingStateRef.current.backendMessageId ===+ chunkMessage.message_id &&+ frontendIndex !== null+ ) {+ setMessages((prev) => {+ const updated = [...prev];+ const index = frontendIndex;++ // Update the message at the tracked index+ if (index < updated.length) {+ const messageToUpdate = updated[index];+ if (messageToUpdate.role === "assistant") {+ updated[index] = {+ ...messageToUpdate,+ parts: [{ type: "text", text: chunkMessage.content }],+ };+ }}++ return updated;+ });++ // Clear streaming state when final chunk arrives+ if (chunkMessage.is_final) {+ streamingStateRef.current = {+ backendMessageId: null,+ frontendMessageIndex: null,+ };}}},@@ -292,74 +416,15 @@ export const Chatbot: React.FC<Props> = (props) => {const handleDelete = (id: string) => {const index = messages.findIndex((message) => message.id === id);if (index !== -1) {+ const newMessages = messages.filter((message) => message.id !== id);props.delete_chat_message({ index });- setMessages((prev) => prev.filter((message) => message.id !== id));- }- };+ setMessages(newMessages);- const renderAttachment = (attachment: FileUIPart) => {- if (attachment.mediaType?.startsWith("image")) {- return (- <img- src={attachment.url}- alt={attachment.filename || "Attachment"}- className="object-contain rounded-sm"- width={100}- height={100}- />- );+ // Since we manage the state in the frontend, we need to update the value.+ if (props.frontendManaged) {+ props.setValue(newMessages);+ }}-- return (- <a- href={attachment.url}- target="_blank"- rel="noopener noreferrer"- className="text-background hover:underline"- >- {attachment.filename || "Attachment"}- </a>- );- };-- const renderMessage = (message: UIMessage) => {- const textParts = message.parts?.filter((p) => p.type === "text");- const textContent = textParts?.map((p) => p.text).join("\n");- const content =- message.role === "assistant" ? (- <LazyStreamdown className="mo-markdown-renderer">- {textContent}- </LazyStreamdown>- ) : (- textContent- );-- const attachments = message.parts?.filter((p) => p.type === "file");-- return (- <>- {content}- {attachments && attachments.length > 0 && (- <div className="mt-2">- {attachments.map((attachment, index) => (- <div key={index} className="flex items-baseline gap-2 ">- {renderAttachment(attachment)}- <a- className={buttonVariants({- variant: "text",- size: "icon",- })}- href={attachment.url}- download={attachment.filename}- >- <DownloadIcon className="size-3" />- </a>- </div>- ))}- </div>- )}- </>- );};const shouldShowAttachments =@@ -403,6 +468,13 @@ export const Chatbot: React.FC<Props> = (props) => {setInput("");};+ const resetChatbot = () => {+ setMessages([]);+ props.setValue([]);+ props.delete_chat_history({});+ clearError();+ };+return (<divclassName="flex flex-col h-full bg-(--slate-1) rounded-lg shadow border border-(--slate-6) overflow-hidden relative"@@ -413,11 +485,7 @@ export const Chatbot: React.FC<Props> = (props) => {variant="text"size="icon"disabled={messages.length === 0}- onClick={() => {- setMessages([]);- props.setValue([]);- props.delete_chat_history({});- }}+ onClick={resetChatbot}><RotateCwIcon className="h-3 w-3" /></Button>@@ -435,11 +503,12 @@ export const Chatbot: React.FC<Props> = (props) => {</p></div>)}- {messages.map((message) => {+ {messages.map((message, index) => {const textContent = message.parts- ?.filter((p) => p.type === "text")+ ?.filter((p): p is TextUIPart => p.type === "text").map((p) => p.text).join("\n");+ const isLast = index === messages.length - 1;return (<div@@ -456,21 +525,18 @@ export const Chatbot: React.FC<Props> = (props) => {: "bg-(--slate-4) text-(--slate-12)"}`}>- {renderMessage(message)}+ {renderUIMessage({+ message,+ isStreamingReasoning: status === "streaming",+ isLast,+ })}</div><div className="flex justify-end text-xs gap-2 invisible group-hover:visible">- <button- type="button"- onClick={async () => {- await copyToClipboard(textContent);- toast({- title: "Copied to clipboard",- });- }}- className="text-xs text-(--slate-9) hover:text-(--slate-11)"- >- <ClipboardIcon className="h-3 w-3" />- </button>+ <CopyClipboardIcon+ value={textContent}+ className="h-3 w-3"+ buttonClassName="text-xs text-(--slate-9) hover:text-(--slate-11)"+ /><buttontype="button"onClick={() => handleDelete(message.id)}@@ -676,10 +742,19 @@ const ConfigPopup: React.FC<{const [localConfig, setLocalConfig] = useState<ChatConfig>(config);const [open, setOpen] = useState(false);- const handleChange = (key: keyof ChatConfig, value: number) => {- const { min, max } = configDescriptions[key];- const clampedValue = Math.max(min, Math.min(max, value));- const newConfig = { ...localConfig, [key]: clampedValue };+ const handleChange = (key: keyof ChatConfig, value: number | null) => {+ // NaN represents an empty field, treat as null+ const normalizedValue =+ value === null || Number.isNaN(value) ? null : value;+ let finalValue: number | null = normalizedValue;++ if (finalValue !== null) {+ const { min, max } = configDescriptions[key];+ const clampedValue = Math.max(min, Math.min(max, finalValue));+ finalValue = clampedValue;+ }++ const newConfig = { ...localConfig, [key]: finalValue };setLocalConfig(newConfig);onChange(newConfig);};@@ -698,7 +773,7 @@ const ConfigPopup: React.FC<{<Buttonvariant="outline"size="sm"- className="border-none shadow-initial"+ className="border-none shadow-none hover:bg-transparent"><SettingsIcon className="h-3 w-3" /></Button>@@ -732,7 +807,9 @@ const ConfigPopup: React.FC<{</Label><NumberFieldid={key}- value={value}+ aria-label={key}+ value={value ?? Number.NaN}+ placeholder={"null"}minValue={configDescriptions[key].min}maxValue={configDescriptions[key].max}step={configDescriptions[key].step ?? 1}@@ -776,9 +853,9 @@ const PromptsPopover: React.FC<{<Buttonvariant="outline"size="sm"- className="border-none shadow-initial"+ className="border-none shadow-none hover:bg-transparent">- <ChatBubbleIcon className="h-3 w-3 mx-1" />+ <ChatBubbleIcon className="h-3 w-3" /></Button></DropdownMenuTrigger></Tooltip>diff --git a/frontend/src/plugins/impl/chat/types.ts b/frontend/src/plugins/impl/chat/types.tsindex e0b819e70a..eeb0c241d8 100644--- a/frontend/src/plugins/impl/chat/types.ts+++ b/frontend/src/plugins/impl/chat/types.ts@@ -4,21 +4,19 @@ import type { UIMessage } from "ai";export type ChatRole = "system" | "user" | "assistant";-export interface ChatMessage {- role: ChatRole;- content: string; // TODO: Deprecate content- parts: UIMessage["parts"] | null;+export interface ChatMessage extends UIMessage {+ content: string | null; // Content is only added for backwards compatibility}export interface SendMessageRequest {messages: ChatMessage[];config: {- max_tokens?: number;- temperature?: number;- top_p?: number;- top_k?: number;- frequency_penalty?: number;- presence_penalty?: number;+ max_tokens: number | null;+ temperature: number | null;+ top_p: number | null;+ top_k: number | null;+ frequency_penalty: number | null;+ presence_penalty: number | null;};}@@ -27,10 +25,10 @@ export interface SendMessageRequest {* and are not modified when sent to the frontend.*/export interface ChatConfig {- max_tokens: number;- temperature: number;- top_p: number;- top_k: number;- frequency_penalty: number;- presence_penalty: number;+ max_tokens: number | null;+ temperature: number | null;+ top_p: number | null;+ top_k: number | null;+ frequency_penalty: number | null;+ presence_penalty: number | null;}diff --git a/marimo/_ai/_types.py b/marimo/_ai/_types.pyindex 2f6706de17..b05f4a7a88 100644--- a/marimo/_ai/_types.py+++ b/marimo/_ai/_types.py@@ -2,9 +2,8 @@from __future__ import annotationsimport abc-import dataclassesimport mimetypes-from dataclasses import dataclass+from dataclasses import dataclass, is_dataclassfrom typing import (TYPE_CHECKING,Any,@@ -18,6 +17,7 @@ from typing import (import msgspecfrom marimo import _loggers+from marimo._dependencies.dependencies import DependencyManagerfrom marimo._utils.parse_dataclass import parse_rawLOGGER = _loggers.marimo_logger()@@ -67,10 +67,12 @@ ChatPartDict = Union[class ChatMessageDict(TypedDict):+ id: strrole: Literal["user", "assistant", "system"]- content: str+ content: Optional[str]attachments: Optional[list[ChatAttachmentDict]]- parts: Optional[list[ChatPartDict]]+ parts: list[ChatPartDict]+ metadata: Optional[Any]class ChatModelConfigDict(TypedDict, total=False):@@ -196,13 +198,17 @@ class ChatMessage(msgspec.Struct):# The content of the message.content: Any+ # The id of the message.+ id: str = ""++ # Parts from AI SDK+ parts: list[ChatPart] = []+# Optional attachments to the message.# TODO: Deprecate in favour of partsattachments: Optional[list[ChatAttachment]] = None- # Parts from AI SDK. (see types above)- # TODO: Make this required- parts: Optional[list[ChatPart]] = None+ metadata: Any | None = Nonedef __post_init__(self) -> None:# Hack: msgspec only supports discriminated unions. This is a hack to just@@ -215,18 +221,69 @@ class ChatMessage(msgspec.Struct):self.parts = partsdef _convert_part(self, part: Any) -> Optional[ChatPart]:+ # If we receive a Vercel AI SDK part (through pydantic-ai), return it as is.+ if DependencyManager.pydantic_ai.imported():+ from pydantic_ai.ui.vercel_ai.request_types import UIMessagePart++ if isinstance(part, UIMessagePart):+ return cast(ChatPart, part)+PartType = Nonefor PartType in PART_TYPES:try:- if dataclasses.is_dataclass(part):+ if is_dataclass(part):return cast(ChatPart, part)return parse_raw(part, cls=PartType, allow_unknown_keys=True)except Exception:continue- LOGGER.error(f"Could not decode part as {PartType}, for part {part}")+ LOGGER.error(f"Could not decode part {part}")return None+ @classmethod+ def create(+ cls,+ *,+ role: Literal["user", "assistant", "system"],+ message_id: str,+ content: Optional[str],+ parts: list[ChatPart],+ part_validator_class: Any | None = None,+ ) -> ChatMessage:+ """+ Helper method to create a ChatMessage object.+ If part_validator_class is provided, the parts will be converted to the given class.+ """++ if part_validator_class:+ validated_parts = []+ for part in parts:+ if isinstance(part, part_validator_class):+ validated_parts.append(part)+ elif isinstance(part, dict):+ # Try pydantic validation for dict -> class conversion+ try:+ from pydantic import TypeAdapter++ adapter = TypeAdapter(part_validator_class)+ validated_parts.append(adapter.validate_python(part))+ except ImportError:+ LOGGER.debug(+ "Pydantic not installed, skipping dict validation"+ )+ except Exception:+ LOGGER.debug("Part %r could not be validated", part)+ else:+ LOGGER.debug(+ "Part %r (type=%s) is not an instance of %s and not a dict, dropping",+ part,+ type(part).__name__,+ part_validator_class.__name__,+ )+ parts = validated_parts++ return cls(role=role, id=message_id, content=content, parts=parts)+@dataclassclass ChatModelConfig:diff --git a/marimo/_ai/llm/__init__.py b/marimo/_ai/llm/__init__.pyindex f676b161ef..cd674e2276 100644--- a/marimo/_ai/llm/__init__.py+++ b/marimo/_ai/llm/__init__.py@@ -1,5 +1,12 @@# Copyright 2026 Marimo. All rights reserved.-from marimo._ai.llm._impl import anthropic, bedrock, google, groq, openai+from marimo._ai.llm._impl import (+ anthropic,+ bedrock,+ google,+ groq,+ openai,+ pydantic_ai,+)-__all__ = ["openai", "anthropic", "google", "groq", "bedrock"]+__all__ = ["openai", "anthropic", "google", "groq", "bedrock", "pydantic_ai"]diff --git a/marimo/_ai/llm/_impl.py b/marimo/_ai/llm/_impl.pyindex 42948c4ff1..2d3e83229e 100644--- a/marimo/_ai/llm/_impl.py+++ b/marimo/_ai/llm/_impl.py@@ -1,12 +1,21 @@# Copyright 2026 Marimo. All rights reserved.from __future__ import annotations+import dataclassesimport osimport refrom typing import TYPE_CHECKING, Any, Callable, Optional, cast+from marimo import _loggers+from marimo._ai._pydantic_ai_utils import generate_id+if TYPE_CHECKING:- from collections.abc import Generator+ from collections.abc import AsyncGenerator, Generator++ from pydantic_ai import Agent+ from pydantic_ai.settings import ModelSettings+ from pydantic_ai.ui.vercel_ai.request_types import UIMessage+ from pydantic_ai.ui.vercel_ai.response_types import BaseChunkfrom marimo._ai._convert import (convert_to_anthropic_messages,@@ -21,6 +30,8 @@ from marimo._ai._types import ()from marimo._dependencies.dependencies import DependencyManager+LOGGER = _loggers.marimo_logger()+DEFAULT_SYSTEM_MESSAGE = ("You are a helpful assistant specializing in data science.")@@ -664,3 +675,165 @@ class bedrock(ChatModel):else:# Re-raise original exception if not handledraise+++class pydantic_ai(ChatModel):+ """+ Pydantic AI ChatModel++ Args:+ agent: The Pydantic AI agent to use.+ """++ def __init__(self, agent: Agent[Any, Any]):+ DependencyManager.pydantic_ai.require(+ "pydantic-ai chat model requires pydantic-ai. `pip install pydantic-ai`"+ )+ self.agent = agent++ def __call__(+ self, messages: list[ChatMessage], config: ChatModelConfig+ ) -> AsyncGenerator[dict[str, Any], None]:+ return self._stream_response(messages, config)++ def _get_model_settings(self, config: ChatModelConfig) -> ModelSettings:+ model_settings: ModelSettings = {}+ if config.max_tokens is not None:+ model_settings["max_tokens"] = config.max_tokens+ if config.temperature is not None:+ model_settings["temperature"] = config.temperature+ if config.top_p is not None:+ model_settings["top_p"] = config.top_p+ if config.frequency_penalty is not None:+ model_settings["frequency_penalty"] = config.frequency_penalty+ if config.presence_penalty is not None:+ model_settings["presence_penalty"] = config.presence_penalty+ return model_settings++ def _build_ui_messages(+ self, messages: list[ChatMessage]+ ) -> list[UIMessage]:+ from pydantic_ai.ui.vercel_ai.request_types import (+ TextUIPart,+ UIMessage,+ UIMessagePart,+ )++ ui_messages: list[UIMessage] = []++ for message in messages:+ message_id = message.id or generate_id("message")++ if not message.id:+ LOGGER.warning("Message %s has no id", message)++ parts: list[UIMessagePart] = []+ if message.parts:+ parts = cast(+ list[UIMessagePart],+ [+ dataclasses.asdict(part)+ if dataclasses.is_dataclass(part)+ else part+ for part in message.parts+ ],+ )+ if not parts:+ if message.content is not None:+ LOGGER.warning(+ "Message %s has no valid parts, using content instead",+ message,+ )+ parts = [TextUIPart(text=str(message.content))]+ else:+ LOGGER.error(+ "Message %s has no parts and no content, skipping",+ message,+ )+ continue++ ui_messages.append(+ UIMessage(+ id=message_id,+ role=message.role,+ parts=parts,+ metadata=message.metadata,+ )+ )+ return ui_messages++ def _serialize_vercel_ai_chunk(+ self, chunk: BaseChunk+ ) -> dict[str, Any] | None:+ """+ Serialize vercel ai chunk to a dictionary. Skip "done" chunks - not part of Vercel AI SDK schema.++ by_alias=True: Use camelCase keys expected by Vercel AI SDK.+ exclude_none=True: Remove null values which cause validation errors.+ """+ try:+ serialized = chunk.model_dump(+ mode="json", by_alias=True, exclude_none=True+ )+ except Exception as e:+ LOGGER.error("Error serializing vercel ai chunk: %s", e)+ return None+ else:+ if serialized.get("type") == "done":+ return None+ return serialized++ async def _stream_response(+ self, messages: list[ChatMessage], config: ChatModelConfig+ ) -> AsyncGenerator[dict[str, Any], None]:+ """Streams back Vercel AI events.++ Args:+ messages: The messages to send to the model.+ config: The model configuration.++ Returns:+ An asynchronous generator of serialized Vercel AI events.+ """+ from pydantic_ai.ui.vercel_ai import VercelAIAdapter+ from pydantic_ai.ui.vercel_ai.request_types import SubmitMessage++ ui_messages = self._build_ui_messages(messages)+ model_settings = self._get_model_settings(config)+ run_input = SubmitMessage(+ id=generate_id("submit-message"),+ trigger="submit-message",+ messages=ui_messages,+ )++ adapter = VercelAIAdapter(agent=self.agent, run_input=run_input)+ event_stream = adapter.run_stream(model_settings=model_settings)+ async for event in event_stream:+ if serialized := self._serialize_vercel_ai_chunk(event):+ yield serialized++ async def _stream_text(+ self, messages: list[ChatMessage], config: ChatModelConfig+ ) -> AsyncGenerator[str, None]:+ """Streams back text from the model.++ Args:+ messages: The messages to send to the model.+ config: The model configuration.++ Returns:+ An asynchronous generator of text.+ """+ from pydantic_ai.ui.vercel_ai import VercelAIAdapter++ ui_messages = self._build_ui_messages(messages)+ pydantic_model_messages = VercelAIAdapter.load_messages(ui_messages)+ model_settings = self._get_model_settings(config)++ async with self.agent.run_stream(+ user_prompt=None,+ message_history=pydantic_model_messages,+ model_settings=model_settings,+ ) as result:+ async for text in result.stream_text(delta=True):+ yield textdiff --git a/marimo/_dependencies/dependencies.py b/marimo/_dependencies/dependencies.pyindex 7a4de21937..bc205f9253 100644--- a/marimo/_dependencies/dependencies.py+++ b/marimo/_dependencies/dependencies.py@@ -17,7 +17,7 @@ class Dependency:max_version: str | None = Nonedef has(self, quiet: bool = False) -> bool:- """Return True if the dependency is installed."""+ """Return True if the dependency is installed. For a more performant check, use imported()."""try:has_dep = importlib.util.find_spec(self.pkg) is not Noneif not has_dep:diff --git a/marimo/_plugins/ui/_impl/chat/chat.py b/marimo/_plugins/ui/_impl/chat/chat.pyindex 4b5a790b6d..47727d548a 100644--- a/marimo/_plugins/ui/_impl/chat/chat.py+++ b/marimo/_plugins/ui/_impl/chat/chat.py@@ -11,12 +11,14 @@ from marimo._ai._types import (ChatModelConfig,ChatModelConfigDict,)+from marimo._ai.llm._impl import pydantic_aifrom marimo._output.formatting import as_htmlfrom marimo._output.rich_help import mddocfrom marimo._plugins.core.web_component import JSONTypefrom marimo._plugins.ui._core.ui_element import UIElementfrom marimo._plugins.ui._impl.chat.utils import from_chat_message_dictfrom marimo._runtime.commands import UpdateUIElementCommand+from marimo._runtime.context import get_contextfrom marimo._runtime.context.types import ContextNotInitializedErrorfrom marimo._runtime.functions import EmptyArgs, Function@@ -157,6 +159,10 @@ class chat(UIElement[dict[str, Any], list[ChatMessage]]):) -> None:self._model = modelself._chat_history: list[ChatMessage] = []+ self._frontend_managed = False++ if isinstance(model, pydantic_ai):+ self._frontend_managed = Trueif config is None:config = DEFAULT_CONFIG@@ -175,6 +181,7 @@ class chat(UIElement[dict[str, Any], list[ChatMessage]]):"config": cast(JSONType, config or {}),"allow-attachments": allow_attachments,"max-height": max_height,+ "frontend-managed": self._frontend_managed,},functions=(Function(@@ -214,62 +221,102 @@ class chat(UIElement[dict[str, Any], list[ChatMessage]]):del self._chat_history[index]self._value = self._chat_history- async def _handle_streaming_response(self, response: Any) -> str:+ def _send_chat_message(+ self,+ *,+ message_id: str,+ content: str | dict[str, Any] | None,+ is_final: bool,+ ) -> None:+ """Helper method to send a chat message to the frontend."""+ self._send_message(+ {+ "type": "stream_chunk",+ "message_id": message_id,+ "content": content,+ "is_final": is_final,+ },+ buffers=None,+ )++ async def _handle_streaming_response(self, response: Any) -> str | None:"""Handle streaming from both sync and async generators.Generators should yield delta chunks (new content only), which thismethod accumulates and sends to the frontend as complete text.This follows the standard streaming pattern used by OpenAI, Anthropic,- and other AI providers.+ and other AI providers. For frontend-managed streaming, the response is set on the frontend,+ so we don't need to return anything."""message_id = str(uuid.uuid4())accumulated_text = ""+ if self._frontend_managed:+ async for delta in response:+ self._send_chat_message(+ message_id=message_id, content=delta, is_final=False+ )+ # Send final message to indicate streaming is complete+ self._send_chat_message(+ message_id=message_id, content=None, is_final=True+ )+ return None+# Use async for if it's an async generator, otherwise regular forif inspect.isasyncgen(response):async for delta in response:# Accumulate each delta chunkdelta_str = str(delta)accumulated_text += delta_str- self._send_message(- {- "type": "stream_chunk",- "message_id": message_id,- "content": accumulated_text,- "is_final": False,- },- buffers=None,+ self._send_chat_message(+ message_id=message_id,+ content=accumulated_text,+ is_final=False,)else:for delta in response:# Accumulate each delta chunkdelta_str = str(delta)accumulated_text += delta_str- self._send_message(- {- "type": "stream_chunk",- "message_id": message_id,- "content": accumulated_text,- "is_final": False,- },- buffers=None,+ self._send_chat_message(+ message_id=message_id,+ content=accumulated_text,+ is_final=False,)# Send final message to indicate streaming is completeif accumulated_text:- self._send_message(- {- "type": "stream_chunk",- "message_id": message_id,- "content": accumulated_text,- "is_final": True,- },- buffers=None,+ self._send_chat_message(+ message_id=message_id, content=accumulated_text, is_final=True)-return accumulated_text- async def _send_prompt(self, args: SendMessageRequest) -> str:+ def _update_chat_history(self, chat_history: list[ChatMessage]) -> None:+ self._chat_history = chat_history+ # The frontend doesn't manage state, so we have to manually enqueue+ # a control request.+ try:+ ctx = get_context()+ except ContextNotInitializedError:+ # For testing ... this should never happen in real usage.+ self._value = self._chat_history+ if self._on_change is not None:+ self._on_change(self._value)+ else:+ from marimo._runtime.context.kernel_context import (+ KernelRuntimeContext,+ )++ if isinstance(ctx, KernelRuntimeContext):+ ctx._kernel.enqueue_control_request(+ UpdateUIElementCommand(+ object_ids=[self._id],+ values=[{"messages": self._chat_history}],+ request=None,+ )+ )++ async def _send_prompt(self, args: SendMessageRequest) -> str | None:messages = args.messages# If the model is a callable that takes a single argument,@@ -292,33 +339,14 @@ class chat(UIElement[dict[str, Any], list[ChatMessage]]):# representation of the response, and the last value is the full valueresponse = await self._handle_streaming_response(response)- response_message = ChatMessage(role="assistant", content=response)- self._chat_history = messages + [response_message]-- from marimo._runtime.context import get_context+ if self._frontend_managed:+ # For frontend-managed streaming, the response is set on the frontend,+ # so we don't need to return anything.+ return None- # The frontend doesn't manage state, so we have to manually enqueue- # a control request.- try:- ctx = get_context()- except ContextNotInitializedError:- # For testing ... this should never happen in real usage.- self._value = self._chat_history- if self._on_change is not None:- self._on_change(self._value)- else:- from marimo._runtime.context.kernel_context import (- KernelRuntimeContext,- )-- if isinstance(ctx, KernelRuntimeContext):- ctx._kernel.enqueue_control_request(- UpdateUIElementCommand(- object_ids=[self._id],- values=[{"messages": self._chat_history}],- request=None,- )- )+ response_message = ChatMessage(role="assistant", content=response)+ chat_history = messages + [response_message]+ self._update_chat_history(chat_history)# Return the response as HTML# If the response is a string, convert it to markdown@@ -331,4 +359,21 @@ class chat(UIElement[dict[str, Any], list[ChatMessage]]):raise ValueError("Invalid chat history format")messages = value["messages"]++ if self._frontend_managed:+ from pydantic_ai.ui.vercel_ai.request_types import UIMessagePart++ # The frontend sends messages as UIMessage dicts so we use pydantic-ai to cast them+ # as Vercel messages+ return [+ ChatMessage.create(+ role=msg.get("role", "user"),+ message_id=msg.get("id"),+ content=None,+ parts=msg.get("parts", []),+ part_validator_class=UIMessagePart,+ )+ for msg in messages+ ]+return [from_chat_message_dict(msg) for msg in messages]diff --git a/marimo/_plugins/ui/_impl/chat/utils.py b/marimo/_plugins/ui/_impl/chat/utils.pyindex 506ac88fd2..c342c5c8f5 100644--- a/marimo/_plugins/ui/_impl/chat/utils.py+++ b/marimo/_plugins/ui/_impl/chat/utils.py@@ -1,7 +1,7 @@# Copyright 2026 Marimo. All rights reserved.from __future__ import annotations-from typing import Optional, cast+from typing import castfrom marimo._ai._types import (ChatMessage,@@ -25,7 +25,7 @@ def from_chat_message_dict(d: ChatMessageDict) -> ChatMessage:# Handle partsparts_dict = d.get("parts", None)- parts: Optional[list[ChatPart]] = None+ parts: list[ChatPart] = []if parts_dict is not None:parts = []for part_dict in parts_dict:@@ -93,4 +93,10 @@ def from_chat_message_dict(d: ChatMessageDict) -> ChatMessage:))- return ChatMessage(role=d["role"], content=d["content"], parts=parts)+ return ChatMessage(+ id=d.get("id", ""),+ role=d["role"],+ content=d["content"],+ parts=parts,+ metadata=d.get("metadata"),+ )diff --git a/packages/openapi/api.yaml b/packages/openapi/api.yamlindex e1496344ff..f875712c60 100644--- a/packages/openapi/api.yaml+++ b/packages/openapi/api.yaml@@ -512,13 +512,19 @@ components:- type: 'null'default: nullcontent: {}- parts:+ id:+ default: ''+ type: string+ metadata:anyOf:- - items:- type: object- type: array+ - {}- type: 'null'default: null+ parts:+ default: []+ items:+ type: object+ type: arrayrole:enum:- assistantdiff --git a/packages/openapi/src/api.ts b/packages/openapi/src/api.tsindex 826ab6bc6b..4ac68520ca 100644--- a/packages/openapi/src/api.ts+++ b/packages/openapi/src/api.ts@@ -3203,8 +3203,12 @@ export interface components {/** @default null */attachments?: components["schemas"]["ChatAttachment"][] | null;content: unknown;+ /** @default */+ id?: string;/** @default null */- parts?: Record<string, any>[] | null;+ metadata?: unknown | null;+ /** @default [] */+ parts?: Record<string, any>[];/** @enum {unknown} */role: "assistant" | "system" | "user";};diff --git a/tests/_ai/llm/test_impl.py b/tests/_ai/llm/test_impl.pyindex 40cfd23081..4d7ed47314 100644--- a/tests/_ai/llm/test_impl.py+++ b/tests/_ai/llm/test_impl.py@@ -3,6 +3,7 @@from __future__ import annotationsimport os+from typing import TYPE_CHECKING, Anyfrom unittest.mock import MagicMock, patchimport pytest@@ -15,10 +16,14 @@ from marimo._ai.llm._impl import (google,groq,openai,+ pydantic_ai,simple,)from marimo._dependencies.dependencies import DependencyManager+if TYPE_CHECKING:+ from pydantic_ai.settings import ModelSettings+@pytest.fixturedef mock_openai_client():@@ -1101,3 +1106,533 @@ class TestBedrock:assert call_args["top_p"] == pytest.approx(0.9)assert call_args["frequency_penalty"] == pytest.approx(0.5)assert call_args["presence_penalty"] == pytest.approx(0.5)+++ not DependencyManager.pydantic_ai.has(),+ reason="Pydantic AI is not installed",+)+class TestPydanticAI:+ """Tests for the pydantic_ai ChatModel class."""++ def test_init(self):+ """Test initialization of the pydantic_ai class."""+ mock_agent = MagicMock()+ model = pydantic_ai(mock_agent)+ assert model.agent is mock_agent++ def test_get_model_settings_full_config(self):+ """Test _get_model_settings with all config options."""++ mock_agent = MagicMock()+ model = pydantic_ai(mock_agent)++ config = ChatModelConfig(+ max_tokens=100,+ temperature=0.7,+ top_p=0.9,+ frequency_penalty=0.5,+ presence_penalty=0.3,+ )++ settings: ModelSettings = model._get_model_settings(config)++ assert settings.get("max_tokens") == 100+ assert settings.get("temperature") == pytest.approx(0.7)+ assert settings.get("top_p") == pytest.approx(0.9)+ assert settings.get("frequency_penalty") == pytest.approx(0.5)+ assert settings.get("presence_penalty") == pytest.approx(0.3)++ def test_get_model_settings_partial_config(self):+ """Test _get_model_settings with partial config."""+ mock_agent = MagicMock()+ model = pydantic_ai(mock_agent)++ config = ChatModelConfig(+ max_tokens=200,+ temperature=0.5,+ )++ settings: ModelSettings = model._get_model_settings(config)++ assert settings.get("max_tokens") == 200+ assert settings.get("temperature") == pytest.approx(0.5)+ assert settings.get("top_p") is None+ assert settings.get("frequency_penalty") is None+ assert settings.get("presence_penalty") is None++ def test_get_model_settings_empty_config(self):+ """Test _get_model_settings with empty config."""+ mock_agent = MagicMock()+ model = pydantic_ai(mock_agent)++ config = ChatModelConfig()++ settings: ModelSettings = model._get_model_settings(config)++ assert settings == {}++ def test_build_ui_messages_with_parts(self):+ """Test _build_ui_messages with messages that have parts."""+ from pydantic_ai.ui.vercel_ai.request_types import (+ TextUIPart,+ UIMessage,+ )++ mock_agent = MagicMock()+ model = pydantic_ai(mock_agent)++ messages = [+ ChatMessage(+ role="user",+ content="Hello",+ id="msg-1",+ parts=[TextPart(type="text", text="Hello")],+ ),+ ChatMessage(+ role="assistant",+ content="Hi there",+ id="msg-2",+ parts=[TextPart(type="text", text="Hi there")],+ ),+ ]++ ui_messages = model._build_ui_messages(messages)++ assert ui_messages == [+ UIMessage(+ id="msg-1",+ role="user",+ parts=[TextUIPart(type="text", text="Hello")],+ metadata=None,+ ),+ UIMessage(+ id="msg-2",+ role="assistant",+ parts=[TextUIPart(type="text", text="Hi there")],+ metadata=None,+ ),+ ]++ def test_build_ui_messages_without_parts(self):+ """Test _build_ui_messages falls back to content when no parts."""+ from pydantic_ai.ui.vercel_ai.request_types import (+ TextUIPart,+ UIMessage,+ )++ mock_agent = MagicMock()+ model = pydantic_ai(mock_agent)++ messages = [+ ChatMessage(+ role="user",+ content="Hello from content",+ id="msg-1",+ parts=None,+ ),+ ]++ ui_messages = model._build_ui_messages(messages)++ assert ui_messages == [+ UIMessage(+ id="msg-1",+ role="user",+ parts=[TextUIPart(type="text", text="Hello from content")],+ metadata=None,+ ),+ ]++ def test_build_ui_messages_with_empty_parts(self):+ """Test _build_ui_messages falls back to content when parts list is empty."""+ from pydantic_ai.ui.vercel_ai.request_types import (+ TextUIPart,+ UIMessage,+ )++ mock_agent = MagicMock()+ model = pydantic_ai(mock_agent)++ messages = [+ ChatMessage(+ role="user",+ content="Hello from content",+ id="msg-1",+ parts=[],+ ),+ ]++ ui_messages = model._build_ui_messages(messages)++ assert ui_messages == [+ UIMessage(+ id="msg-1",+ role="user",+ parts=[TextUIPart(type="text", text="Hello from content")],+ metadata=None,+ ),+ ]++ def test_build_ui_messages_with_unknown_parts(self) -> None:+ """Test _build_ui_messages handles unknown pydantic parts."""+ from pydantic_ai.ui.vercel_ai.request_types import (+ DynamicToolInputAvailablePart,+ UIMessage,+ )++ mock_agent = MagicMock()+ model = pydantic_ai(mock_agent)++ dynamic_tool_part = DynamicToolInputAvailablePart(+ tool_name="tool-name",+ type="dynamic-tool",+ tool_call_id="tool-call-id",+ state="input-available",+ input={"input": "input"},+ call_provider_metadata={"provider": {"name": "provider"}},+ )++ messages = [+ ChatMessage(+ role="user",+ content="Hello",+ id="msg-1",+ parts=[dynamic_tool_part], # type: ignore+ ),+ ]++ ui_messages = model._build_ui_messages(messages)+ assert ui_messages == [+ UIMessage(+ id="msg-1",+ role="user",+ parts=[dynamic_tool_part],+ metadata=None,+ ),+ ]++ def test_build_ui_messages_with_unknown_dicts(self):+ """Test _build_ui_messages handles unknown dicts which are actually UIMessageParts."""+ from pydantic_ai.ui.vercel_ai.request_types import (+ TextUIPart,+ UIMessage,+ )++ mock_agent = MagicMock()+ model = pydantic_ai(mock_agent)++ messages = [+ ChatMessage(+ role="user",+ content="Hello",+ id="msg-1",+ parts=[{"type": "text", "text": "Hello"}], # type: ignore+ ),+ ]++ ui_messages = model._build_ui_messages(messages)+ assert ui_messages == [+ UIMessage(+ id="msg-1",+ role="user",+ parts=[TextUIPart(type="text", text="Hello")],+ ),+ ]++ def test_build_ui_messages_generates_id_when_missing(self):+ """Test _build_ui_messages generates ID when message has no id."""+ mock_agent = MagicMock()+ model = pydantic_ai(mock_agent)++ messages = [+ ChatMessage(+ role="user",+ content="Hello",+ id=None,+ parts=[TextPart(type="text", text="Hello")],+ ),+ ]++ ui_messages = model._build_ui_messages(messages)++ assert len(ui_messages) == 1+ # ID is generated, so just check it starts with "message"+ assert ui_messages[0].id.startswith("message")+ assert ui_messages[0].role == "user"++ def test_build_ui_messages_preserves_metadata(self):+ """Test _build_ui_messages preserves message metadata."""+ from pydantic_ai.ui.vercel_ai.request_types import (+ TextUIPart,+ UIMessage,+ )++ mock_agent = MagicMock()+ model = pydantic_ai(mock_agent)++ messages = [+ ChatMessage(+ role="user",+ content="Hello",+ id="msg-1",+ parts=[TextPart(type="text", text="Hello")],+ metadata={"custom_key": "custom_value"},+ ),+ ]++ ui_messages = model._build_ui_messages(messages)++ assert ui_messages == [+ UIMessage(+ id="msg-1",+ role="user",+ parts=[TextUIPart(type="text", text="Hello")],+ metadata={"custom_key": "custom_value"},+ ),+ ]++ async def test_stream_response(self):+ """Test _stream_response streams Vercel AI events."""+ from pydantic_ai.ui.vercel_ai.response_types import TextDeltaChunk++ mock_agent = MagicMock()+ model = pydantic_ai(mock_agent)++ messages = [+ ChatMessage(+ role="user",+ content="Hello",+ id="msg-1",+ parts=[TextPart(type="text", text="Hello")],+ ),+ ]+ config = ChatModelConfig(max_tokens=100)++ # Mock the VercelAIAdapter+ with patch("pydantic_ai.ui.vercel_ai.VercelAIAdapter") as mock_adapter:+ mock_instance = MagicMock()+ mock_adapter.return_value = mock_instance++ # Use actual pydantic-ai chunk types that have model_dump+ async def mock_run_stream(**_kwargs: Any):+ yield TextDeltaChunk(id="1", type="text-delta", delta="Hello")+ yield TextDeltaChunk(id="2", type="text-delta", delta=" World")++ mock_instance.run_stream = mock_run_stream++ chunks = []+ async for chunk in model._stream_response(messages, config):+ chunks.append(chunk)++ assert chunks == [+ {"id": "1", "type": "text-delta", "delta": "Hello"},+ {"id": "2", "type": "text-delta", "delta": " World"},+ ]++ async def test_stream_text(self):+ """Test _stream_text streams text from the model."""+ mock_agent = MagicMock()+ model = pydantic_ai(mock_agent)++ messages = [+ ChatMessage(+ role="user",+ content="Hello",+ id="msg-1",+ parts=[TextPart(type="text", text="Hello")],+ ),+ ]+ config = ChatModelConfig(max_tokens=100)++ # Mock VercelAIAdapter.load_messages+ with patch("pydantic_ai.ui.vercel_ai.VercelAIAdapter") as mock_adapter:+ mock_adapter.load_messages.return_value = []++ # Mock the agent's run_stream as an async context manager+ async def mock_stream_text(delta=True): # noqa: ARG001+ yield "Hello"+ yield " World"++ mock_result = MagicMock()+ mock_result.stream_text = mock_stream_text++ async def mock_aenter(self: Any): # noqa: ARG001+ return mock_result++ async def mock_aexit(self: Any, *args: Any):+ pass++ mock_context = MagicMock()+ mock_context.__aenter__ = mock_aenter+ mock_context.__aexit__ = mock_aexit++ mock_agent.run_stream.return_value = mock_context++ chunks = []+ async for chunk in model._stream_text(messages, config):+ chunks.append(chunk)++ assert chunks == ["Hello", " World"]++ def test_call_returns_stream_response(self):+ """Test that __call__ returns _stream_response result."""+ mock_agent = MagicMock()+ model = pydantic_ai(mock_agent)++ messages = [+ ChatMessage(+ role="user",+ content="Hello",+ id="msg-1",+ parts=[TextPart(type="text", text="Hello")],+ ),+ ]+ config = ChatModelConfig()++ result = model(messages, config)+ # Should return an async generator+ import inspect++ assert inspect.isasyncgen(result)++ @patch(+ "marimo._dependencies.dependencies.DependencyManager.pydantic_ai.require"+ )+ def test_dependency_check(self, mock_require):+ """Test that the dependency check is performed on init."""+ mock_agent = MagicMock()+ pydantic_ai(mock_agent)++ mock_require.assert_called_once_with(+ "pydantic-ai chat model requires pydantic-ai. `pip install pydantic-ai`"+ )++ def test_build_ui_messages_with_multiple_roles(self):+ """Test _build_ui_messages with system, user, and assistant roles."""+ from pydantic_ai.ui.vercel_ai.request_types import (+ TextUIPart,+ UIMessage,+ )++ mock_agent = MagicMock()+ model = pydantic_ai(mock_agent)++ messages = [+ ChatMessage(+ role="system",+ content="You are helpful",+ id="msg-1",+ parts=[TextPart(type="text", text="You are helpful")],+ ),+ ChatMessage(+ role="user",+ content="Hello",+ id="msg-2",+ parts=[TextPart(type="text", text="Hello")],+ ),+ ChatMessage(+ role="assistant",+ content="Hi there",+ id="msg-3",+ parts=[TextPart(type="text", text="Hi there")],+ ),+ ]++ ui_messages = model._build_ui_messages(messages)++ assert ui_messages == [+ UIMessage(+ id="msg-1",+ role="system",+ parts=[TextUIPart(type="text", text="You are helpful")],+ metadata=None,+ ),+ UIMessage(+ id="msg-2",+ role="user",+ parts=[TextUIPart(type="text", text="Hello")],+ metadata=None,+ ),+ UIMessage(+ id="msg-3",+ role="assistant",+ parts=[TextUIPart(type="text", text="Hi there")],+ metadata=None,+ ),+ ]++ def test_pydantic_ai_serialize_vercel_ai_chunk(self) -> None:+ """Test _serialize_vercel_ai_chunk with valid chunks."""+ from pydantic_ai.ui.vercel_ai.response_types import (+ TextDeltaChunk,+ ToolInputAvailableChunk,+ )++ mock_agent = MagicMock()+ model = pydantic_ai(mock_agent)++ # Test text-delta chunk+ text_chunk = TextDeltaChunk(id="1", type="text-delta", delta="Hello")+ result = model._serialize_vercel_ai_chunk(text_chunk)+ assert result == {"id": "1", "type": "text-delta", "delta": "Hello"}++ # Test tool-call chunk+ tool_chunk = ToolInputAvailableChunk(+ tool_name="search",+ tool_call_id="call-1",+ input={"query": "test"},+ )+ result = model._serialize_vercel_ai_chunk(tool_chunk)+ assert result == {+ "type": "tool-input-available",+ "toolCallId": "call-1",+ "toolName": "search",+ "input": {"query": "test"},+ }++ def test_pydantic_ai_serialize_vercel_ai_chunk_done_type(self) -> None:+ """Test that 'done' type chunks are skipped."""+ from pydantic_ai.ui.vercel_ai.response_types import DoneChunk++ mock_agent = MagicMock()+ model = pydantic_ai(mock_agent)++ # Test done chunk - should return None+ done_chunk = DoneChunk(type="done")+ result = model._serialize_vercel_ai_chunk(done_chunk)+ assert result is None++ def test_pydantic_ai_serialize_vercel_ai_chunk_error_handling(+ self,+ ) -> None:+ """Test error handling in _serialize_vercel_ai_chunk."""+ from typing import cast++ mock_agent = MagicMock()+ model = pydantic_ai(mock_agent)++ # Test chunk that raises error - should return None+ error_chunk = MockBaseChunkWithError()+ result = model._serialize_vercel_ai_chunk(cast(Any, error_chunk))+ assert result is None+++class MockBaseChunkWithError:+ """Mock BaseChunk that raises on serialization."""++ def model_dump(+ self, mode: str, by_alias: bool, exclude_none: bool+ ) -> dict[str, Any]:+ del mode, by_alias, exclude_none+ raise ValueError("Serialization error")+++ DependencyManager.pydantic_ai.has(), reason="Pydantic AI is installed"+)+def test_pydantic_ai_require() -> None:+ """Test that pydantic_ai.require raises ModuleNotFoundError."""+ mock_agent = MagicMock()+ with pytest.raises(ModuleNotFoundError):+ pydantic_ai(mock_agent)diff --git a/tests/_ai/test_ai_types.py b/tests/_ai/test_ai_types.pynew file mode 100644index 0000000000..940b3b1982--- /dev/null+++ b/tests/_ai/test_ai_types.py@@ -0,0 +1,508 @@+# Copyright 2026 Marimo. All rights reserved.+from __future__ import annotations++from dataclasses import dataclass+from typing import Any, Literal, cast++import pytest++from marimo._ai._types import (+ ChatMessage,+ ChatPart,+ FilePart,+ ReasoningPart,+ TextPart,+ ToolInvocationPart,+)+from marimo._dependencies.dependencies import DependencyManager+++class TestChatMessageCreate:+ """Tests for ChatMessage.create class method."""++ def test_basic_usage_without_validator(self):+ """Test create without part_validator_class."""+ parts: list[Any] = [{"type": "text", "text": "Hello"}]++ message = ChatMessage.create(+ role="user",+ message_id="msg-123",+ content="Hello world",+ parts=parts,+ )++ assert message == ChatMessage(+ role="user",+ id="msg-123",+ content="Hello world",+ parts=[TextPart(type="text", text="Hello")],+ )++ def test_with_empty_parts(self):+ """Test create with empty parts list."""+ message = ChatMessage.create(+ role="assistant",+ message_id="msg-456",+ content="Response",+ parts=[],+ )++ assert message == ChatMessage(+ role="assistant",+ id="msg-456",+ content="Response",+ parts=[],+ )++ def test_with_none_content(self):+ """Test create with None content."""+ parts: list[Any] = [{"type": "text", "text": "Content in parts"}]++ message = ChatMessage.create(+ role="user",+ message_id="msg-789",+ content=None,+ parts=parts,+ )++ assert message == ChatMessage(+ role="user",+ id="msg-789",+ content=None,+ parts=[TextPart(type="text", text="Content in parts")],+ )++ def test_parts_already_correct_type_with_validator(self):+ """Test with parts that are already the correct type."""++ @dataclass+ class MockPart:+ type: Literal["mock"]+ value: str++ existing_part = MockPart(type="mock", value="test")++ message = ChatMessage.create(+ role="user",+ message_id="msg-123",+ content=None,+ parts=[cast(ChatPart, existing_part)],+ part_validator_class=MockPart,+ )++ # The part should be kept as-is since it's already the right type+ assert message == ChatMessage(+ role="user",+ id="msg-123",+ content=None,+ parts=[existing_part], # type: ignore+ )++ def test_dict_parts_with_dataclass_validator(self):+ """Test converting dict parts using a dataclass validator."""+ dict_part: dict[str, str] = {"type": "text", "text": "Hello from dict"}++ message = ChatMessage.create(+ role="user",+ message_id="msg-123",+ content=None,+ parts=[cast(ChatPart, dict_part)],+ part_validator_class=TextPart,+ )++ assert message == ChatMessage(+ role="user",+ id="msg-123",+ content=None,+ parts=[TextPart(type="text", text="Hello from dict")],+ )++ def test_mixed_parts_with_validator(self):+ """Test with a mix of already-typed and dict parts."""++ @dataclass+ class MockPart:+ type: Literal["mock"]+ value: str++ existing_part = MockPart(type="mock", value="existing")+ dict_part: dict[str, str] = {"type": "mock", "value": "from_dict"}++ message = ChatMessage.create(+ role="user",+ message_id="msg-123",+ content=None,+ parts=[cast(ChatPart, existing_part), cast(ChatPart, dict_part)],+ part_validator_class=MockPart,+ )++ assert message == ChatMessage(+ role="user",+ id="msg-123",+ content=None,+ parts=[existing_part, MockPart(type="mock", value="from_dict")], # type: ignore+ )++ def test_invalid_dict_part_is_skipped(self):+ """Test that invalid dict parts are skipped during validation."""++ @dataclass+ class StrictPart:+ type: Literal["strict"]+ required_field: str++ # This dict is missing required_field+ invalid_dict: dict[str, str] = {"type": "strict"}+ valid_part = StrictPart(type="strict", required_field="valid")++ message = ChatMessage.create(+ role="user",+ message_id="msg-123",+ content=None,+ parts=[cast(ChatPart, valid_part), cast(ChatPart, invalid_dict)],+ part_validator_class=StrictPart,+ )++ # Invalid dict should be skipped, only valid part remains+ assert message.parts is not None+ assert len(message.parts) == 1+ assert message.parts[0] is valid_part++ def test_non_dict_non_validator_part_is_dropped_with_logging(self):+ """Test that parts that are neither validator instances nor dicts are dropped."""++ @dataclass+ class ExpectedPart:+ type: Literal["expected"]+ value: str++ @dataclass+ class UnexpectedPart:+ type: Literal["unexpected"]+ data: int++ valid_part = ExpectedPart(type="expected", value="valid")+ unexpected_part = UnexpectedPart(type="unexpected", data=42)++ message = ChatMessage.create(+ role="user",+ message_id="msg-123",+ content=None,+ parts=[+ cast(ChatPart, valid_part),+ cast(ChatPart, unexpected_part),+ ],+ part_validator_class=ExpectedPart,+ )++ # Unexpected part (different dataclass) should be dropped, only valid part remains+ assert message.parts is not None+ assert len(message.parts) == 1+ assert message.parts[0] is valid_part++ def test_all_roles(self):+ """Test create with all valid roles."""+ for role in ["user", "assistant", "system"]:+ message = ChatMessage.create(+ role=role, # type: ignore+ message_id=f"msg-{role}",+ content=f"{role} message",+ parts=[],+ )+ assert message == ChatMessage(+ role=role, # type: ignore+ id=f"msg-{role}",+ content=f"{role} message",+ parts=[],+ )+++class TestChatMessageFromRequestWithPydanticAI:+ """Tests for create with pydantic-ai types."""++ @pytest.mark.skipif(+ not DependencyManager.pydantic_ai.has(),+ reason="pydantic-ai is not installed",+ )+ def test_with_ui_message_part_validator(self):+ """Test create with UIMessagePart from pydantic-ai."""+ from pydantic_ai.ui.vercel_ai.request_types import (+ TextUIPart,+ UIMessagePart,+ )++ dict_part: dict[str, str] = {+ "type": "text",+ "text": "Hello pydantic-ai",+ }++ message = ChatMessage.create(+ role="user",+ message_id="msg-pydantic",+ content=None,+ parts=[cast(ChatPart, dict_part)],+ part_validator_class=UIMessagePart,+ )++ assert message == ChatMessage(+ role="user",+ id="msg-pydantic",+ content=None,+ parts=cast(+ Any, [TextUIPart(type="text", text="Hello pydantic-ai")]+ ),+ )++ @pytest.mark.skipif(+ not DependencyManager.pydantic_ai.has(),+ reason="pydantic-ai is not installed",+ )+ def test_with_existing_ui_message_part(self):+ """Test that existing UIMessagePart instances are kept as-is."""+ from pydantic_ai.ui.vercel_ai.request_types import (+ TextUIPart,+ UIMessagePart,+ )++ existing_part = TextUIPart(type="text", text="Already typed")++ message = ChatMessage.create(+ role="assistant",+ message_id="msg-existing",+ content=None,+ parts=[cast(ChatPart, existing_part)],+ part_validator_class=UIMessagePart,+ )++ # Check identity - the same object should be kept+ assert message.parts is not None+ assert len(message.parts) == 1+ assert message.parts[0] is existing_part++ @pytest.mark.skipif(+ not DependencyManager.pydantic_ai.has(),+ reason="pydantic-ai is not installed",+ )+ def test_with_multiple_ui_part_types(self):+ """Test create with multiple UI part types."""+ from pydantic_ai.ui.vercel_ai.request_types import (+ TextUIPart,+ UIMessagePart,+ )++ parts: list[dict[str, str]] = [+ {"type": "text", "text": "First part"},+ {"type": "text", "text": "Second part"},+ ]++ message = ChatMessage.create(+ role="user",+ message_id="msg-multi",+ content=None,+ parts=cast(Any, parts),+ part_validator_class=UIMessagePart,+ )++ assert message == ChatMessage(+ role="user",+ id="msg-multi",+ content=None,+ parts=cast(+ Any,+ [+ TextUIPart(type="text", text="First part"),+ TextUIPart(type="text", text="Second part"),+ ],+ ),+ )++ @pytest.mark.skipif(+ not DependencyManager.pydantic_ai.has(),+ reason="pydantic-ai is not installed",+ )+ def test_with_reasoning_ui_part(self):+ """Test create with reasoning UI part."""+ from pydantic_ai.ui.vercel_ai.request_types import (+ ReasoningUIPart,+ UIMessagePart,+ )++ parts: list[dict[str, str]] = [+ {"type": "reasoning", "text": "Let me think about this..."},+ ]++ message = ChatMessage.create(+ role="assistant",+ message_id="msg-reasoning",+ content=None,+ parts=cast(Any, parts),+ part_validator_class=UIMessagePart,+ )++ assert message == ChatMessage(+ role="assistant",+ id="msg-reasoning",+ content=None,+ parts=cast(+ Any,+ [+ ReasoningUIPart(+ type="reasoning", text="Let me think about this..."+ )+ ],+ ),+ )+++class TestChatMessagePostInit:+ """Tests for ChatMessage.__post_init__ part conversion."""++ def test_converts_text_part_dict(self):+ """Test that text part dicts are converted to TextPart."""+ message = ChatMessage(+ role="user",+ content="Hello",+ parts=[cast(ChatPart, {"type": "text", "text": "Part text"})],+ )++ assert message == ChatMessage(+ role="user",+ content="Hello",+ parts=[TextPart(type="text", text="Part text")],+ )++ def test_converts_reasoning_part_dict(self):+ """Test that reasoning part dicts are converted to ReasoningPart."""+ message = ChatMessage(+ role="assistant",+ content=None,+ parts=[+ cast(ChatPart, {"type": "reasoning", "text": "Thinking..."})+ ],+ )++ assert message == ChatMessage(+ role="assistant",+ content=None,+ parts=[ReasoningPart(type="reasoning", text="Thinking...")],+ )++ def test_converts_file_part_dict(self):+ """Test that file part dicts are converted to FilePart."""+ message = ChatMessage(+ role="user",+ content=None,+ parts=[+ cast(+ ChatPart,+ {+ "type": "file",+ "media_type": "image/png",+ "url": "data:image/png;base64,abc123",+ },+ ),+ ],+ )++ assert message == ChatMessage(+ role="user",+ content=None,+ parts=[+ FilePart(+ type="file",+ media_type="image/png",+ url="data:image/png;base64,abc123",+ )+ ],+ )++ def test_converts_tool_invocation_part_dict(self):+ """Test that tool invocation part dicts are converted."""+ message = ChatMessage(+ role="assistant",+ content=None,+ parts=cast(+ Any,+ [+ {+ "type": "tool-call",+ "tool_call_id": "call-123",+ "state": "output-available",+ "input": {"query": "test"},+ "output": {"result": "success"},+ }+ ],+ ),+ )++ assert message == ChatMessage(+ role="assistant",+ content=None,+ parts=[+ ToolInvocationPart(+ type="tool-call",+ tool_call_id="call-123",+ state="output-available",+ input={"query": "test"},+ output={"result": "success"},+ )+ ],+ )++ def test_keeps_already_typed_parts(self):+ """Test that already-typed parts are kept as-is."""+ text_part = TextPart(type="text", text="Already typed")+ message = ChatMessage(+ role="user",+ content="Hello",+ parts=[text_part],+ )++ # Check identity - the same object should be kept+ assert message.parts is not None+ assert len(message.parts) == 1+ assert message.parts[0] is text_part++ def test_handles_invalid_parts_gracefully(self):+ """Test that invalid parts are dropped gracefully."""+ message = ChatMessage(+ role="user",+ content="Hello",+ parts=cast(+ Any,+ [+ {"type": "text", "text": "Valid"},+ {"type": "unknown_type", "data": "invalid"},+ ],+ ),+ )++ # Valid part should be kept, invalid should be dropped+ assert message == ChatMessage(+ role="user",+ content="Hello",+ parts=[TextPart(type="text", text="Valid")],+ )++ def test_with_none_parts(self):+ """Test that None parts is handled."""+ message = ChatMessage(role="user", content="Hello", parts=[])+ assert message == ChatMessage(role="user", content="Hello", parts=[])++ def test_with_empty_parts(self):+ """Test that empty parts list stays empty."""+ message = ChatMessage(role="user", content="Hello", parts=[])++ assert message == ChatMessage(role="user", content="Hello", parts=[])++ def test_id_defaults_to_empty_string(self):+ """Test that id defaults to empty string when not provided."""+ message = ChatMessage(role="user", content="Hello")++ assert message.id == ""++ def test_id_can_be_set_explicitly(self):+ """Test that id can be set explicitly."""+ message = ChatMessage(role="user", content="Hello", id="custom-id")++ assert message.id == "custom-id"diff --git a/tests/_ai/test_chat_convert.py b/tests/_ai/test_chat_convert.pyindex 1b02ce52ea..b0a2c56be1 100644--- a/tests/_ai/test_chat_convert.py+++ b/tests/_ai/test_chat_convert.py@@ -670,7 +670,7 @@ class TestAnthropic:ChatMessage(role="user",content="Hello",- parts=None,+ parts=[],),ChatMessage(role="assistant",@@ -700,12 +700,12 @@ class TestAnthropic:ChatMessage(role="user",content="",- parts=None,+ parts=[],),ChatMessage(role="assistant",content="Hi there!",- parts=None,+ parts=[],),]@@ -938,6 +938,7 @@ def test_from_chat_message_dict():result = from_chat_message_dict(message_dict)assert result == ChatMessage(+ id="",role="user",content="Hello, this is a test message.",attachments=None,diff --git a/tests/_plugins/ui/_impl/chat/test_chat.py b/tests/_plugins/ui/_impl/chat/test_chat.pyindex 0b5165d33a..510d57abdb 100644--- a/tests/_plugins/ui/_impl/chat/test_chat.py+++ b/tests/_plugins/ui/_impl/chat/test_chat.py@@ -11,6 +11,7 @@ from marimo._ai._types import (ChatModelConfig,ChatModelConfigDict,)+from marimo._dependencies.dependencies import DependencyManagerfrom marimo._plugins import uifrom marimo._plugins.ui._impl.chat.chat import (DEFAULT_CONFIG,@@ -80,7 +81,7 @@ async def test_chat_send_prompt():messages=[ChatMessage(role="user", content="Hello")],config=ChatModelConfig(),)- response: str = await chat._send_prompt(request)+ response = await chat._send_prompt(request)assert response == "Response to: Hello"assert len(chat._chat_history) == 2@@ -103,7 +104,7 @@ async def test_chat_send_prompt_async_function():messages=[ChatMessage(role="user", content="Hello")],config=ChatModelConfig(),)- response: str = await chat._send_prompt(request)+ response = await chat._send_prompt(request)assert response == "Response to: Hello"assert len(chat._chat_history) == 2@@ -129,7 +130,7 @@ async def test_chat_send_prompt_async_generator():messages=[ChatMessage(role="user", content="Hello")],config=ChatModelConfig(),)- response: str = await chat._send_prompt(request)+ response = await chat._send_prompt(request)# All deltas are accumulated: "0" + "1" + "2" = "012"assert response == "012"@@ -168,7 +169,7 @@ async def test_chat_streaming_sends_messages():config=ChatModelConfig(),)- response: str = await chat._send_prompt(request)+ response = await chat._send_prompt(request)# Verify final response (deltas accumulated)assert response == "Hello world !"@@ -213,7 +214,7 @@ async def test_chat_sync_generator_streaming():config=ChatModelConfig(),)- response: str = await chat._send_prompt(request)+ response = await chat._send_prompt(request)# Verify final response (deltas accumulated)assert response == "Hello world !"@@ -254,7 +255,7 @@ async def test_chat_streaming_complete_response():config=ChatModelConfig(),)- response: str = await chat._send_prompt(request)+ response = await chat._send_prompt(request)# Verify we got the complete final response (all deltas accumulated)assert response == "Hello world!"@@ -449,3 +450,263 @@ async def test_chat_send_message_enqueues_ui_element_request(await chatbot._send_prompt(request)assert len(control_requests) == 1assert isinstance(control_requests[0], UpdateUIElementCommand)+++def test_send_chat_message_helper():+ """Test the _send_chat_message helper method."""++ def mock_model(+ messages: list[ChatMessage], config: ChatModelConfig+ ) -> str:+ del messages, config+ return "Mock response"++ chat = ui.chat(mock_model)++ sent_messages: list[dict] = []++ def capture_send_message(message: dict, buffers): # noqa: ARG001+ sent_messages.append(message)++ chat._send_message = capture_send_message++ # Send a non-final message+ chat._send_chat_message(+ message_id="msg-123", content="Hello", is_final=False+ )++ assert sent_messages == [+ {+ "type": "stream_chunk",+ "message_id": "msg-123",+ "content": "Hello",+ "is_final": False,+ }+ ]++ # Send a final message+ chat._send_chat_message(+ message_id="msg-123", content="Hello world", is_final=True+ )+ assert sent_messages == [+ {+ "type": "stream_chunk",+ "message_id": "msg-123",+ "content": "Hello",+ "is_final": False,+ },+ {+ "type": "stream_chunk",+ "message_id": "msg-123",+ "content": "Hello world",+ "is_final": True,+ },+ ]+++def test_send_chat_message_with_dict_content():+ """Test _send_chat_message with dict content (pydantic-ai mode)."""++ def mock_model(+ messages: list[ChatMessage], config: ChatModelConfig+ ) -> str:+ del messages, config+ return "Mock response"++ chat = ui.chat(mock_model)++ sent_messages: list[dict] = []++ def capture_send_message(message: dict, buffers): # noqa: ARG001+ sent_messages.append(message)++ chat._send_message = capture_send_message++ # Send dict content (like pydantic-ai serialized chunks)+ chunk_content = {"type": "text-delta", "textDelta": "Hello"}+ chat._send_chat_message(+ message_id="msg-456", content=chunk_content, is_final=False+ )++ assert len(sent_messages) == 1+ assert sent_messages[0]["content"] == chunk_content+++def test_send_chat_message_with_none_content():+ """Test _send_chat_message with None content (final message for pydantic-ai)."""++ def mock_model(+ messages: list[ChatMessage], config: ChatModelConfig+ ) -> str:+ del messages, config+ return "Mock response"++ chat = ui.chat(mock_model)++ sent_messages: list[dict] = []++ def capture_send_message(message: dict, buffers): # noqa: ARG001+ sent_messages.append(message)++ chat._send_message = capture_send_message++ # Send None content (final message indicator for pydantic-ai)+ chat._send_chat_message(message_id="msg-789", content=None, is_final=True)+ assert sent_messages == [+ {+ "type": "stream_chunk",+ "message_id": "msg-789",+ "content": None,+ "is_final": True,+ }+ ]+++def test_convert_value_with_list_input():+ """Test _convert_value raises on invalid list input."""+ from typing import Any, cast++ def mock_model(+ messages: list[ChatMessage], config: ChatModelConfig+ ) -> str:+ del messages, config+ return "Mock response"++ chat = ui.chat(mock_model)++ # Test that passing a list directly raises ValueError+ with pytest.raises(ValueError, match="Invalid chat history format"):+ chat._convert_value(cast(Any, [{"role": "user", "content": "Hello"}]))+++ not DependencyManager.pydantic_ai.has(),+ reason="Pydantic AI is not installed",+)+def test_convert_value_frontend_managed():+ """Test _convert_value in frontend-managed mode."""+ from pydantic_ai.ui.vercel_ai.request_types import TextUIPart++ def mock_model(+ messages: list[ChatMessage], config: ChatModelConfig+ ) -> str:+ del messages, config+ return "Mock response"++ chat = ui.chat(mock_model)+ # Force frontend-managed mode+ chat._frontend_managed = True++ value = {+ "messages": [+ {+ "id": "msg-1",+ "role": "user",+ "parts": [{"type": "text", "text": "Hello"}],+ },+ {+ "id": "msg-2",+ "role": "assistant",+ "parts": [{"type": "text", "text": "Hi there!"}],+ },+ ]+ }++ converted = chat._convert_value(value)+ assert converted == [+ ChatMessage(+ role="user",+ id="msg-1",+ parts=[TextUIPart(type="text", text="Hello")], # type: ignore+ content=None,+ ),+ ChatMessage(+ role="assistant",+ id="msg-2",+ parts=[TextUIPart(type="text", text="Hi there!")], # type: ignore+ content=None,+ ),+ ]+++ not DependencyManager.pydantic_ai.has(),+ reason="We use Pydantic to check for vercel parts",+)+def test_convert_value_frontend_managed_missing_fields():+ """Test _convert_value in frontend-managed mode with missing fields."""++ def mock_model(+ messages: list[ChatMessage], config: ChatModelConfig+ ) -> str:+ del messages, config+ return "Mock response"++ chat = ui.chat(mock_model)+ chat._frontend_managed = True++ # Message without parts+ value = {"messages": [{"role": "user", "id": ""}]}++ converted = chat._convert_value(value)+ assert converted == [+ ChatMessage(id="", role="user", parts=[], content=None)+ ]+++async def test_frontend_managed_streaming_sends_serialized_chunks():+ """Test that frontend-managed streaming sends already-serialized chunks correctly."""++ # Chunks are already serialized dicts (from pydantic_ai._serialize_vercel_ai_chunk)+ chunk1 = {"type": "text-delta", "textDelta": "Hello"}+ chunk2 = {"type": "text-delta", "textDelta": " world"}+ # "done" chunks are filtered out by pydantic_ai._serialize_vercel_ai_chunk++ async def mock_pydantic_ai_generator():+ """Mock async generator yielding pre-serialized dict chunks."""+ yield chunk1+ yield chunk2++ def mock_model(+ messages: list[ChatMessage], config: ChatModelConfig+ ) -> str:+ del messages, config+ return "Mock response"++ chat = ui.chat(mock_model)+ chat._frontend_managed = True++ sent_messages: list[dict] = []++ def capture_send_message(message: dict, buffers): # noqa: ARG001+ sent_messages.append(message)++ chat._send_message = capture_send_message++ result = await chat._handle_streaming_response(+ mock_pydantic_ai_generator()+ )++ # Result should be None for frontend-managed mode+ assert result is None++ assert sent_messages == [+ {+ "type": "stream_chunk",+ "message_id": sent_messages[0]["message_id"],+ "content": chunk1,+ "is_final": False,+ },+ {+ "type": "stream_chunk",+ "content": chunk2,+ "is_final": False,+ "message_id": sent_messages[1]["message_id"],+ },+ {+ "type": "stream_chunk",+ "message_id": sent_messages[2]["message_id"],+ "content": None,+ "is_final": True,+ },+ ]diff --git a/tests/_plugins/test_chat_delta_streaming.py b/tests/_plugins/ui/_impl/chat/test_chat_delta_streaming.pysimilarity index 100%rename from tests/_plugins/test_chat_delta_streaming.pyrename to tests/_plugins/ui/_impl/chat/test_chat_delta_streaming.pydiff --git a/tests/snapshots/api.txt b/tests/snapshots/api.txtindex 7a4e307ca5..87a01015ae 100644--- a/tests/snapshots/api.txt+++ b/tests/snapshots/api.txt@@ -16,6 +16,7 @@ aigroqopenai+ pydantic_aiapp_metaas_htmlaudio