Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(aio): show user message in Vercel AI trace input preview (#90668)

posthog[bot]cc23f3460828
diff --git a/products/ai_observability/frontend/AIObservabilityTracesScene.tsx b/products/ai_observability/frontend/AIObservabilityTracesScene.tsx
index 291d01c278c0..0be236e526b2 100644
--- a/products/ai_observability/frontend/AIObservabilityTracesScene.tsx
+++ b/products/ai_observability/frontend/AIObservabilityTracesScene.tsx
@@ -542,10 +542,11 @@ export function pickLastOutputMessage(
* Some SDKs emit the trace input/output as a state wrapper object rather than a
* bare messages array. Langchain/LangGraph writes `$ai_input_state` /
* `$ai_output_state` as something like `{ agent_mode, messages: [...], ... }`.
- * Drill into the known `.messages` key so the picker sees a clean array; for
- * unknown wrapper shapes (agent-specific state like `{ current_step, ... }`)
- * return `null` in strict mode so the picker can fall through to the
- * generation-level fallback rather than dumping raw JSON.
+ * Drill into the known `.messages` key so the picker sees a clean array. The
+ * Vercel AI OTel path writes a single bare message object (`{ role, content }`),
+ * so wrap that as a one-element array. For unknown wrapper shapes (agent-specific
+ * state like `{ current_step, ... }`) return `null` in strict mode so the picker
+ * can fall through to the generation-level fallback rather than dumping raw JSON.
*/
function unwrapMessageContainer(raw: unknown, strict: boolean): unknown {
if (raw == null || typeof raw !== 'object' || Array.isArray(raw)) {
@@ -555,9 +556,22 @@ function unwrapMessageContainer(raw: unknown, strict: boolean): unknown {
if (Array.isArray(obj.messages)) {
return obj.messages
}
+ if (isSingleMessage(obj)) {
+ return [obj]
+ }
return strict ? null : raw
}
+/**
+ * `role` alone is too weak a signal, since a state wrapper can carry one too.
+ * Requiring the payload as well (`content` for the chat shapes, a `parts` array
+ * for the OTel one) keeps those wrappers falling through to the strict-mode
+ * fallback.
+ */
+function isSingleMessage(obj: Record<string, unknown>): boolean {
+ return typeof obj.role === 'string' && ('content' in obj || Array.isArray(obj.parts))
+}
+
function safeNormalize(
raw: unknown,
defaultRole: string,
diff --git a/products/ai_observability/frontend/tracesInputPreview.test.ts b/products/ai_observability/frontend/tracesInputPreview.test.ts
index fd7e8f15170a..d018b991c32a 100644
--- a/products/ai_observability/frontend/tracesInputPreview.test.ts
+++ b/products/ai_observability/frontend/tracesInputPreview.test.ts
@@ -35,6 +35,16 @@ describe('pickLastInputMessage', () => {
expect(pickLastInputMessage(conversation)).toEqual({ role: 'assistant', content: 'tool result' })
})
+ it.each([
+ ['content', { role: 'user', content: 'what is the weather?' }],
+ ['parts', { role: 'user', parts: [{ type: 'text', content: 'what is the weather?' }] }],
+ ])('wraps a single bare %s message from the Vercel AI OTel path', (_, message) => {
+ expect(pickLastInputMessage(message, { strict: true })).toEqual({
+ role: 'user',
+ content: 'what is the weather?',
+ })
+ })
+
it('rejects unknown state-wrapper shapes in strict mode', () => {
expect(pickLastInputMessage({ current_step: 3 }, { strict: true })).toBeNull()
})
diff --git a/products/ai_observability/frontend/tracesOutputPreview.test.ts b/products/ai_observability/frontend/tracesOutputPreview.test.ts
index 85d89d5ef6ba..f014f8d3e646 100644
--- a/products/ai_observability/frontend/tracesOutputPreview.test.ts
+++ b/products/ai_observability/frontend/tracesOutputPreview.test.ts
@@ -71,6 +71,11 @@ describe('pickLastOutputMessage', () => {
expect(pickLastOutputMessage(state)).toMatchObject({ role: 'assistant', content: 'final answer' })
})
+ it('wraps a single bare assistant message from the Vercel AI OTel path', () => {
+ const message = { role: 'assistant', content: 'It is 72F.' }
+ expect(pickLastOutputMessage(message, { strict: true })).toMatchObject(message)
+ })
+
it('rejects unknown state-wrapper shapes in strict mode', () => {
expect(pickLastOutputMessage({ current_step: 3 }, { strict: true })).toBeNull()
})