Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat(ui): Add trace download button, exports trace and observation metadata as json (#7668)

Marc Klingen0d2586e4f28f
diff --git a/web/src/components/trace/index.tsx b/web/src/components/trace/index.tsx
index c7bb4b6b7e..8a38365670 100644
--- a/web/src/components/trace/index.tsx
+++ b/web/src/components/trace/index.tsx
@@ -10,7 +10,7 @@ import {
import { type ObservationReturnTypeWithMetadata } from "@/src/server/api/routers/traces";
import { api } from "@/src/utils/api";
import useLocalStorage from "@/src/components/useLocalStorage";
-import { Settings2, ChevronsUpDown, ChevronsDownUp } from "lucide-react";
+import { Settings2, ChevronsUpDown, ChevronsDownUp, Download } from "lucide-react";
import { useCallback, useState } from "react";
import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
import { TraceTimelineView } from "@/src/components/trace/TraceTimelineView";
@@ -205,6 +205,27 @@ export function Trace(props: {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
+ const downloadTraceAsJson = useCallback(() => {
+ const exportData = {
+ trace: props.trace,
+ observations: props.observations,
+ };
+
+ const jsonString = JSON.stringify(exportData, null, 2);
+ const blob = new Blob([jsonString], { type: "application/json" });
+ const url = URL.createObjectURL(blob);
+
+ const link = document.createElement("a");
+ link.href = url;
+ link.download = `trace-${props.trace.id}.json`;
+ document.body.appendChild(link);
+ link.click();
+ document.body.removeChild(link);
+ URL.revokeObjectURL(url);
+
+ capture("trace_detail:download_button_click");
+ }, [props.trace, props.observations, capture]);
+
const [expandedItems, setExpandedItems] = useSessionStorage<string[]>(
`${props.trace.id}-expanded`,
[`trace-${props.trace.id}`],
@@ -388,6 +409,14 @@ export function Trace(props: {
</DropdownMenuSub>
</DropdownMenuContent>
</DropdownMenu>
+ <Button
+ variant="ghost"
+ size="icon"
+ onClick={downloadTraceAsJson}
+ title="Download trace as JSON"
+ >
+ <Download className="h-4 w-4" />
+ </Button>
<Switch
checked={props.selectedTab?.includes("timeline")}
onCheckedChange={(checked) =>
diff --git a/web/src/features/posthog-analytics/usePostHogClientCapture.ts b/web/src/features/posthog-analytics/usePostHogClientCapture.ts
index ac4ae4d4b0..62e3b38c32 100644
--- a/web/src/features/posthog-analytics/usePostHogClientCapture.ts
+++ b/web/src/features/posthog-analytics/usePostHogClientCapture.ts
@@ -27,6 +27,7 @@ const events = {
"io_pretty_format_toggle_group",
"test_in_playground_button_click",
"display_mode_switch",
+ "download_button_click",
],
generations: ["export"],
saved_views: [