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(llma): extract pydantic-ai tool calls from output parts (#59755)

shauryapednekar42a7cf3e99d4
diff --git a/nodejs/src/ingestion/ai/process-ai-event.test.ts b/nodejs/src/ingestion/ai/process-ai-event.test.ts
index a9a3c3b9a2f2..429f0d255591 100644
--- a/nodejs/src/ingestion/ai/process-ai-event.test.ts
+++ b/nodejs/src/ingestion/ai/process-ai-event.test.ts
@@ -244,6 +244,64 @@ describe('processAiEvent()', () => {
})
})
+ describe('OTel ingestion', () => {
+ it('extracts Pydantic AI tool calls from output message parts', () => {
+ event.properties = {
+ $ai_ingestion_source: 'otel',
+ 'logfire.json_schema': '{}',
+ 'gen_ai.response.model': 'testing_model',
+ 'gen_ai.provider.name': 'openai',
+ 'gen_ai.usage.input_tokens': 100,
+ 'gen_ai.usage.output_tokens': 50,
+ 'gen_ai.output.messages': JSON.stringify([
+ {
+ role: 'assistant',
+ parts: [
+ { type: 'text', content: 'Let me check.' },
+ {
+ type: 'tool_call',
+ id: 'call_weather',
+ name: 'get_weather',
+ arguments: '{"city":"NYC"}',
+ },
+ {
+ type: 'tool_call',
+ id: 'call_docs',
+ name: 'search_docs',
+ arguments: '{"query":"weather"}',
+ },
+ ],
+ },
+ ]),
+ }
+
+ const result = processAiEvent(event)
+
+ expect(result.properties!.$ai_output_choices).toEqual([
+ {
+ role: 'assistant',
+ parts: [
+ { type: 'text', content: 'Let me check.' },
+ {
+ type: 'tool_call',
+ id: 'call_weather',
+ name: 'get_weather',
+ arguments: '{"city":"NYC"}',
+ },
+ {
+ type: 'tool_call',
+ id: 'call_docs',
+ name: 'search_docs',
+ arguments: '{"query":"weather"}',
+ },
+ ],
+ },
+ ])
+ expect(result.properties!.$ai_tools_called).toBe('get_weather,search_docs')
+ expect(result.properties!.$ai_tool_call_count).toBe(2)
+ })
+ })
+
describe('model matching', () => {
it('matches exact model names', () => {
const result = processAiEvent(event)
diff --git a/nodejs/src/ingestion/ai/tools/extract-tool-calls.test.ts b/nodejs/src/ingestion/ai/tools/extract-tool-calls.test.ts
index dbe64d926508..f84fac3b6aca 100644
--- a/nodejs/src/ingestion/ai/tools/extract-tool-calls.test.ts
+++ b/nodejs/src/ingestion/ai/tools/extract-tool-calls.test.ts
@@ -170,6 +170,93 @@ describe('extractToolCallNames', () => {
})
})
+ describe('Pydantic AI OTel format (parts with type=tool_call)', () => {
+ it.each([
+ [
+ 'single tool_call part',
+ [
+ {
+ role: 'assistant',
+ parts: [
+ {
+ type: 'tool_call',
+ id: 'call_abc',
+ name: 'get_weather',
+ arguments: '{"city":"NYC"}',
+ },
+ ],
+ },
+ ],
+ ['get_weather'],
+ ],
+ [
+ 'multiple tool_call parts',
+ [
+ {
+ role: 'assistant',
+ parts: [
+ { type: 'tool_call', id: 'call_1', name: 'get_weather', arguments: '{}' },
+ { type: 'tool_call', id: 'call_2', name: 'search_docs', arguments: '{}' },
+ ],
+ },
+ ],
+ ['get_weather', 'search_docs'],
+ ],
+ [
+ 'mixed text and tool_call parts',
+ [
+ {
+ role: 'assistant',
+ parts: [
+ { type: 'text', content: 'Let me check.' },
+ {
+ type: 'tool_call',
+ id: 'call_1',
+ name: 'get_weather',
+ arguments: '{"city":"NYC"}',
+ },
+ ],
+ },
+ ],
+ ['get_weather'],
+ ],
+ [
+ 'tool_call parts inside message wrapper',
+ [
+ {
+ message: {
+ parts: [
+ {
+ type: 'tool_call',
+ id: 'call_abc',
+ name: 'get_weather',
+ arguments: '{}',
+ },
+ ],
+ },
+ },
+ ],
+ ['get_weather'],
+ ],
+ [
+ 'ignores malformed tool_call parts without string names',
+ [
+ {
+ role: 'assistant',
+ parts: [
+ { type: 'tool_call', id: 'call_1', arguments: '{}' },
+ { type: 'tool_call', id: 'call_2', name: 123, arguments: '{}' },
+ { type: 'tool_call', id: 'call_3', name: 'search_docs', arguments: '{}' },
+ ],
+ },
+ ],
+ ['search_docs'],
+ ],
+ ])('%s', (_description, input, expected) => {
+ expect(extractToolCallNames(input)).toEqual(expected)
+ })
+ })
+
describe('OpenAI Responses API (flat function_call items)', () => {
it.each([
[
@@ -591,6 +678,36 @@ describe('processAiToolCallExtraction', () => {
expect(result.properties!['$ai_tool_call_count']).toBe(1)
})
+ it('extracts Pydantic AI OTel tool_call parts from stringified JSON', () => {
+ const event = createEvent('$ai_generation', {
+ $ai_output_choices: JSON.stringify([
+ {
+ role: 'assistant',
+ parts: [
+ { type: 'text', content: 'Let me check.' },
+ {
+ type: 'tool_call',
+ id: 'call_abc',
+ name: 'get_weather',
+ arguments: '{"city":"NYC"}',
+ },
+ {
+ type: 'tool_call',
+ id: 'call_def',
+ name: 'search_docs',
+ arguments: '{"q":"weather"}',
+ },
+ ],
+ },
+ ]),
+ })
+
+ const result = processAiToolCallExtraction(event)
+
+ expect(result.properties!['$ai_tools_called']).toBe('get_weather,search_docs')
+ expect(result.properties!['$ai_tool_call_count']).toBe(2)
+ })
+
it('extracts normalized format with content.type=function', () => {
const event = createEvent('$ai_generation', {
$ai_output_choices: [
diff --git a/nodejs/src/ingestion/ai/tools/extract-tool-calls.ts b/nodejs/src/ingestion/ai/tools/extract-tool-calls.ts
index cf2520ee8549..1f7664113681 100644
--- a/nodejs/src/ingestion/ai/tools/extract-tool-calls.ts
+++ b/nodejs/src/ingestion/ai/tools/extract-tool-calls.ts
@@ -7,6 +7,7 @@
* - OpenAI Responses API: flat `[].type="function_call"` with `name` at top level
* - Normalized SDK: `[].content[].type="function"` with `function.name`
* - Vercel AI SDK / OTel: `[].content[].type="tool-call"` with `function.name`
+ * - Pydantic AI OTel: `[].parts[].type="tool_call"` with `name`
* - Anthropic: `[].message.content[].type="tool_use"` with `name`
* - Python repr (OpenAI Agents SDK): `ResponseFunctionToolCall(name='...')`
*/
@@ -27,10 +28,12 @@ interface OutputItem {
name?: string
role?: string
content?: ToolCallItem[]
+ parts?: ToolCallItem[]
tool_calls?: ToolCallItem[]
message?: {
tool_calls?: ToolCallItem[]
content?: ToolCallItem[]
+ parts?: ToolCallItem[]
}
}
@@ -67,26 +70,30 @@ function extractFromToolCalls(toolCalls: unknown[], names: string[], cap: number
}
}
-function extractFromContentBlocks(content: unknown[], names: string[], cap: number): void {
- for (const block of content) {
+function extractFromMessageParts(parts: unknown[], names: string[], cap: number): void {
+ for (const part of parts) {
if (names.length >= cap) {
return
}
- if (typeof block !== 'object' || block === null) {
+ if (typeof part !== 'object' || part === null) {
continue
}
- const cb = block as ToolCallItem
+ const messagePart = part as ToolCallItem
+ // Pydantic AI OTel: {type: "tool_call", name: "..."}
+ if (messagePart.type === 'tool_call' && messagePart.name && typeof messagePart.name === 'string') {
+ pushSanitized(names, messagePart.name)
+ }
// Anthropic: {type: "tool_use", name: "..."}
- if (cb.type === 'tool_use' && cb.name && typeof cb.name === 'string') {
- pushSanitized(names, cb.name)
+ else if (messagePart.type === 'tool_use' && messagePart.name && typeof messagePart.name === 'string') {
+ pushSanitized(names, messagePart.name)
}
// Normalized OpenAI / Vercel AI SDK / OTel: {type: "function"|"tool-call", function: {name: "..."}}
else if (
- (cb.type === 'function' || cb.type === 'tool-call') &&
- cb.function?.name &&
- typeof cb.function.name === 'string'
+ (messagePart.type === 'function' || messagePart.type === 'tool-call') &&
+ messagePart.function?.name &&
+ typeof messagePart.function.name === 'string'
) {
- pushSanitized(names, cb.function.name)
+ pushSanitized(names, messagePart.function.name)
}
}
}
@@ -158,7 +165,10 @@ export function extractToolCallNames(outputChoices: unknown, rawString?: string)
extractFromToolCalls(c.message.tool_calls, names, MAX_TOOLS_PER_EVENT)
}
if ('content' in c.message && Array.isArray(c.message.content)) {
- extractFromContentBlocks(c.message.content, names, MAX_TOOLS_PER_EVENT)
+ extractFromMessageParts(c.message.content, names, MAX_TOOLS_PER_EVENT)
+ }
+ if ('parts' in c.message && Array.isArray(c.message.parts)) {
+ extractFromMessageParts(c.message.parts, names, MAX_TOOLS_PER_EVENT)
}
} else {
// Unwrapped: {tool_calls: [...], role: "assistant"} (no message wrapper)
@@ -168,7 +178,12 @@ export function extractToolCallNames(outputChoices: unknown, rawString?: string)
// Normalized: {content: [...], role: "assistant"} (no message wrapper)
if ('content' in c && Array.isArray(c.content)) {
- extractFromContentBlocks(c.content, names, MAX_TOOLS_PER_EVENT)
+ extractFromMessageParts(c.content, names, MAX_TOOLS_PER_EVENT)
+ }
+
+ // Pydantic AI OTel: {parts: [{type: "tool_call", name: "..."}], role: "assistant"}
+ if ('parts' in c && Array.isArray(c.parts)) {
+ extractFromMessageParts(c.parts, names, MAX_TOOLS_PER_EVENT)
}
}
}