Skip to content

Commit ea07996

Browse files
committed
feat(llmo): Use default PH client for langchain
1 parent 7a8b091 commit ea07996

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

posthog/ai/langchain/callbacks.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from langchain_core.outputs import ChatGeneration, LLMResult
3434
from pydantic import BaseModel
3535

36-
from posthog import default_client
36+
from posthog import setup
3737
from posthog.ai.utils import get_model_params, with_privacy_mode
3838
from posthog.client import Client
3939

@@ -81,7 +81,7 @@ class CallbackHandler(BaseCallbackHandler):
8181
The PostHog LLM observability callback handler for LangChain.
8282
"""
8383

84-
_client: Client
84+
_ph_client: Client
8585
"""PostHog client instance."""
8686

8787
_distinct_id: Optional[Union[str, int, UUID]]
@@ -127,10 +127,7 @@ def __init__(
127127
privacy_mode: Whether to redact the input and output of the trace.
128128
groups: Optional additional PostHog groups to use for the trace.
129129
"""
130-
posthog_client = client or default_client
131-
if posthog_client is None:
132-
raise ValueError("PostHog client is required")
133-
self._client = posthog_client
130+
self._ph_client = client or setup()
134131
self._distinct_id = distinct_id
135132
self._trace_id = trace_id
136133
self._properties = properties or {}
@@ -481,7 +478,7 @@ def _capture_trace_or_span(
481478
event_properties = {
482479
"$ai_trace_id": trace_id,
483480
"$ai_input_state": with_privacy_mode(
484-
self._client, self._privacy_mode, run.input
481+
self._ph_client, self._privacy_mode, run.input
485482
),
486483
"$ai_latency": run.latency,
487484
"$ai_span_name": run.name,
@@ -497,13 +494,13 @@ def _capture_trace_or_span(
497494
event_properties["$ai_is_error"] = True
498495
elif outputs is not None:
499496
event_properties["$ai_output_state"] = with_privacy_mode(
500-
self._client, self._privacy_mode, outputs
497+
self._ph_client, self._privacy_mode, outputs
501498
)
502499

503500
if self._distinct_id is None:
504501
event_properties["$process_person_profile"] = False
505502

506-
self._client.capture(
503+
self._ph_client.capture(
507504
distinct_id=self._distinct_id or run_id,
508505
event=event_name,
509506
properties=event_properties,
@@ -550,14 +547,14 @@ def _capture_generation(
550547
"$ai_provider": run.provider,
551548
"$ai_model": run.model,
552549
"$ai_model_parameters": run.model_params,
553-
"$ai_input": with_privacy_mode(self._client, self._privacy_mode, run.input),
550+
"$ai_input": with_privacy_mode(self._ph_client, self._privacy_mode, run.input),
554551
"$ai_http_status": 200,
555552
"$ai_latency": run.latency,
556553
"$ai_base_url": run.base_url,
557554
}
558555
if run.tools:
559556
event_properties["$ai_tools"] = with_privacy_mode(
560-
self._client,
557+
self._ph_client,
561558
self._privacy_mode,
562559
run.tools,
563560
)
@@ -589,7 +586,7 @@ def _capture_generation(
589586
_extract_raw_esponse(generation) for generation in generation_result
590587
]
591588
event_properties["$ai_output_choices"] = with_privacy_mode(
592-
self._client, self._privacy_mode, completions
589+
self._ph_client, self._privacy_mode, completions
593590
)
594591

595592
if self._properties:
@@ -598,7 +595,7 @@ def _capture_generation(
598595
if self._distinct_id is None:
599596
event_properties["$process_person_profile"] = False
600597

601-
self._client.capture(
598+
self._ph_client.capture(
602599
distinct_id=self._distinct_id or trace_id,
603600
event="$ai_generation",
604601
properties=event_properties,

0 commit comments

Comments
 (0)