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): read OTel tool results from the schema's response field (#81121)

Christiaan Hendriksen2d0994ac9403
diff --git a/posthog/temporal/ai_observability/message_utils.py b/posthog/temporal/ai_observability/message_utils.py
index eb2a10d0fe0b..8e34feaf4d9d 100644
--- a/posthog/temporal/ai_observability/message_utils.py
+++ b/posthog/temporal/ai_observability/message_utils.py
@@ -62,8 +62,11 @@ def _flatten_parts_message(msg: dict) -> list[dict]:
conversation the trace view renders:
- `{"type": "text", "content": ...}` parts join into the message's `content`
- `{"type": "tool_call", "id", "name", "arguments"}` parts become `tool_calls`
- - each `{"type": "tool_call_response", "id", "result"}` part becomes its own
- follow-up `role: "tool"` message so the existing `tool[<id>]:` correlation applies
+ - each `{"type": "tool_call_response", "id", "response"}` part becomes its own
+ follow-up `role: "tool"` message so the existing `tool[<id>]:` correlation applies.
+ `response` is the field the GenAI semconv schema requires. The spec's own example
+ showed `result` for its first ten months, so producers that followed the example
+ are in the wild too, and both keys are read with `response` winning
- `reasoning` and other non-text parts are dropped, matching the trace view
Like the recipe, this only applies to dicts with a string `role` alongside the
@@ -98,10 +101,10 @@ def _flatten_parts_message(msg: dict) -> list[dict]:
}
)
elif part_type == "tool_call_response":
- result = part.get("result", "")
- if not isinstance(result, str):
- result = json.dumps(result, default=str)
- tool_results.append({"role": "tool", "content": result, "tool_call_id": part.get("id")})
+ tool_response = next((part[key] for key in ("response", "result") if part.get(key) is not None), "")
+ if not isinstance(tool_response, str):
+ tool_response = json.dumps(tool_response, default=str)
+ tool_results.append({"role": "tool", "content": tool_response, "tool_call_id": part.get("id")})
primary = {key: value for key, value in msg.items() if key != "parts"}
if text_chunks:
diff --git a/posthog/temporal/ai_observability/test_message_utils.py b/posthog/temporal/ai_observability/test_message_utils.py
index e942ef4ad7fa..62bc9a86d552 100644
--- a/posthog/temporal/ai_observability/test_message_utils.py
+++ b/posthog/temporal/ai_observability/test_message_utils.py
@@ -365,7 +365,22 @@ class TestExtractTextFromMessages:
]
assert extract_text_from_messages(messages) == "user: First. Second."
- def test_otel_parts_agentic_conversation_correlates_calls_and_results(self):
+ @pytest.mark.parametrize(
+ "response_part",
+ [
+ pytest.param({"type": "tool_call_response", "id": "call_1", "response": "-10C"}, id="schema_response_key"),
+ pytest.param({"type": "tool_call_response", "id": "call_1", "result": "-10C"}, id="example_result_key"),
+ pytest.param(
+ {"type": "tool_call_response", "id": "call_1", "response": "-10C", "result": "stale"},
+ id="response_wins_over_result",
+ ),
+ pytest.param(
+ {"type": "tool_call_response", "id": "call_1", "response": None, "result": "-10C"},
+ id="null_response_falls_back_to_result",
+ ),
+ ],
+ )
+ def test_otel_parts_agentic_conversation_correlates_calls_and_results(self, response_part):
messages = [
{"role": "user", "parts": [{"type": "text", "content": "Weather in Montreal?"}]},
{
@@ -380,7 +395,7 @@ class TestExtractTextFromMessages:
},
],
},
- {"role": "tool", "parts": [{"type": "tool_call_response", "id": "call_1", "result": "-10C"}]},
+ {"role": "tool", "parts": [response_part]},
{"role": "assistant", "parts": [{"type": "text", "content": "It is -10C."}]},
]
result = extract_text_from_messages(messages)
@@ -389,10 +404,29 @@ class TestExtractTextFromMessages:
assert "tool[call_1]: -10C" in result
assert "assistant: It is -10C." in result
assert "need the weather tool" not in result
+ assert "stale" not in result
+
+ @pytest.mark.parametrize(
+ "response_part",
+ [
+ pytest.param(
+ {"type": "tool_call_response", "id": "call_1", "response": "", "result": "stale"},
+ id="empty_string_response_wins_over_result",
+ ),
+ pytest.param(
+ {"type": "tool_call_response", "id": "call_1", "result": None},
+ id="null_result_renders_empty_not_null",
+ ),
+ ],
+ )
+ def test_otel_parts_tool_call_response_null_and_empty_values(self, response_part):
+ messages = [{"role": "tool", "parts": [response_part]}]
+ assert extract_text_from_messages(messages) == "tool[call_1]:"
- def test_otel_parts_dict_tool_call_response_result_is_stringified(self):
+ @pytest.mark.parametrize("field", ["response", "result"])
+ def test_otel_parts_dict_tool_call_response_is_stringified(self, field):
messages = [
- {"role": "tool", "parts": [{"type": "tool_call_response", "id": "call_1", "result": {"temperature": 25}}]}
+ {"role": "tool", "parts": [{"type": "tool_call_response", "id": "call_1", field: {"temperature": 25}}]}
]
assert extract_text_from_messages(messages) == 'tool[call_1]: {"temperature": 25}'
diff --git a/products/ai_observability/frontend/normalizer/recipe/default_recipes/otel.yaml b/products/ai_observability/frontend/normalizer/recipe/default_recipes/otel.yaml
index b9cf77b41feb..49a19ea5ffe6 100644
--- a/products/ai_observability/frontend/normalizer/recipe/default_recipes/otel.yaml
+++ b/products/ai_observability/frontend/normalizer/recipe/default_recipes/otel.yaml
@@ -1,6 +1,8 @@
# OTel parts format: `{role, parts: [...]}`. text parts become content,
# tool_call parts become tool_calls, and each tool_call_response part spawns its
-# own follow-up tool message.
+# own follow-up tool message. The GenAI semconv schema requires `response` for the
+# tool result, but its example showed `result` for ten months, so producers followed
+# both. Both keys are read, with `response` winning.
#
# `if_empty: ~` drops the primary only when followups exist, so a message whose
# parts produce nothing at all still emits one empty-content message (legacy parity).
@@ -40,4 +42,7 @@ rules:
role: tool
toolCallId: $.id
content:
- stringify: $.result
+ stringify:
+ coalesce:
+ - $.response
+ - $.result
diff --git a/products/ai_observability/frontend/utils.test.ts b/products/ai_observability/frontend/utils.test.ts
index 02dd3c7882b4..45928bd8aced 100644
--- a/products/ai_observability/frontend/utils.test.ts
+++ b/products/ai_observability/frontend/utils.test.ts
@@ -2159,7 +2159,12 @@ describe.each(IMPLS)('AI observability utils [$name]', ({ normalizeMessage, norm
expect(normalizeMessage(message, 'assistant')).toEqual([{ role: 'user', content: 'Hi' }])
})
- it('normalizes tool_call_response parts into tool messages', () => {
+ it.each([
+ ['the schema response key', { response: 'Sunny, 25°C' }],
+ ['the spec example result key', { result: 'Sunny, 25°C' }],
+ ['both keys, preferring response', { response: 'Sunny, 25°C', result: 'stale' }],
+ ['a null response falling back to result', { response: null, result: 'Sunny, 25°C' }],
+ ])('normalizes tool_call_response parts into tool messages from %s', (_label, resultFields) => {
const message = {
role: 'user',
parts: [
@@ -2167,7 +2172,7 @@ describe.each(IMPLS)('AI observability utils [$name]', ({ normalizeMessage, norm
type: 'tool_call_response',
id: 'call_abc',
name: 'get_weather',
- result: 'Sunny, 25°C',
+ ...resultFields,
},
],
}
@@ -2177,7 +2182,24 @@ describe.each(IMPLS)('AI observability utils [$name]', ({ normalizeMessage, norm
])
})
- it('stringifies non-string tool_call_response results', () => {
+ it('keeps an empty string response over a stale result', () => {
+ const message = {
+ role: 'user',
+ parts: [
+ {
+ type: 'tool_call_response',
+ id: 'call_abc',
+ name: 'get_weather',
+ response: '',
+ result: 'stale',
+ },
+ ],
+ }
+
+ expect(normalizeMessage(message, 'user')).toEqual([{ role: 'tool', content: '', tool_call_id: 'call_abc' }])
+ })
+
+ it.each(['response', 'result'])('stringifies non-string tool_call_response %s values', (field) => {
const message = {
role: 'user',
parts: [
@@ -2185,7 +2207,7 @@ describe.each(IMPLS)('AI observability utils [$name]', ({ normalizeMessage, norm
type: 'tool_call_response',
id: 'call_1',
name: 'get_data',
- result: { temperature: 25, unit: 'C' },
+ [field]: { temperature: 25, unit: 'C' },
},
],
}