Skip to content

Commit 3b1874a

Browse files
committed
chore(ph-ai): updates
chore(ph-ai): posthog props
1 parent b7b5fdc commit 3b1874a

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-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: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2128,8 +2128,8 @@ def test_agent_action_and_finish_imports():
21282128
assert call_args["event"] == "$ai_span"
21292129

21302130

2131-
def test_billable_field_in_generation_metadata(mock_client):
2132-
"""Test that the billable field is properly stored in GenerationMetadata."""
2131+
def test_custom_metadata_field_in_generation_metadata(mock_client):
2132+
"""Test that custom_metadata is properly stored in GenerationMetadata."""
21332133
callbacks = CallbackHandler(mock_client)
21342134
run_id = uuid.uuid4()
21352135

@@ -2143,7 +2143,7 @@ def test_billable_field_in_generation_metadata(mock_client):
21432143
metadata={
21442144
"ls_model_name": "gpt-4o",
21452145
"ls_provider": "openai",
2146-
"posthog_ai_billable": True,
2146+
"custom_metadata": {"$ai_billable": True},
21472147
},
21482148
name="test",
21492149
)
@@ -2156,11 +2156,11 @@ def test_billable_field_in_generation_metadata(mock_client):
21562156
provider="openai",
21572157
base_url="https://api.openai.com",
21582158
name="test",
2159-
billable=True,
2159+
custom_metadata={"$ai_billable": True},
21602160
end_time=None,
21612161
)
21622162
assert callbacks._runs[run_id] == expected
2163-
assert callbacks._runs[run_id].billable is True
2163+
assert callbacks._runs[run_id].custom_metadata == {"$ai_billable": True}
21642164

21652165
callbacks._pop_run_metadata(run_id)
21662166

@@ -2175,15 +2175,15 @@ def test_billable_field_in_generation_metadata(mock_client):
21752175
metadata={
21762176
"ls_model_name": "gpt-4o",
21772177
"ls_provider": "openai",
2178-
"posthog_ai_billable": False,
2178+
"custom_metadata": {"$ai_billable": False},
21792179
},
21802180
name="test",
21812181
)
21822182

2183-
assert callbacks._runs[run_id2].billable is False
2183+
assert callbacks._runs[run_id2].custom_metadata == {"$ai_billable": False}
21842184
callbacks._pop_run_metadata(run_id2)
21852185

2186-
# Test default billable=False when not provided
2186+
# Test when custom_metadata not provided
21872187
run_id3 = uuid.uuid4()
21882188
with patch("time.time", return_value=1234567890):
21892189
callbacks._set_llm_metadata(
@@ -2195,7 +2195,7 @@ def test_billable_field_in_generation_metadata(mock_client):
21952195
name="test",
21962196
)
21972197

2198-
assert callbacks._runs[run_id3].billable is False
2198+
assert callbacks._runs[run_id3].custom_metadata is None
21992199

22002200

22012201
def test_billable_property_in_generation_event(mock_client):
@@ -2210,7 +2210,10 @@ def test_billable_property_in_generation_event(mock_client):
22102210
{},
22112211
run_id,
22122212
messages=[{"role": "user", "content": "Test"}],
2213-
metadata={"posthog_ai_billable": True, "ls_model_name": "test-model"},
2213+
metadata={
2214+
"custom_metadata": {"$ai_billable": True},
2215+
"ls_model_name": "test-model",
2216+
},
22142217
invocation_params={},
22152218
)
22162219

@@ -2237,7 +2240,7 @@ def test_billable_property_in_generation_event(mock_client):
22372240

22382241

22392242
def test_billable_defaults_to_false_in_event(mock_client):
2240-
"""Test that $ai_billable defaults to False when not specified."""
2243+
"""Test that $ai_billable is not present when not specified."""
22412244
prompt = ChatPromptTemplate.from_messages([("user", "Test query")])
22422245
model = FakeMessagesListChatModel(
22432246
responses=[AIMessage(content="Test response")],
@@ -2255,7 +2258,7 @@ def test_billable_defaults_to_false_in_event(mock_client):
22552258

22562259
assert generation_call is not None
22572260
props = generation_call[1]["properties"]
2258-
assert props["$ai_billable"] is False
2261+
assert "$ai_billable" not in props
22592262

22602263

22612264
def test_billable_with_real_chain(mock_client):
@@ -2271,12 +2274,12 @@ def test_billable_with_real_chain(mock_client):
22712274
metadata={
22722275
"ls_model_name": "fake-model",
22732276
"ls_provider": "fake",
2274-
"posthog_ai_billable": True,
2277+
"custom_metadata": {"$ai_billable": True},
22752278
},
22762279
invocation_params={"temperature": 0.7},
22772280
)
22782281

2279-
assert callbacks._runs[run_id].billable is True
2282+
assert callbacks._runs[run_id].custom_metadata == {"$ai_billable": True}
22802283

22812284
mock_response = MagicMock()
22822285
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)