Skip to content

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

fix(chatml): render Google ADK invocation root spans as chat messages (#15299)

Milana Gurbanovadc188a7f4726
diff --git a/packages/shared/src/utils/chatml/adapters/gemini.ts b/packages/shared/src/utils/chatml/adapters/gemini.ts
index 7df3d8d6ca..3a665186bd 100644
--- a/packages/shared/src/utils/chatml/adapters/gemini.ts
+++ b/packages/shared/src/utils/chatml/adapters/gemini.ts
@@ -35,6 +35,17 @@ const GeminiADKOutputSchema = z.looseObject({
}),
});
+// ADK invocation input format (root span of the OpenInference ADK
+// instrumentation): {user_id, session_id, new_message: {parts, role}, run_config}
+const GeminiADKInvocationInputSchema = z.looseObject({
+ user_id: z.string(),
+ session_id: z.string(),
+ new_message: z.looseObject({
+ parts: z.array(z.any()),
+ role: z.string().optional(),
+ }),
+});
+
// ADK input format: {config: {tools, system_instruction}, contents: [...]}
const GeminiADKInputSchema = z.looseObject({
config: z.looseObject({
@@ -378,7 +389,20 @@ function preprocessData(data: unknown): unknown {
}
// ========================================
- // STEP 3: Handle ADK input format
+ // STEP 3: Unwrap ADK invocation input format (root invocation span)
+ // ========================================
+ // {new_message: {parts, role}, user_id, session_id, run_config, ...} → [{parts, role}]
+ if (GeminiADKInvocationInputSchema.safeParse(data).success) {
+ const obj = data as Record<string, unknown>;
+ const newMessage = obj.new_message as Record<string, unknown>;
+ if ("parts" in newMessage && Array.isArray(newMessage.parts)) {
+ // new_message is the user's request to the agent; default the role
+ return normalizeMessages([{ role: "user", ...newMessage }]);
+ }
+ }
+
+ // ========================================
+ // STEP 4: Handle ADK input format
// ========================================
// {config: {tools, system_instruction}, contents: [...]}
if (GeminiADKInputSchema.safeParse(data).success) {
@@ -424,7 +448,7 @@ function preprocessData(data: unknown): unknown {
}
// ========================================
- // STEP 4: Handle simple request format
+ // STEP 5: Handle simple request format
// ========================================
// {contents: [{parts, role}], model: "..."}
if (GeminiRequestSchema.safeParse(data).success) {
@@ -433,14 +457,14 @@ function preprocessData(data: unknown): unknown {
}
// ========================================
- // STEP 5: Handle arrays
+ // STEP 6: Handle arrays
// ========================================
if (Array.isArray(data)) {
return normalizeMessages(data);
}
// ========================================
- // STEP 6: Handle messages wrapper
+ // STEP 7: Handle messages wrapper
// ========================================
if (typeof data === "object" && "messages" in data) {
const obj = data as Record<string, unknown>;
@@ -494,12 +518,15 @@ export const geminiAdapter: ProviderAdapter = {
// STRUCTURAL: Schema-based detection on metadata (check metadata first for performance)
if (GeminiRequestSchema.safeParse(ctx.metadata).success) return true;
if (GeminiADKInputSchema.safeParse(ctx.metadata).success) return true;
+ if (GeminiADKInvocationInputSchema.safeParse(ctx.metadata).success)
+ return true;
if (GeminiRawAPISchema.safeParse(ctx.metadata).success) return true;
if (GeminiADKOutputSchema.safeParse(ctx.metadata).success) return true;
// Schema-based detection on data (slower, do last)
if (GeminiRequestSchema.safeParse(ctx.data).success) return true;
if (GeminiADKInputSchema.safeParse(ctx.data).success) return true;
+ if (GeminiADKInvocationInputSchema.safeParse(ctx.data).success) return true;
if (GeminiRawAPISchema.safeParse(ctx.data).success) return true;
if (GeminiADKOutputSchema.safeParse(ctx.data).success) return true;
diff --git a/worker/src/__tests__/chatml/adapters/gemini.test.ts b/worker/src/__tests__/chatml/adapters/gemini.test.ts
index 2f615716a9..9d4ae31509 100644
--- a/worker/src/__tests__/chatml/adapters/gemini.test.ts
+++ b/worker/src/__tests__/chatml/adapters/gemini.test.ts
@@ -3,6 +3,9 @@ import {
geminiAdapter,
selectAdapter,
SimpleChatMlArraySchema,
+ normalizeInput as chatmlNormalizeInput,
+ normalizeOutput as chatmlNormalizeOutput,
+ combineInputOutputMessages,
type NormalizerContext,
} from "@langfuse/shared";
@@ -270,3 +273,122 @@ describe("geminiAdapter", () => {
});
});
});
+
+// Real payloads from a Google ADK invocation root span
+// (openinference-instrumentation-google-adk 0.1.17, google-adk 2.4.0).
+// Regression tests for https://github.com/langfuse/langfuse/issues/13292
+describe("geminiAdapter: Google ADK invocation root span", () => {
+ const invocationInput = {
+ user_id: "demo-user",
+ session_id: "demo-session",
+ invocation_id: null,
+ new_message: {
+ parts: [
+ {
+ text: "hi",
+ },
+ ],
+ role: "user",
+ },
+ state_delta: null,
+ run_config: {
+ save_input_blobs_as_artifacts: false,
+ support_cfc: false,
+ streaming_mode: "StreamingMode.NONE",
+ output_audio_transcription: {},
+ input_audio_transcription: {},
+ save_live_blob: false,
+ save_live_audio: false,
+ max_llm_calls: 500,
+ },
+ yield_user_message: false,
+ };
+
+ const invocationOutput = {
+ model_version: "gemini-3.1-flash-lite",
+ content: {
+ parts: [
+ {
+ text: "Hello! How can I help you today?",
+ thought_signature:
+ "EjQKMgERTTIP4vHbi_GtaO9Rcz5K7VC1uJkE5IsctGfJn7xqR9usflAYTBhwqAuJMyGmdnsy",
+ },
+ ],
+ role: "model",
+ },
+ finish_reason: "STOP",
+ usage_metadata: {
+ candidates_token_count: 9,
+ prompt_token_count: 62,
+ prompt_tokens_details: [
+ {
+ modality: "TEXT",
+ token_count: 62,
+ },
+ ],
+ total_token_count: 71,
+ },
+ invocation_id: "e-db33baf0-bb57-4b21-a6f7-a222364a4309",
+ author: "hello_agent",
+ actions: {
+ state_delta: {},
+ artifact_delta: {},
+ requested_auth_configs: {},
+ requested_tool_confirmations: {},
+ },
+ node_info: {
+ path: "hello_agent@1",
+ },
+ id: "8f01c045-0d98-421a-848f-cf25fc4fe1d3",
+ timestamp: 1784663909.934617,
+ };
+
+ const observationMetadata = {
+ attributes: {
+ "langfuse.internal.is_app_root": "true",
+ "input.mime_type": "application/json",
+ "user.id": "demo-user",
+ "session.id": "demo-session",
+ "output.mime_type": "application/json",
+ "openinference.span.kind": "CHAIN",
+ },
+ resourceAttributes: {
+ "telemetry.sdk.language": "python",
+ "telemetry.sdk.name": "opentelemetry",
+ "telemetry.sdk.version": "1.42.1",
+ "service.name": "unknown_service",
+ },
+ scope: {
+ name: "openinference.instrumentation.google_adk",
+ version: "0.1.17",
+ attributes: {},
+ },
+ };
+
+ // ctx exactly as parseChatML (useChatMLParser.ts) builds it
+ const ctx: NormalizerContext = {
+ metadata: observationMetadata,
+ observationName: "invocation [hello_app]",
+ };
+
+ it("normalizes the invocation input envelope into a user message", () => {
+ const res = chatmlNormalizeInput(invocationInput, ctx);
+ expect(res.success).toBe(true);
+ if (!res.success) return;
+ expect(res.data).toHaveLength(1);
+ expect(res.data[0].role).toBe("user");
+ expect(res.data[0].content).toBe("hi");
+ });
+
+ it("combines invocation input and output so the trace renders as chat", () => {
+ const inResult = chatmlNormalizeInput(invocationInput, ctx);
+ const outResult = chatmlNormalizeOutput(invocationOutput, ctx);
+ const messages = combineInputOutputMessages(
+ inResult,
+ outResult,
+ invocationOutput,
+ );
+ expect(messages).toHaveLength(2);
+ expect(messages[1].role).toBe("model");
+ });
+});
diff --git a/worker/src/__tests__/chatml/adapters/generic.test.ts b/worker/src/__tests__/chatml/adapters/generic.test.ts
index b3aa6fbcc1..366930bef3 100644
--- a/worker/src/__tests__/chatml/adapters/generic.test.ts
+++ b/worker/src/__tests__/chatml/adapters/generic.test.ts
@@ -44,21 +44,20 @@ describe("Generic Adapter", () => {
it("should not display non-ChatML formats as chat", () => {
const input = {
- new_message: {
- parts: [{ text: "hi" }],
- role: "user",
+ job_request: {
+ steps: [{ name: "fetch" }],
+ priority: "high",
},
- run_config: {
- streaming_mode: "StreamingMode.NONE",
+ retry_policy: {
+ max_attempts: 3,
},
};
const output = {
- content: {
- parts: [{ text: "Hello!" }],
- role: "model",
+ status: "completed",
+ metrics: {
+ duration_ms: 12,
},
- finish_reason: "STOP",
};
const inResult = normalizeInput(input);
diff --git a/worker/src/__tests__/chatml/framework-traces/google-adk-2025-08-28.chatml.json b/worker/src/__tests__/chatml/framework-traces/google-adk-2025-08-28.chatml.json
index 041d49bd8c..a6383d4833 100644
--- a/worker/src/__tests__/chatml/framework-traces/google-adk-2025-08-28.chatml.json
+++ b/worker/src/__tests__/chatml/framework-traces/google-adk-2025-08-28.chatml.json
@@ -41,11 +41,13 @@
},
"86cd4912944ddc41": {
"input": {
- "success": false,
- "error": {
- "name": "ZodError",
- "message": "[\n {\n \"expected\": \"array\",\n \"code\": \"invalid_type\",\n \"path\": [],\n \"message\": \"Invalid input: expected array, received object\"\n }\n]"
- }
+ "success": true,
+ "data": [
+ {
+ "role": "user",
+ "content": "hi"
+ }
+ ]
},
"output": {
"success": true,