Skip to content

Commit 99e4f12

Browse files
committed
chore(ph-ai): updates
chore(ph-ai): posthog props chore: fix tests
1 parent b7b5fdc commit 99e4f12

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 6.9.2 - 2025-11-10
2+
3+
- feat(ph-ai): PostHog properties dict in GenerationMetadata
4+
15
# 6.9.1 - 2025-11-07
26

37
- fix(error-tracking): pass code variables config from init to client

posthog/ai/langchain/callbacks.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ class GenerationMetadata(SpanMetadata):
7979
"""Base URL of the provider's API used in the run."""
8080
tools: Optional[List[Dict[str, Any]]] = None
8181
"""Tools provided to the model."""
82-
custom_metadata: Optional[Dict[str, Any]] = None
83-
"""Custom metadata of the run."""
82+
posthog_properties: Optional[Dict[str, Any]] = None
83+
"""PostHog properties of the run."""
8484

8585

8686
RunMetadata = Union[SpanMetadata, GenerationMetadata]
@@ -423,7 +423,7 @@ def _set_llm_metadata(
423423
if provider := metadata.get("ls_provider"):
424424
generation.provider = provider
425425

426-
generation.custom_metadata = metadata.get("custom_metadata")
426+
generation.posthog_properties = metadata.get("posthog_properties")
427427
try:
428428
base_url = serialized["kwargs"]["openai_api_base"]
429429
if base_url is not None:
@@ -570,8 +570,8 @@ def _capture_generation(
570570
"$ai_framework": "langchain",
571571
}
572572

573-
if run.custom_metadata:
574-
event_properties.update(run.custom_metadata)
573+
if run.posthog_properties:
574+
event_properties.update(run.posthog_properties)
575575

576576
if run.tools:
577577
event_properties["$ai_tools"] = run.tools

posthog/test/ai/langchain/test_callbacks.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ def test_metadata_capture(mock_client):
113113
base_url="https://us.posthog.com",
114114
name="test",
115115
end_time=None,
116+
posthog_properties=None,
116117
)
117118
assert callbacks._runs[run_id] == expected
118119
with patch("time.time", return_value=1234567891):
@@ -1269,6 +1270,7 @@ def test_metadata_tools(mock_client):
12691270
name="test",
12701271
tools=tools,
12711272
end_time=None,
1273+
posthog_properties=None,
12721274
)
12731275
assert callbacks._runs[run_id] == expected
12741276
with patch("time.time", return_value=1234567891):
@@ -2128,8 +2130,8 @@ def test_agent_action_and_finish_imports():
21282130
assert call_args["event"] == "$ai_span"
21292131

21302132

2131-
def test_billable_field_in_generation_metadata(mock_client):
2132-
"""Test that the billable field is properly stored in GenerationMetadata."""
2133+
def test_custom_metadata_field_in_generation_metadata(mock_client):
2134+
"""Test that posthog_properties is properly stored in GenerationMetadata."""
21332135
callbacks = CallbackHandler(mock_client)
21342136
run_id = uuid.uuid4()
21352137

@@ -2143,7 +2145,7 @@ def test_billable_field_in_generation_metadata(mock_client):
21432145
metadata={
21442146
"ls_model_name": "gpt-4o",
21452147
"ls_provider": "openai",
2146-
"posthog_ai_billable": True,
2148+
"posthog_properties": {"$ai_billable": True},
21472149
},
21482150
name="test",
21492151
)
@@ -2156,11 +2158,11 @@ def test_billable_field_in_generation_metadata(mock_client):
21562158
provider="openai",
21572159
base_url="https://api.openai.com",
21582160
name="test",
2159-
billable=True,
2161+
posthog_properties={"$ai_billable": True},
21602162
end_time=None,
21612163
)
21622164
assert callbacks._runs[run_id] == expected
2163-
assert callbacks._runs[run_id].billable is True
2165+
assert callbacks._runs[run_id].posthog_properties == {"$ai_billable": True}
21642166

21652167
callbacks._pop_run_metadata(run_id)
21662168

@@ -2175,15 +2177,15 @@ def test_billable_field_in_generation_metadata(mock_client):
21752177
metadata={
21762178
"ls_model_name": "gpt-4o",
21772179
"ls_provider": "openai",
2178-
"posthog_ai_billable": False,
2180+
"posthog_properties": {"$ai_billable": False},
21792181
},
21802182
name="test",
21812183
)
21822184

2183-
assert callbacks._runs[run_id2].billable is False
2185+
assert callbacks._runs[run_id2].posthog_properties == {"$ai_billable": False}
21842186
callbacks._pop_run_metadata(run_id2)
21852187

2186-
# Test default billable=False when not provided
2188+
# Test when posthog_properties not provided
21872189
run_id3 = uuid.uuid4()
21882190
with patch("time.time", return_value=1234567890):
21892191
callbacks._set_llm_metadata(
@@ -2195,7 +2197,7 @@ def test_billable_field_in_generation_metadata(mock_client):
21952197
name="test",
21962198
)
21972199

2198-
assert callbacks._runs[run_id3].billable is False
2200+
assert callbacks._runs[run_id3].posthog_properties is None
21992201

22002202

22012203
def test_billable_property_in_generation_event(mock_client):
@@ -2210,7 +2212,10 @@ def test_billable_property_in_generation_event(mock_client):
22102212
{},
22112213
run_id,
22122214
messages=[{"role": "user", "content": "Test"}],
2213-
metadata={"posthog_ai_billable": True, "ls_model_name": "test-model"},
2215+
metadata={
2216+
"posthog_properties": {"$ai_billable": True},
2217+
"ls_model_name": "test-model",
2218+
},
22142219
invocation_params={},
22152220
)
22162221

@@ -2237,7 +2242,7 @@ def test_billable_property_in_generation_event(mock_client):
22372242

22382243

22392244
def test_billable_defaults_to_false_in_event(mock_client):
2240-
"""Test that $ai_billable defaults to False when not specified."""
2245+
"""Test that $ai_billable is not present when not specified."""
22412246
prompt = ChatPromptTemplate.from_messages([("user", "Test query")])
22422247
model = FakeMessagesListChatModel(
22432248
responses=[AIMessage(content="Test response")],
@@ -2255,7 +2260,7 @@ def test_billable_defaults_to_false_in_event(mock_client):
22552260

22562261
assert generation_call is not None
22572262
props = generation_call[1]["properties"]
2258-
assert props["$ai_billable"] is False
2263+
assert "$ai_billable" not in props
22592264

22602265

22612266
def test_billable_with_real_chain(mock_client):
@@ -2271,12 +2276,12 @@ def test_billable_with_real_chain(mock_client):
22712276
metadata={
22722277
"ls_model_name": "fake-model",
22732278
"ls_provider": "fake",
2274-
"posthog_ai_billable": True,
2279+
"posthog_properties": {"$ai_billable": True},
22752280
},
22762281
invocation_params={"temperature": 0.7},
22772282
)
22782283

2279-
assert callbacks._runs[run_id].billable is True
2284+
assert callbacks._runs[run_id].posthog_properties == {"$ai_billable": True}
22802285

22812286
mock_response = MagicMock()
22822287
mock_response.generations = [[MagicMock()]]

posthog/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = "6.9.1"
1+
VERSION = "6.9.2"
22

33
if __name__ == "__main__":
44
print(VERSION, end="") # noqa: T201

0 commit comments

Comments
 (0)