Skip to content

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

fix(ai): send stable conversation headers to OpenCode Go (#10895)

Shahmir Varqha17dd04637b5f
diff --git a/frontend/src/components/editor/ai/add-cell-with-ai.tsx b/frontend/src/components/editor/ai/add-cell-with-ai.tsx
index f7d3c731ef..acd1541e65 100644
--- a/frontend/src/components/editor/ai/add-cell-with-ai.tsx
+++ b/frontend/src/components/editor/ai/add-cell-with-ai.tsx
@@ -124,6 +124,7 @@ export const AddCellWithAI: React.FC<{
);
const body = {
...completionBody,
+ id: options.id,
code: "",
prompt: "", // Don't need prompt since we are using messages
language: language,
diff --git a/frontend/src/components/editor/ai/ai-completion-editor.tsx b/frontend/src/components/editor/ai/ai-completion-editor.tsx
index 0b2fa81081..313a8f6453 100644
--- a/frontend/src/components/editor/ai/ai-completion-editor.tsx
+++ b/frontend/src/components/editor/ai/ai-completion-editor.tsx
@@ -124,7 +124,9 @@ export const AiCompletionEditor: React.FC<Props> = ({
new DefaultChatTransport<CompletionUIMessage>({
api: runtimeManager.getAiURL("completion").toString(),
headers: () => runtimeManager.headers(),
- prepareSendMessagesRequest: ({ body }) => ({ body: body ?? {} }),
+ prepareSendMessagesRequest: ({ body, id }) => ({
+ body: { ...body, id },
+ }),
}),
[runtimeManager],
);
diff --git a/marimo/_server/ai/providers.py b/marimo/_server/ai/providers.py
index 06e06f4fdc..01bd0924c5 100644
--- a/marimo/_server/ai/providers.py
+++ b/marimo/_server/ai/providers.py
@@ -2,10 +2,11 @@
from __future__ import annotations
import functools
+import hashlib
import inspect
import os
from abc import ABC, abstractmethod
-from dataclasses import dataclass
+from dataclasses import dataclass, replace
from typing import (
TYPE_CHECKING,
Generic,
@@ -45,6 +46,7 @@ from marimo._server.models.completion import UIMessage as ServerUIMessage
from marimo._utils.assert_never import log_never
from marimo._utils.http import HTTPStatus
from marimo._utils.typing import override
+from marimo._version import __version__
if TYPE_CHECKING:
from collections.abc import AsyncIterator, Sequence
@@ -1225,10 +1227,26 @@ class BedrockProvider(PydanticProvider["PydanticBedrock"]):
def get_completion_provider(
- config: AnyProviderConfig, model: str
+ config: AnyProviderConfig, model: str, *, session_id: str | None = None
) -> PydanticProvider[Provider]:
model_id = AiModelId.from_model(model)
+ if model_id.provider == "opencode-go":
+ headers = {
+ "User-Agent": f"marimo/{__version__}",
+ "x-opencode-client": "marimo",
+ }
+ if session_id:
+ # Keep client-supplied IDs bounded and safe for HTTP headers.
+ headers["x-opencode-session"] = hashlib.sha256(
+ session_id.encode("utf-8")
+ ).hexdigest()
+ # Preserve default casing: the SDK merges its own headers by key.
+ header_names = {name.lower(): name for name in headers}
+ for name, value in (config.extra_headers or {}).items():
+ headers[header_names.get(name.lower(), name)] = value
+ config = replace(config, extra_headers=headers)
+
if model_id.provider == "anthropic":
return AnthropicProvider(
model_id.model, config, [DependencyManager.anthropic]
diff --git a/marimo/_server/api/endpoints/ai.py b/marimo/_server/api/endpoints/ai.py
index 0ec3da2ba7..7d4c96c60d 100644
--- a/marimo/_server/api/endpoints/ai.py
+++ b/marimo/_server/api/endpoints/ai.py
@@ -165,6 +165,7 @@ async def ai_completion(
provider = get_completion_provider(
get_provider_config(model, config),
model=model,
+ session_id=body.id or f"{session_id}:completion",
)
# These models require the optional Pydantic AI dependency checked above.
@@ -247,6 +248,7 @@ async def ai_chat(
provider = get_completion_provider(
get_provider_config(model, config),
model=model,
+ session_id=body.id or f"{session_id}:chat",
)
additional_tools = body.tools or []
@@ -333,7 +335,11 @@ async def ai_inline_completion(
if provider_config.tools:
provider_config.tools.clear()
- provider = get_completion_provider(provider_config, model=model)
+ provider = get_completion_provider(
+ provider_config,
+ model=model,
+ session_id=f"{session_id}:inline_completion",
+ )
try:
content = await provider.completion(
messages=[create_simple_prompt(prompt)],
diff --git a/marimo/_server/models/completion.py b/marimo/_server/models/completion.py
index 801afc1d63..1824fd010a 100644
--- a/marimo/_server/models/completion.py
+++ b/marimo/_server/models/completion.py
@@ -48,6 +48,7 @@ class AiCompletionRequest(msgspec.Struct, rename="camel"):
prompt: str
include_other_code: str
code: str
+ id: str | None = None
ui_messages: list[UIMessage] = []
selected_text: str | None = None
context: AiCompletionContext | None = None
@@ -76,6 +77,7 @@ class ChatRequest(msgspec.Struct, rename="camel"):
include_other_code: str
ui_messages: list[UIMessage]
+ id: str | None = None
tools: list[ToolDefinition] | None = None
model: str | None = None
variables: list[VariableContext | str] | None = None
diff --git a/packages/openapi/api.yaml b/packages/openapi/api.yaml
index 1e040cb4fc..56f6da6474 100644
--- a/packages/openapi/api.yaml
+++ b/packages/openapi/api.yaml
@@ -90,6 +90,11 @@ components:
- type: 'null'
- $ref: '#/components/schemas/AiCompletionContext'
default: null
+ id:
+ anyOf:
+ - type: string
+ - type: 'null'
+ default: null
includeOtherCode:
type: string
language:
@@ -664,6 +669,11 @@ components:
See pydantic_ai.ui.vercel_ai.request_types.UIMessage or Vercel AI SDK documentation.'
properties:
+ id:
+ anyOf:
+ - type: string
+ - type: 'null'
+ default: null
includeOtherCode:
type: string
model:
diff --git a/packages/openapi/src/api.ts b/packages/openapi/src/api.ts
index 13c456f348..9ba4cdd7d2 100644
--- a/packages/openapi/src/api.ts
+++ b/packages/openapi/src/api.ts
@@ -3863,6 +3863,8 @@ export interface components {
code: string;
/** @default null */
context?: null | components["schemas"]["AiCompletionContext"];
+ /** @default null */
+ id?: string | null;
includeOtherCode: string;
/**
* @default python
@@ -4262,6 +4264,8 @@ export interface components {
* See pydantic_ai.ui.vercel_ai.request_types.UIMessage or Vercel AI SDK documentation.
*/
ChatRequest: {
+ /** @default null */
+ id?: string | null;
includeOtherCode: string;
/** @default null */
model?: string | null;
diff --git a/tests/_server/ai/test_providers.py b/tests/_server/ai/test_providers.py
index 8fafbd1379..2988171c52 100644
--- a/tests/_server/ai/test_providers.py
+++ b/tests/_server/ai/test_providers.py
@@ -1,6 +1,7 @@
"""Tests for the LLM providers in marimo._server.ai.providers."""
import asyncio
+import hashlib
import os
from collections.abc import AsyncIterator
from pathlib import Path
@@ -1466,3 +1467,108 @@ def test_get_openai_client_custom_certs(
mock_http.assert_called_once_with(verify=fake_ctx)
assert mock_openai.call_args.kwargs["http_client"] is fake_client
+
+
[email protected]("openai", "pydantic_ai")
[email protected]("override_headers", [False, True])
+async def test_opencode_go_conversation_headers(
+ override_headers: bool,
+) -> None:
+ import httpx
+ from pydantic_ai.providers.openai import OpenAIProvider as PydanticOpenAI
+
+ from marimo._version import __version__
+
+ session_ids = (
+ "chat-1",
+ "chat-1",
+ "chat-2",
+ "bad\r\nInjected: value",
+ "a" * 20_000,
+ "conversation-你好",
+ )
+ headers: list[dict[str, list[str]]] = []
+ extra_headers = {"x-custom": "preserved"}
+ if override_headers:
+ extra_headers.update(
+ {
+ "user-agent": "custom-agent",
+ "X-OpenCode-Client": "custom-client",
+ "X-OpenCode-Session": "custom-session",
+ }
+ )
+
+ def respond(request: httpx.Request) -> httpx.Response:
+ headers.append(
+ {
+ name: request.headers.get_list(name)
+ for name in (
+ "user-agent",
+ "x-opencode-client",
+ "x-opencode-session",
+ "x-custom",
+ )
+ }
+ )
+ return httpx.Response(
+ 200,
+ json={
+ "id": "response",
+ "object": "chat.completion",
+ "created": 0,
+ "model": "deepseek-v4-flash",
+ "choices": [],
+ },
+ )
+
+ config = AnyProviderConfig(
+ api_key="test-key",
+ base_url="https://opencode.ai/zen/go/v1/",
+ extra_headers=extra_headers.copy(),
+ )
+ async with httpx.AsyncClient(
+ transport=httpx.MockTransport(respond)
+ ) as client:
+ for session_id in session_ids:
+ provider = get_completion_provider(
+ config, "opencode-go/deepseek-v4-flash", session_id=session_id
+ )
+ assert isinstance(provider.provider, PydanticOpenAI)
+ openai_client = provider.provider.client
+ try:
+ await openai_client.with_options(
+ http_client=client
+ ).chat.completions.create(
+ model="deepseek-v4-flash",
+ messages=[{"role": "user", "content": "Hello"}],
+ )
+ finally:
+ await openai_client.close()
+
+ assert headers == [
+ {
+ "user-agent": [
+ "custom-agent" if override_headers else f"marimo/{__version__}"
+ ],
+ "x-opencode-client": [
+ "custom-client" if override_headers else "marimo"
+ ],
+ "x-opencode-session": [
+ "custom-session"
+ if override_headers
+ else hashlib.sha256(session_id.encode("utf-8")).hexdigest()
+ ],
+ "x-custom": ["preserved"],
+ }
+ for session_id in session_ids
+ ]
+ assert config.extra_headers == extra_headers
+
+
[email protected]("openai", "pydantic_ai")
+def test_session_headers_do_not_affect_other_providers() -> None:
+ config = AnyProviderConfig(api_key="test-key", base_url=None)
+ provider = get_completion_provider(
+ config, "openai/gpt-4o", session_id="chat-1"
+ )
+ assert provider.config.extra_headers is None
diff --git a/tests/_server/api/endpoints/test_ai.py b/tests/_server/api/endpoints/test_ai.py
index 94409c68a3..0a46038ff1 100644
--- a/tests/_server/api/endpoints/test_ai.py
+++ b/tests/_server/api/endpoints/test_ai.py
@@ -1322,3 +1322,72 @@ def test_resolve_completion_messages_from_ui_messages() -> None:
messages, support_multiple_cells = resolve_completion_messages(body)
assert support_multiple_cells is True
assert messages == ui_messages
+
+
+ ("endpoint", "body", "expected_session_id"),
+ [
+ (
+ "chat",
+ {"includeOtherCode": "", "uiMessages": [], "id": "chat-1"},
+ "chat-1",
+ ),
+ (
+ "chat",
+ {"includeOtherCode": "", "uiMessages": []},
+ f"{SESSION_ID}:chat",
+ ),
+ (
+ "completion",
+ {"includeOtherCode": "", "code": "", "prompt": "", "id": "edit-1"},
+ "edit-1",
+ ),
+ (
+ "completion",
+ {"includeOtherCode": "", "code": "", "prompt": ""},
+ f"{SESSION_ID}:completion",
+ ),
+ (
+ "inline_completion",
+ {"prefix": "", "suffix": ""},
+ f"{SESSION_ID}:inline_completion",
+ ),
+ ],
+)
+def test_ai_endpoints_forward_conversation_id(
+ client: TestClient,
+ temp_marimo_file: str,
+ endpoint: str,
+ body: dict[str, Any],
+ expected_session_id: str,
+) -> None:
+ @with_session(SESSION_ID)
+ def check_endpoint(client: TestClient) -> None:
+ user_config_manager = get_session_config_manager(client)
+ provider = MagicMock()
+ provider.stream_completion = AsyncMock(
+ return_value=_mock_stream_completion_response()
+ )
+ provider.stream_structured_completion = AsyncMock(
+ return_value=_mock_stream_completion_response()
+ )
+ provider.completion = AsyncMock(return_value="pass")
+ with (
+ patch.object(
+ user_config_manager,
+ "get_config",
+ return_value=_openai_config(),
+ ),
+ patch(
+ "marimo._server.api.endpoints.ai.get_completion_provider",
+ return_value=provider,
+ ) as factory,
+ ):
+ response = client.post(
+ f"/api/ai/{endpoint}", headers=HEADERS, json=body
+ )
+
+ assert response.status_code == 200, response.text
+ assert factory.call_args.kwargs["session_id"] == expected_session_id
+
+ check_endpoint(client, temp_marimo_file)