: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
from collections.abc import Mapping, Sequence
from typing import Any, Literal, cast
from langchain_core import messages
from langchain_core.messages import BaseMessage
from posthog.schema import AssistantMessage, AssistantToolCallMessage, ContextMessage, FailureMessage, HumanMessage
from ee.hogai.utils.types.base import AssistantMessageUnion
# The blocks below MUST be preserved as in the original Anthropic responses, in order for thinking signatures to match up
SUPPORTED_ANTHROPIC_BLOCKS = ("thinking", "redacted_thinking", "server_tool_use", "web_search_tool_result")
def get_anthropic_thinking_from_assistant_message(message: AssistantMessage) -> list[dict[str, Any]]:
if message.meta and message.meta.thinking:
return [item for item in message.meta.thinking if item["type"] in SUPPORTED_ANTHROPIC_BLOCKS]
return []
def add_cache_control(message: BaseMessage, ttl: Literal["5m", "1h"] | None = None) -> BaseMessage:
ttl = ttl or "5m"
if isinstance(message.content, str):
message.content = [
{"type": "text", "text": message.content, "cache_control": {"type": "ephemeral", "ttl": ttl}},
]
if message.content:
last_content = message.content[-1]
if isinstance(last_content, str):
message.content[-1] = {
"type": "text",
"text": last_content,
"cache_control": {"type": "ephemeral", "ttl": ttl},
}
else:
last_content["cache_control"] = {"type": "ephemeral", "ttl": ttl}
return message
def convert_human_message_to_anthropic_message(message: HumanMessage) -> messages.HumanMessage:
return messages.HumanMessage(content=[{"type": "text", "text": message.content}])
def convert_context_message_to_anthropic_message(message: ContextMessage) -> messages.HumanMessage:
return messages.HumanMessage(content=[{"type": "text", "text": message.content}])
def _get_provenance_note(message: AssistantMessage) -> str | None:
if not message.meta or not message.meta.source:
return None
source = message.meta.source
if source.startswith("slash_command:"):
command_name = source.split(":", 1)[1]
return (
f"[System note: the previous assistant message was produced by the /{command_name} slash command. "
"It is deterministic PostHog code, not assistant-authored content.]"
)
return None
def convert_assistant_message_to_anthropic_message(
message: AssistantMessage, tool_result_map: Mapping[str, AssistantToolCallMessage]
) -> list[messages.BaseMessage]:
history: list[messages.BaseMessage] = []
content = get_anthropic_thinking_from_assistant_message(message)
if message.content:
content.append({"type": "text", "text": message.content})
# Filter out tool calls without a tool response, so the completion doesn't fail.
tool_calls = [tool for tool in (message.model_dump()["tool_calls"] or []) if tool["id"] in tool_result_map]
if content or tool_calls:
history.append(
messages.AIMessage(
content=cast(list[str | dict[str, Any]], content),
tool_calls=tool_calls,
)
)
# Append associated tool call messages.
for tool_call in tool_calls:
tool_call_id = tool_call["id"]
result_message = tool_result_map.get(tool_call_id)
if result_message is None:
continue
history.append(
messages.HumanMessage(
content=[{"type": "tool_result", "tool_use_id": tool_call_id, "content": result_message.content}],
),
)
# Provenance note goes as a follow-up HumanMessage rather than prefilling the AIMessage
# — prefilling assistant content isn't supported in Sonnet 4.6+.
provenance_note = _get_provenance_note(message)
if provenance_note:
history.append(messages.HumanMessage(content=[{"type": "text", "text": provenance_note}]))
return history
def convert_failure_message_to_anthropic_message(message: FailureMessage) -> messages.HumanMessage:
return messages.HumanMessage(
content=[{"type": "text", "text": message.content or "An unknown failure occurred."}],
)
def convert_to_anthropic_message(
message: AssistantMessageUnion, tool_result_map: Mapping[str, AssistantToolCallMessage]
) -> list[messages.BaseMessage]:
if isinstance(message, HumanMessage):
return [convert_human_message_to_anthropic_message(message)]
if isinstance(message, ContextMessage):
return [convert_context_message_to_anthropic_message(message)]
elif isinstance(message, AssistantMessage):
return convert_assistant_message_to_anthropic_message(message, tool_result_map)
elif isinstance(message, FailureMessage):
return [convert_failure_message_to_anthropic_message(message)]
raise ValueError(f"Unknown message type: {type(message)}")
def convert_to_anthropic_messages(
conversation: Sequence[AssistantMessageUnion],
tool_result_map: Mapping[str, AssistantToolCallMessage],
) -> list[messages.BaseMessage]:
history: list[messages.BaseMessage] = []
for message in conversation:
try:
history.extend(convert_to_anthropic_message(message, tool_result_map))
except ValueError:
continue
return drop_trailing_assistant_messages(history)
def drop_trailing_assistant_messages(history: list[messages.BaseMessage]) -> list[messages.BaseMessage]:
"""Drop assistant messages the conversation ends on, so we never prefill.
Anthropic reads a trailing assistant turn as content to continue from, which Sonnet 4.6 and
later reject outright. A conversation can end on one when a tool appended its own assistant
message after the tool result, which the pre-migration `summarize_sessions` did to render its
"Open report" button.
Assistant messages carrying tool calls are left in place: dropping one would orphan the tool
result that follows it, which Anthropic rejects too.
"""
end = len(history)
while end > 0:
message = history[end - 1]
if not isinstance(message, messages.AIMessage) or message.tool_calls:
break
end -= 1
# A history that is nothing but assistant messages has no valid prefix to send, so leave it
# alone and let the caller's own validation report it.
return history[:end] if end > 0 else history