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
Instrument pydantic-ai for editor AI OpenTelemetry tracing (#9905)
Shahmir Varqha0946cb693ef3
diff --git a/development_docs/traces.md b/development_docs/traces.mdindex 9418f60584..15f9ddc4c3 100644--- a/development_docs/traces.md+++ b/development_docs/traces.md@@ -99,3 +99,10 @@ If the notebook exits gracefully (i.e., is shut down manually), profilingstatistics will be written to the profiles/ directory. You can then usestandard tools to analyze the dumped statistics. To view flamegraphs,we recommend snakeviz or tuna (`uvx snakeviz path_to_profile`)++## AI Tracing++When `MARIMO_TRACING=true` and `pydantic_ai` is installed, marimo automatically calls+`Agent.instrument_all()` at server startup so editor AI (chat panel,+completions, inline completion) emits OpenTelemetry spans through the+same exporter as other server traces.diff --git a/marimo/_tracer.py b/marimo/_tracer.pyindex e98715ba5f..f1bcb35ce1 100644--- a/marimo/_tracer.py+++ b/marimo/_tracer.py@@ -213,6 +213,25 @@ def _set_tracer_provider() -> None:# Sets the global default tracer providertrace.set_tracer_provider(provider)+ _instrument_ai(provider)+++def _instrument_ai(provider: trace.TracerProvider) -> None:+ """Current AI integrations use pydantic_ai, so we instrument it globally."""++ if not DependencyManager.pydantic_ai.has():+ LOGGER.debug("Skipping AI instrumentation (pydantic_ai not installed)")+ return++ try:+ from pydantic_ai import Agent+ from pydantic_ai.models.instrumented import InstrumentationSettings++ Agent.instrument_all(InstrumentationSettings(tracer_provider=provider))+ LOGGER.debug("Enabled AI instrumentation")+ except Exception as e:+ LOGGER.debug("AI instrumentation failed: %s", e)+def create_tracer(trace_name: str) -> trace.Tracer:"""diff --git a/tests/test_tracer.py b/tests/test_tracer.pyindex 633ba35304..ef09847a33 100644--- a/tests/test_tracer.py+++ b/tests/test_tracer.py@@ -1,6 +1,7 @@# Copyright 2026 Marimo. All rights reserved.from __future__ import annotations+import sysfrom unittest.mock import MagicMock, patchimport pytest@@ -238,6 +239,28 @@ class TestSetTracerProvider:provider = trace.get_tracer_provider()assert not hasattr(provider, "_active_span_processor")+ def test_instruments_ai_with_built_provider(+ self,+ monkeypatch: pytest.MonkeyPatch,+ ) -> None:+ # Guards against dropping the _instrument_ai() call in+ # _set_tracer_provider().+ monkeypatch.delenv("OTEL_EXPORTER_OTLP_ENDPOINT", raising=False)+ monkeypatch.delenv("OTEL_EXPORTER_OTLP_TRACES_ENDPOINT", raising=False)++ from opentelemetry import trace++ # Import while tracing is disabled to avoid import-time instrumentation.+ import marimo._tracer as tracer_module+ from marimo._config.settings import GLOBAL_SETTINGS++ monkeypatch.setattr(GLOBAL_SETTINGS, "TRACING", True)++ with patch.object(tracer_module, "_instrument_ai") as mock_instrument:+ tracer_module._set_tracer_provider()++ mock_instrument.assert_called_once_with(trace.get_tracer_provider())+@pytest.mark.requires("opentelemetry")class TestCreateTracer:@@ -262,3 +285,72 @@ class TestCreateTracer:monkeypatch.setattr(GLOBAL_SETTINGS, "TRACING", True)tracer = create_tracer("test.real")assert not isinstance(tracer, MockTracer)+++class TestInstrumentAI:+ """Tests for _instrument_ai() pydantic_ai instrumentation."""++ def test_skips_when_pydantic_ai_not_installed(+ self,+ monkeypatch: pytest.MonkeyPatch,+ ) -> None:+ from marimo._dependencies.dependencies import DependencyManager+ from marimo._tracer import _instrument_ai++ monkeypatch.setattr(+ DependencyManager.pydantic_ai, "has", lambda *_, **__: False+ )++ # Inject a fake module so we can prove it's never touched.+ fake_agent = MagicMock()+ fake_module = MagicMock()+ fake_module.Agent = fake_agent+ monkeypatch.setitem(sys.modules, "pydantic_ai", fake_module)++ _instrument_ai(MagicMock())++ fake_agent.instrument_all.assert_not_called()++ @pytest.mark.requires("pydantic_ai")+ def test_instruments_when_pydantic_ai_installed(+ self,+ monkeypatch: pytest.MonkeyPatch,+ ) -> None:+ from marimo._dependencies.dependencies import DependencyManager+ from marimo._tracer import _instrument_ai++ monkeypatch.setattr(+ DependencyManager.pydantic_ai, "has", lambda *_, **__: True+ )++ from pydantic_ai.models.instrumented import InstrumentationSettings++ provider = MagicMock()+ with patch("pydantic_ai.Agent.instrument_all") as mock_instrument:+ _instrument_ai(provider)++ mock_instrument.assert_called_once()+ settings = mock_instrument.call_args.args[0]+ assert isinstance(settings, InstrumentationSettings)+ # InstrumentationSettings builds its tracer from the provider rather+ # than storing the provider itself, so assert the provider was used.+ assert settings.tracer is provider.get_tracer.return_value++ @pytest.mark.requires("pydantic_ai")+ def test_swallows_exceptions(+ self,+ monkeypatch: pytest.MonkeyPatch,+ ) -> None:+ from marimo._dependencies.dependencies import DependencyManager+ from marimo._tracer import _instrument_ai++ monkeypatch.setattr(+ DependencyManager.pydantic_ai, "has", lambda *_, **__: True+ )++ # Should not raise even though instrument_all blows up.+ with patch(+ "pydantic_ai.Agent.instrument_all",+ side_effect=RuntimeError("boom"),+ ):+ _instrument_ai(MagicMock())