Skip to content

Commit 034df7c

Browse files
andrewm4894claude
andcommitted
Refactor: use unified utility function for $ai_lib_metadata
Creates a single `get_ai_lib_metadata(framework)` utility function to generate the $ai_lib_metadata object, replacing inline implementations across the codebase. Changes: - Add get_ai_lib_metadata() utility to utils.py - Update LangChain callbacks to use utility function - Update call_llm_and_track_usage() to use utility function - Update call_llm_and_track_usage_async() to use utility function - Update capture_streaming_event() to use utility function Benefits: - Consistency across all integrations - Single source of truth for metadata structure - Easier to extend with version detection later 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 6d8368d commit 034df7c

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

posthog/ai/langchain/callbacks.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from pydantic import BaseModel
3737

3838
from posthog import setup
39-
from posthog.ai.utils import get_model_params, with_privacy_mode
39+
from posthog.ai.utils import get_model_params, with_privacy_mode, get_ai_lib_metadata
4040
from posthog.ai.sanitization import sanitize_langchain
4141
from posthog.client import Client
4242

@@ -486,7 +486,7 @@ def _capture_trace_or_span(
486486
"$ai_latency": run.latency,
487487
"$ai_span_name": run.name,
488488
"$ai_span_id": run_id,
489-
"$ai_lib_metadata": _get_ai_lib_metadata(),
489+
"$ai_lib_metadata": get_ai_lib_metadata("langchain"),
490490
}
491491
if parent_run_id is not None:
492492
event_properties["$ai_parent_id"] = parent_run_id
@@ -557,7 +557,7 @@ def _capture_generation(
557557
"$ai_http_status": 200,
558558
"$ai_latency": run.latency,
559559
"$ai_base_url": run.base_url,
560-
"$ai_lib_metadata": _get_ai_lib_metadata(),
560+
"$ai_lib_metadata": get_ai_lib_metadata("langchain"),
561561
}
562562

563563
if run.tools:
@@ -867,12 +867,3 @@ def _stringify_exception(exception: BaseException) -> str:
867867
if description:
868868
return f"{exception.__class__.__name__}: {description}"
869869
return exception.__class__.__name__
870-
871-
872-
def _get_ai_lib_metadata() -> Dict[str, Any]:
873-
"""
874-
Generate the $ai_lib_metadata dict with framework information.
875-
Since this file is langchain/callbacks.py, we know the framework is LangChain.
876-
Returns a dict with schema version and framework name.
877-
"""
878-
return {"schema": "v1", "frameworks": [{"name": "langchain"}]}

posthog/ai/utils.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
)
1313

1414

15+
def get_ai_lib_metadata(framework: str) -> Dict[str, Any]:
16+
"""
17+
Generate AI library metadata object with framework name.
18+
Used to identify which framework/SDK generated the AI event.
19+
"""
20+
return {"schema": "v1", "frameworks": [{"name": framework}]}
21+
22+
1523
def merge_usage_stats(
1624
target: TokenUsage, source: TokenUsage, mode: str = "incremental"
1725
) -> None:
@@ -320,7 +328,7 @@ def call_llm_and_track_usage(
320328
ph_client, posthog_privacy_mode, kwargs.get("instructions")
321329
)
322330

323-
event_properties["$ai_lib_metadata"] = {"schema": "v1", "frameworks": [{"name": provider}]}
331+
event_properties["$ai_lib_metadata"] = get_ai_lib_metadata(provider)
324332

325333
# send the event to posthog
326334
if hasattr(ph_client, "capture") and callable(ph_client.capture):
@@ -425,7 +433,7 @@ async def call_llm_and_track_usage_async(
425433
ph_client, posthog_privacy_mode, kwargs.get("instructions")
426434
)
427435

428-
event_properties["$ai_lib_metadata"] = {"schema": "v1", "frameworks": [{"name": provider}]}
436+
event_properties["$ai_lib_metadata"] = get_ai_lib_metadata(provider)
429437

430438
# send the event to posthog
431439
if hasattr(ph_client, "capture") and callable(ph_client.capture):
@@ -487,7 +495,7 @@ def capture_streaming_event(
487495

488496
# Build base event properties
489497
event_properties = {
490-
"$ai_lib_metadata": {"schema": "v1", "frameworks": [{"name": event_data["provider"]}]},
498+
"$ai_lib_metadata": get_ai_lib_metadata(event_data["provider"]),
491499
"$ai_provider": event_data["provider"],
492500
"$ai_model": event_data["model"],
493501
"$ai_model_parameters": get_model_params(event_data["kwargs"]),

0 commit comments

Comments
 (0)