Skip to content

Commit af8a5d4

Browse files
authored
Merge branch 'master' into feat/anthropic
2 parents cea91e8 + 9b42349 commit af8a5d4

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

posthog/ai/langchain/callbacks.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,8 @@ def _convert_message_to_dict(message: BaseMessage) -> Dict[str, Any]:
333333
else:
334334
message_dict = {"role": message.type, "content": str(message.content)}
335335

336-
if "name" in message.additional_kwargs:
337-
message_dict["name"] = message.additional_kwargs["name"]
338336
if message.additional_kwargs:
339-
message_dict["additional_kwargs"] = message.additional_kwargs
337+
message_dict.update(message.additional_kwargs)
340338

341339
return message_dict
342340

posthog/test/ai/langchain/test_callbacks.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,3 +731,44 @@ async def test_async_anthropic_streaming(mock_client):
731731
assert first_call_props["$ai_http_status"] == 200
732732
assert first_call_props["$ai_input_tokens"] == 17
733733
assert first_call_props["$ai_output_tokens"] is not None
734+
assert call["properties"]["$ai_output"] is None
735+
736+
737+
def test_tool_calls(mock_client):
738+
prompt = ChatPromptTemplate.from_messages([("user", "Foo")])
739+
model = FakeMessagesListChatModel(
740+
responses=[
741+
AIMessage(
742+
content="Bar",
743+
additional_kwargs={
744+
"tool_calls": [
745+
{
746+
"type": "function",
747+
"id": "123",
748+
"function": {
749+
"name": "test",
750+
"args": '{"a": 1}',
751+
},
752+
}
753+
]
754+
},
755+
)
756+
]
757+
)
758+
chain = prompt | model
759+
callbacks = CallbackHandler(mock_client)
760+
chain.invoke({}, config={"callbacks": [callbacks]})
761+
762+
assert mock_client.capture.call_count == 1
763+
call = mock_client.capture.call_args[1]
764+
assert call["properties"]["$ai_output"]["choices"][0]["tool_calls"] == [
765+
{
766+
"type": "function",
767+
"id": "123",
768+
"function": {
769+
"name": "test",
770+
"args": '{"a": 1}',
771+
},
772+
}
773+
]
774+
assert "additional_kwargs" not in call["properties"]["$ai_output"]["choices"][0]

0 commit comments

Comments
 (0)