Skip to content

Commit 77a64f3

Browse files
andrewm4894claude
andcommitted
Add $ai_lib_metadata to AI integrations
Adds framework identification metadata to all AI events for easier filtering and analytics. Each integration now includes a $ai_lib_metadata property with schema version and framework name. - LangChain: Hardcoded to "langchain" - Native wrappers (Anthropic, OpenAI, Gemini): Uses provider name - Ready for future frameworks (pydantic-ai, crewai, llamaindex) This enables PostHog queries to easily distinguish between: - Direct SDK wrapper usage - Framework-mediated usage (LangChain, etc.) - Different framework types 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent e06830e commit 77a64f3

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

posthog/ai/langchain/callbacks.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +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(),
489490
}
490491
if parent_run_id is not None:
491492
event_properties["$ai_parent_id"] = parent_run_id
@@ -556,6 +557,7 @@ def _capture_generation(
556557
"$ai_http_status": 200,
557558
"$ai_latency": run.latency,
558559
"$ai_base_url": run.base_url,
560+
"$ai_lib_metadata": _get_ai_lib_metadata(),
559561
}
560562

561563
if run.tools:
@@ -865,3 +867,12 @@ def _stringify_exception(exception: BaseException) -> str:
865867
if description:
866868
return f"{exception.__class__.__name__}: {description}"
867869
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ def capture_streaming_event(
483483

484484
# Build base event properties
485485
event_properties = {
486+
"$ai_lib_metadata": {"schema": "v1", "frameworks": [{"name": event_data["provider"]}]},
486487
"$ai_provider": event_data["provider"],
487488
"$ai_model": event_data["model"],
488489
"$ai_model_parameters": get_model_params(event_data["kwargs"]),

0 commit comments

Comments
 (0)