Skip to content

Commit cc36924

Browse files
committed
feat: Test the CallbackHandler without any PH client
1 parent 12624cf commit cc36924

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

posthog/test/ai/langchain/test_callbacks.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,3 +1727,29 @@ def test_openai_reasoning_tokens(mock_client):
17271727
assert call["properties"]["$ai_reasoning_tokens"] is not None
17281728
assert call["properties"]["$ai_input_tokens"] is not None
17291729
assert call["properties"]["$ai_output_tokens"] is not None
1730+
1731+
1732+
def test_callback_handler_without_client():
1733+
"""Test that CallbackHandler works properly when no PostHog client is passed."""
1734+
with patch("posthog.ai.langchain.callbacks.setup") as mock_setup:
1735+
mock_client = mock_setup.return_value
1736+
1737+
callbacks = CallbackHandler()
1738+
1739+
# Verify that setup() was called
1740+
mock_setup.assert_called_once()
1741+
1742+
# Verify that the client was set to the result of setup()
1743+
assert callbacks._ph_client == mock_client
1744+
1745+
# Test that the callback handler works with a simple chain
1746+
prompt = ChatPromptTemplate.from_messages([("user", "Foo")])
1747+
model = FakeMessagesListChatModel(responses=[AIMessage(content="Bar")])
1748+
chain = prompt | model
1749+
1750+
# This should work and call the mock client
1751+
result = chain.invoke({}, config={"callbacks": [callbacks]})
1752+
assert result.content == "Bar"
1753+
1754+
# Verify that the mock client was used for capturing events
1755+
assert mock_client.capture.call_count == 3

0 commit comments

Comments
 (0)