@posthog/ai 8.6.1 → 8.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -215,7 +215,7 @@ function sanitizeValues(obj) {
215
215
  return jsonSafe;
216
216
  }
217
217
 
218
- var version = "8.6.1";
218
+ var version = "8.6.3";
219
219
 
220
220
  const DEFAULT_MAX_DEPTH = 3;
221
221
  const MAX_STACK_LINES = 20;
@@ -529,6 +529,13 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
529
529
  }
530
530
  return parentRunId;
531
531
  }
532
+ _safeCapture(message) {
533
+ try {
534
+ this.client.capture(message);
535
+ } catch {
536
+ // Telemetry delivery must never affect the LangChain callback lifecycle.
537
+ }
538
+ }
532
539
  _popRunAndCaptureTraceOrSpan(runId, parentRunId, outputs) {
533
540
  const traceId = this._getTraceId(runId);
534
541
  this._popParentOfRun(runId);
@@ -581,7 +588,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
581
588
  } else if (outputs !== undefined) {
582
589
  eventProperties['$ai_output_state'] = withPrivacyMode(this.client, this.privacyMode, sanitizeLangChain(outputs));
583
590
  }
584
- this.client.capture({
591
+ this._safeCapture({
585
592
  distinctId: this.distinctId ? this.distinctId.toString() : runId,
586
593
  event: eventName,
587
594
  properties: eventProperties,
@@ -679,7 +686,7 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
679
686
  if (!this.distinctId) {
680
687
  eventProperties['$process_person_profile'] = false;
681
688
  }
682
- this.client.capture({
689
+ this._safeCapture({
683
690
  distinctId: this.distinctId ? this.distinctId.toString() : traceId,
684
691
  event: '$ai_generation',
685
692
  properties: eventProperties,
@@ -802,27 +809,10 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
802
809
  return undefined;
803
810
  }
804
811
  const gen = lastGeneration[0];
805
-
806
- // Check generationInfo for finish_reason (OpenAI format)
807
- if (gen.generationInfo?.finish_reason) {
808
- return String(gen.generationInfo.finish_reason);
809
- }
810
-
811
- // Check generationInfo for response_metadata.stop_reason (Anthropic format)
812
- if (gen.generationInfo?.response_metadata?.stop_reason) {
813
- return String(gen.generationInfo.response_metadata.stop_reason);
814
- }
815
-
816
- // Check message response_metadata for finish_reason (common LangChain format)
817
- if (gen.generationInfo?.response_metadata?.finish_reason) {
818
- return String(gen.generationInfo.response_metadata.finish_reason);
819
- }
820
-
821
- // Check for stop_reason directly in generationInfo
822
- if (gen.generationInfo?.stop_reason) {
823
- return String(gen.generationInfo.stop_reason);
824
- }
825
- return undefined;
812
+ const messageResponseMetadata = gen.message?.response_metadata;
813
+ const generationResponseMetadata = gen.generationInfo?.response_metadata;
814
+ const stopReason = messageResponseMetadata?.finish_reason || messageResponseMetadata?.stop_reason || gen.generationInfo?.finish_reason || generationResponseMetadata?.stop_reason || generationResponseMetadata?.finish_reason || gen.generationInfo?.stop_reason;
815
+ return stopReason != null ? String(stopReason) : undefined;
826
816
  }
827
817
  _parseUsageModel(usage, provider, model) {
828
818
  const conversionList = [['promptTokens', 'input'], ['completionTokens', 'output'], ['input_tokens', 'input'], ['output_tokens', 'output'], ['prompt_token_count', 'input'], ['candidates_token_count', 'output'], ['inputTokenCount', 'input'], ['outputTokenCount', 'output'], ['input_token_count', 'input'], ['generated_token_count', 'output']];
@@ -940,14 +930,11 @@ class LangChainCallbackHandler extends BaseCallbackHandler {
940
930
  if (llmUsage[0] === 0 && llmUsage[1] === 0 && response.generations) {
941
931
  for (const generation of response.generations) {
942
932
  for (const genChunk of generation) {
943
- // Check other paths for usage information
944
- if (genChunk.generationInfo?.usage_metadata) {
945
- llmUsage = this._parseUsageModel(genChunk.generationInfo.usage_metadata, provider, model);
946
- return llmUsage;
947
- }
948
- const messageChunk = genChunk.generationInfo ?? {};
949
- const responseMetadata = messageChunk.response_metadata ?? {};
950
- const chunkUsage = responseMetadata['usage'] ?? responseMetadata['amazon-bedrock-invocationMetrics'] ?? messageChunk.usage_metadata;
933
+ const message = genChunk.message ?? {};
934
+ const messageResponseMetadata = message.response_metadata ?? {};
935
+ const generationInfo = genChunk.generationInfo ?? {};
936
+ const generationResponseMetadata = generationInfo.response_metadata ?? {};
937
+ const chunkUsage = message.usage_metadata ?? messageResponseMetadata['usage'] ?? messageResponseMetadata['amazon-bedrock-invocationMetrics'] ?? generationInfo.usage_metadata ?? generationResponseMetadata['usage'] ?? generationResponseMetadata['amazon-bedrock-invocationMetrics'];
951
938
  if (chunkUsage) {
952
939
  llmUsage = this._parseUsageModel(chunkUsage, provider, model);
953
940
  return llmUsage;