Skip to content

Commit 2e3a7f2

Browse files
committed
fix: add http test
1 parent 78e9d14 commit 2e3a7f2

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

posthog/test/ai/langchain/test_callbacks.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,9 @@ async def sleep(x): # -> Any:
10331033

10341034
def test_error(mock_client):
10351035
prompt = ChatPromptTemplate.from_messages([("user", "Foo")])
1036-
chain = prompt | FakeMessagesListChatModel(responses=[AIMessage(content="Bar")])
1036+
# Configure the model to raise an exception by passing an error message
1037+
model = FakeMessagesListChatModel(responses=[AIMessage(content="Bar")], error="Test error")
1038+
chain = prompt | model
10371039
callbacks = CallbackHandler(mock_client)
10381040
with pytest.raises(Exception):
10391041
chain.invoke({}, config={"callbacks": [callbacks]})
@@ -1043,3 +1045,18 @@ def test_error(mock_client):
10431045
props = call_args["properties"]
10441046
assert props["$ai_is_error"] is True
10451047
assert props["$ai_error"] == "Test error"
1048+
1049+
def test_http_error(mock_client):
1050+
prompt = ChatPromptTemplate.from_messages([("user", "Foo")])
1051+
model = FakeMessagesListChatModel(responses=[AIMessage(content="Bar")], http_status=400)
1052+
chain = prompt | model
1053+
callbacks = CallbackHandler(mock_client)
1054+
with pytest.raises(Exception):
1055+
chain.invoke({}, config={"callbacks": [callbacks]})
1056+
1057+
assert mock_client.capture.call_count == 1
1058+
call_args = mock_client.capture.call_args[1]
1059+
props = call_args["properties"]
1060+
assert props["$ai_is_error"] is True
1061+
assert props["$ai_error"] is not None
1062+
assert props["$ai_http_status"] == 400

0 commit comments

Comments
 (0)