@posthog/ai 7.16.14 → 7.17.0

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.
@@ -2,8 +2,6 @@ import { OpenAI } from 'openai';
2
2
  import { v4 } from 'uuid';
3
3
  import { uuidv7 } from '@posthog/core';
4
4
 
5
- var version = "7.16.14";
6
-
7
5
  // Type guards for safer type checking
8
6
 
9
7
  const isString = value => {
@@ -476,73 +474,114 @@ function addDefaults(params) {
476
474
  traceId: params.traceId ?? v4()
477
475
  };
478
476
  }
479
- const sendEventWithErrorToPosthog = async ({
480
- client,
481
- traceId,
482
- error,
483
- ...args
484
- }) => {
485
- const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
486
- const properties = {
487
- client,
488
- traceId,
489
- httpStatus,
490
- error: JSON.stringify(error),
491
- ...args
492
- };
493
- const enrichedError = error;
494
- if (client.options?.enableExceptionAutocapture) {
495
- // assign a uuid that can be used to link the trace and exception events
496
- const exceptionId = uuidv7();
497
- client.captureException(error, undefined, {
498
- $ai_trace_id: traceId
499
- }, exceptionId);
500
- enrichedError.__posthog_previously_captured_error = true;
501
- properties.exceptionId = exceptionId;
477
+ function formatOpenAIResponsesInput(input, instructions) {
478
+ const messages = [];
479
+ if (instructions) {
480
+ messages.push({
481
+ role: 'system',
482
+ content: instructions
483
+ });
502
484
  }
503
- await sendEventToPosthog(properties);
504
- return enrichedError;
505
- };
506
- const sendEventToPosthog = async ({
507
- client,
508
- eventType = AIEvent.Generation,
509
- distinctId,
510
- traceId,
511
- model,
512
- provider,
513
- input,
514
- output,
515
- latency,
516
- timeToFirstToken,
517
- baseURL,
518
- params,
519
- httpStatus = 200,
520
- usage = {},
521
- error,
522
- exceptionId,
523
- stopReason,
524
- tools,
525
- captureImmediate = false
526
- }) => {
485
+ if (Array.isArray(input)) {
486
+ for (const item of input) {
487
+ if (typeof item === 'string') {
488
+ messages.push({
489
+ role: 'user',
490
+ content: item
491
+ });
492
+ } else if (item && typeof item === 'object') {
493
+ const obj = item;
494
+ const role = isString(obj.role) ? obj.role : 'user';
495
+
496
+ // Handle content properly - preserve structure for objects/arrays
497
+ const content = obj.content ?? obj.text ?? item;
498
+ messages.push({
499
+ role,
500
+ content: toContentString(content)
501
+ });
502
+ } else {
503
+ messages.push({
504
+ role: 'user',
505
+ content: toContentString(item)
506
+ });
507
+ }
508
+ }
509
+ } else if (typeof input === 'string') {
510
+ messages.push({
511
+ role: 'user',
512
+ content: input
513
+ });
514
+ } else if (input) {
515
+ messages.push({
516
+ role: 'user',
517
+ content: toContentString(input)
518
+ });
519
+ }
520
+ return messages;
521
+ }
522
+
523
+ var version = "7.17.0";
524
+
525
+ /**
526
+ * Options for `captureAiGeneration`. Mirrors the `$ai_generation` event shape
527
+ * directly so that any caller — first-party SDK wrappers and external code
528
+ * alike — produces an identical event.
529
+ */
530
+
531
+ /**
532
+ * Capture an `$ai_generation` (or `$ai_embedding`) event to PostHog.
533
+ *
534
+ * This is the canonical primitive that every `@posthog/ai` wrapper
535
+ * (`withTracing`, `OpenAI`, `Anthropic`, `GoogleGenAI`, …) funnels through, so
536
+ * external code can use it directly to instrument LLM calls made through
537
+ * arbitrary clients (Cloudflare Workers AI, custom HTTP, etc.) and get the
538
+ * same events the SDK wrappers produce.
539
+ *
540
+ * When `error` is set, the event is captured as an error. If the error is an
541
+ * object, it is mutated in place to set `__posthog_previously_captured_error`
542
+ * so callers can re-throw the original error reference safely.
543
+ */
544
+ const captureAiGeneration = async (client, options) => {
527
545
  if (!client.capture) {
528
- return Promise.resolve();
546
+ return;
529
547
  }
530
- // sanitize input and output for UTF-8 validity
531
- const safeInput = sanitizeValues(input);
532
- const safeOutput = sanitizeValues(output);
533
- const safeError = sanitizeValues(error);
548
+ const traceId = options.traceId ?? v4();
549
+ const eventType = options.eventType ?? AIEvent.Generation;
550
+ const privacyMode = options.privacyMode ?? false;
551
+ const usage = options.usage ?? {};
552
+ const safeInput = sanitizeValues(options.input);
553
+ const safeOutput = sanitizeValues(options.output);
554
+ let httpStatus = options.httpStatus;
534
555
  let errorData = {};
535
- if (error) {
556
+ if (options.error) {
557
+ if (httpStatus === undefined) {
558
+ if (typeof options.error === 'object' && 'status' in options.error && typeof options.error.status === 'number') {
559
+ httpStatus = options.error.status;
560
+ } else {
561
+ httpStatus = 500;
562
+ }
563
+ }
564
+ let exceptionId;
565
+ if (client.options?.enableExceptionAutocapture) {
566
+ exceptionId = uuidv7();
567
+ client.captureException(options.error, undefined, {
568
+ $ai_trace_id: traceId
569
+ }, exceptionId);
570
+ if (typeof options.error === 'object') {
571
+ options.error.__posthog_previously_captured_error = true;
572
+ }
573
+ }
536
574
  errorData = {
537
575
  $ai_is_error: true,
538
- $ai_error: safeError,
576
+ $ai_error: sanitizeValues(JSON.stringify(options.error)),
539
577
  $exception_event_id: exceptionId
540
578
  };
541
579
  }
580
+ httpStatus = httpStatus ?? 200;
542
581
  let costOverrideData = {};
543
- if (params.posthogCostOverride) {
544
- const inputCostUSD = (params.posthogCostOverride.inputCost ?? 0) * (usage.inputTokens ?? 0);
545
- const outputCostUSD = (params.posthogCostOverride.outputCost ?? 0) * (usage.outputTokens ?? 0);
582
+ if (options.costOverride) {
583
+ const inputCostUSD = (options.costOverride.inputCost ?? 0) * (usage.inputTokens ?? 0);
584
+ const outputCostUSD = (options.costOverride.outputCost ?? 0) * (usage.outputTokens ?? 0);
546
585
  costOverrideData = {
547
586
  $ai_input_cost_usd: inputCostUSD,
548
587
  $ai_output_cost_usd: outputCostUSD,
@@ -569,96 +608,49 @@ const sendEventToPosthog = async ({
569
608
  const properties = {
570
609
  $ai_lib: 'posthog-ai',
571
610
  $ai_lib_version: version,
572
- $ai_provider: params.posthogProviderOverride ?? provider,
573
- $ai_model: params.posthogModelOverride ?? model,
574
- $ai_model_parameters: getModelParams(params),
575
- $ai_input: withPrivacyMode(client, params.posthogPrivacyMode ?? false, safeInput),
576
- $ai_output_choices: withPrivacyMode(client, params.posthogPrivacyMode ?? false, safeOutput),
611
+ $ai_provider: options.providerOverride ?? options.provider,
612
+ $ai_model: options.modelOverride ?? options.model,
613
+ $ai_model_parameters: options.modelParameters ?? {},
614
+ $ai_input: withPrivacyMode(client, privacyMode, safeInput),
615
+ $ai_output_choices: withPrivacyMode(client, privacyMode, safeOutput),
577
616
  $ai_http_status: httpStatus,
578
617
  $ai_input_tokens: usage.inputTokens ?? 0,
579
618
  ...(usage.outputTokens !== undefined ? {
580
619
  $ai_output_tokens: usage.outputTokens
581
620
  } : {}),
582
621
  ...additionalTokenValues,
583
- $ai_latency: latency,
584
- ...(timeToFirstToken !== undefined ? {
585
- $ai_time_to_first_token: timeToFirstToken
622
+ $ai_latency: options.latency ?? 0,
623
+ ...(options.timeToFirstToken !== undefined ? {
624
+ $ai_time_to_first_token: options.timeToFirstToken
586
625
  } : {}),
587
626
  $ai_trace_id: traceId,
588
- $ai_base_url: baseURL,
589
- ...params.posthogProperties,
590
- $ai_tokens_source: getTokensSource(params.posthogProperties),
591
- ...(distinctId ? {} : {
627
+ $ai_base_url: options.baseURL ?? '',
628
+ ...options.properties,
629
+ $ai_tokens_source: getTokensSource(options.properties),
630
+ ...(options.distinctId ? {} : {
592
631
  $process_person_profile: false
593
632
  }),
594
- ...(stopReason ? {
595
- $ai_stop_reason: stopReason
633
+ ...(options.stopReason ? {
634
+ $ai_stop_reason: options.stopReason
596
635
  } : {}),
597
- ...(tools ? {
598
- $ai_tools: tools
636
+ ...(options.tools ? {
637
+ $ai_tools: options.tools
599
638
  } : {}),
600
639
  ...errorData,
601
640
  ...costOverrideData
602
641
  };
603
642
  const event = {
604
- distinctId: distinctId ?? traceId,
643
+ distinctId: options.distinctId ?? traceId,
605
644
  event: eventType,
606
645
  properties,
607
- groups: params.posthogGroups
646
+ groups: options.groups
608
647
  };
609
- if (captureImmediate) {
610
- // await capture promise to send single event in serverless environments
648
+ if (options.captureImmediate) {
611
649
  await client.captureImmediate(event);
612
650
  } else {
613
651
  client.capture(event);
614
652
  }
615
- return Promise.resolve();
616
653
  };
617
- function formatOpenAIResponsesInput(input, instructions) {
618
- const messages = [];
619
- if (instructions) {
620
- messages.push({
621
- role: 'system',
622
- content: instructions
623
- });
624
- }
625
- if (Array.isArray(input)) {
626
- for (const item of input) {
627
- if (typeof item === 'string') {
628
- messages.push({
629
- role: 'user',
630
- content: item
631
- });
632
- } else if (item && typeof item === 'object') {
633
- const obj = item;
634
- const role = isString(obj.role) ? obj.role : 'user';
635
-
636
- // Handle content properly - preserve structure for objects/arrays
637
- const content = obj.content ?? obj.text ?? item;
638
- messages.push({
639
- role,
640
- content: toContentString(content)
641
- });
642
- } else {
643
- messages.push({
644
- role: 'user',
645
- content: toContentString(item)
646
- });
647
- }
648
- }
649
- } else if (typeof input === 'string') {
650
- messages.push({
651
- role: 'user',
652
- content: input
653
- });
654
- } else if (input) {
655
- messages.push({
656
- role: 'user',
657
- content: toContentString(input)
658
- });
659
- }
660
- return messages;
661
- }
662
654
 
663
655
  /**
664
656
  * Checks if a ResponseStreamEvent chunk represents the first token/content from the model.
@@ -841,8 +833,7 @@ class WrappedCompletions extends Completions {
841
833
  const latency = (Date.now() - startTime) / 1000;
842
834
  const timeToFirstToken = firstTokenTime !== undefined ? (firstTokenTime - startTime) / 1000 : undefined;
843
835
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
844
- await sendEventToPosthog({
845
- client: this.phClient,
836
+ await captureAiGeneration(this.phClient, {
846
837
  ...posthogParams,
847
838
  model: openAIParams.model ?? modelFromResponse,
848
839
  provider: 'openai',
@@ -851,7 +842,7 @@ class WrappedCompletions extends Completions {
851
842
  latency,
852
843
  timeToFirstToken,
853
844
  baseURL: this.baseURL,
854
- params: body,
845
+ modelParameters: getModelParams(body),
855
846
  httpStatus: 200,
856
847
  usage: {
857
848
  inputTokens: usage.inputTokens,
@@ -865,8 +856,7 @@ class WrappedCompletions extends Completions {
865
856
  tools: availableTools
866
857
  });
867
858
  } catch (error) {
868
- const enrichedError = await sendEventWithErrorToPosthog({
869
- client: this.phClient,
859
+ await captureAiGeneration(this.phClient, {
870
860
  ...posthogParams,
871
861
  model: openAIParams.model,
872
862
  provider: 'openai',
@@ -874,14 +864,14 @@ class WrappedCompletions extends Completions {
874
864
  output: [],
875
865
  latency: 0,
876
866
  baseURL: this.baseURL,
877
- params: body,
867
+ modelParameters: getModelParams(body),
878
868
  usage: {
879
869
  inputTokens: 0,
880
870
  outputTokens: 0
881
871
  },
882
872
  error
883
873
  });
884
- throw enrichedError;
874
+ throw error;
885
875
  }
886
876
  })();
887
877
 
@@ -896,8 +886,7 @@ class WrappedCompletions extends Completions {
896
886
  const latency = (Date.now() - startTime) / 1000;
897
887
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
898
888
  const formattedOutput = formatResponseOpenAI(result);
899
- await sendEventToPosthog({
900
- client: this.phClient,
889
+ await captureAiGeneration(this.phClient, {
901
890
  ...posthogParams,
902
891
  model: openAIParams.model ?? result.model,
903
892
  provider: 'openai',
@@ -905,7 +894,7 @@ class WrappedCompletions extends Completions {
905
894
  output: formattedOutput,
906
895
  latency,
907
896
  baseURL: this.baseURL,
908
- params: body,
897
+ modelParameters: getModelParams(body),
909
898
  httpStatus: 200,
910
899
  usage: {
911
900
  inputTokens: result.usage?.prompt_tokens ?? 0,
@@ -922,8 +911,7 @@ class WrappedCompletions extends Completions {
922
911
  return result;
923
912
  }, async error => {
924
913
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
925
- await sendEventToPosthog({
926
- client: this.phClient,
914
+ await captureAiGeneration(this.phClient, {
927
915
  ...posthogParams,
928
916
  model: openAIParams.model,
929
917
  provider: 'openai',
@@ -931,13 +919,13 @@ class WrappedCompletions extends Completions {
931
919
  output: [],
932
920
  latency: 0,
933
921
  baseURL: this.baseURL,
934
- params: body,
922
+ modelParameters: getModelParams(body),
935
923
  httpStatus,
936
924
  usage: {
937
925
  inputTokens: 0,
938
926
  outputTokens: 0
939
927
  },
940
- error: JSON.stringify(error)
928
+ error
941
929
  });
942
930
  throw error;
943
931
  });
@@ -1017,8 +1005,7 @@ class WrappedResponses extends Responses {
1017
1005
  const latency = (Date.now() - startTime) / 1000;
1018
1006
  const timeToFirstToken = firstTokenTime !== undefined ? (firstTokenTime - startTime) / 1000 : undefined;
1019
1007
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
1020
- await sendEventToPosthog({
1021
- client: this.phClient,
1008
+ await captureAiGeneration(this.phClient, {
1022
1009
  ...posthogParams,
1023
1010
  model: openAIParams.model ?? modelFromResponse,
1024
1011
  provider: 'openai',
@@ -1027,7 +1014,7 @@ class WrappedResponses extends Responses {
1027
1014
  latency,
1028
1015
  timeToFirstToken,
1029
1016
  baseURL: this.baseURL,
1030
- params: body,
1017
+ modelParameters: getModelParams(body),
1031
1018
  httpStatus: 200,
1032
1019
  usage: {
1033
1020
  inputTokens: usage.inputTokens,
@@ -1041,8 +1028,7 @@ class WrappedResponses extends Responses {
1041
1028
  tools: availableTools
1042
1029
  });
1043
1030
  } catch (error) {
1044
- const enrichedError = await sendEventWithErrorToPosthog({
1045
- client: this.phClient,
1031
+ await captureAiGeneration(this.phClient, {
1046
1032
  ...posthogParams,
1047
1033
  model: openAIParams.model,
1048
1034
  provider: 'openai',
@@ -1050,14 +1036,14 @@ class WrappedResponses extends Responses {
1050
1036
  output: [],
1051
1037
  latency: 0,
1052
1038
  baseURL: this.baseURL,
1053
- params: body,
1039
+ modelParameters: getModelParams(body),
1054
1040
  usage: {
1055
1041
  inputTokens: 0,
1056
1042
  outputTokens: 0
1057
1043
  },
1058
- error: error
1044
+ error
1059
1045
  });
1060
- throw enrichedError;
1046
+ throw error;
1061
1047
  }
1062
1048
  })();
1063
1049
  return stream2;
@@ -1072,8 +1058,7 @@ class WrappedResponses extends Responses {
1072
1058
  const formattedOutput = formatResponseOpenAI({
1073
1059
  output: result.output
1074
1060
  });
1075
- await sendEventToPosthog({
1076
- client: this.phClient,
1061
+ await captureAiGeneration(this.phClient, {
1077
1062
  ...posthogParams,
1078
1063
  model: openAIParams.model ?? result.model,
1079
1064
  provider: 'openai',
@@ -1081,7 +1066,7 @@ class WrappedResponses extends Responses {
1081
1066
  output: formattedOutput,
1082
1067
  latency,
1083
1068
  baseURL: this.baseURL,
1084
- params: body,
1069
+ modelParameters: getModelParams(body),
1085
1070
  httpStatus: 200,
1086
1071
  usage: {
1087
1072
  inputTokens: result.usage?.input_tokens ?? 0,
@@ -1098,8 +1083,7 @@ class WrappedResponses extends Responses {
1098
1083
  return result;
1099
1084
  }, async error => {
1100
1085
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1101
- await sendEventToPosthog({
1102
- client: this.phClient,
1086
+ await captureAiGeneration(this.phClient, {
1103
1087
  ...posthogParams,
1104
1088
  model: openAIParams.model,
1105
1089
  provider: 'openai',
@@ -1107,13 +1091,13 @@ class WrappedResponses extends Responses {
1107
1091
  output: [],
1108
1092
  latency: 0,
1109
1093
  baseURL: this.baseURL,
1110
- params: body,
1094
+ modelParameters: getModelParams(body),
1111
1095
  httpStatus,
1112
1096
  usage: {
1113
1097
  inputTokens: 0,
1114
1098
  outputTokens: 0
1115
1099
  },
1116
- error: JSON.stringify(error)
1100
+ error
1117
1101
  });
1118
1102
  throw error;
1119
1103
  });
@@ -1134,8 +1118,7 @@ class WrappedResponses extends Responses {
1134
1118
  const parentPromise = super.parse(openAIParams, options);
1135
1119
  const wrappedPromise = parentPromise.then(async result => {
1136
1120
  const latency = (Date.now() - startTime) / 1000;
1137
- await sendEventToPosthog({
1138
- client: this.phClient,
1121
+ await captureAiGeneration(this.phClient, {
1139
1122
  ...posthogParams,
1140
1123
  model: openAIParams.model ?? result.model,
1141
1124
  provider: 'openai',
@@ -1143,7 +1126,7 @@ class WrappedResponses extends Responses {
1143
1126
  output: result.output,
1144
1127
  latency,
1145
1128
  baseURL: this.baseURL,
1146
- params: body,
1129
+ modelParameters: getModelParams(body),
1147
1130
  httpStatus: 200,
1148
1131
  usage: {
1149
1132
  inputTokens: result.usage?.input_tokens ?? 0,
@@ -1156,8 +1139,7 @@ class WrappedResponses extends Responses {
1156
1139
  });
1157
1140
  return result;
1158
1141
  }, async error => {
1159
- const enrichedError = await sendEventWithErrorToPosthog({
1160
- client: this.phClient,
1142
+ await captureAiGeneration(this.phClient, {
1161
1143
  ...posthogParams,
1162
1144
  model: openAIParams.model,
1163
1145
  provider: 'openai',
@@ -1165,14 +1147,14 @@ class WrappedResponses extends Responses {
1165
1147
  output: [],
1166
1148
  latency: 0,
1167
1149
  baseURL: this.baseURL,
1168
- params: body,
1150
+ modelParameters: getModelParams(body),
1169
1151
  usage: {
1170
1152
  inputTokens: 0,
1171
1153
  outputTokens: 0
1172
1154
  },
1173
- error: JSON.stringify(error)
1155
+ error
1174
1156
  });
1175
- throw enrichedError;
1157
+ throw error;
1176
1158
  });
1177
1159
  return wrappedPromise;
1178
1160
  } finally {
@@ -1196,8 +1178,7 @@ class WrappedEmbeddings extends Embeddings {
1196
1178
  const parentPromise = super.create(openAIParams, options);
1197
1179
  const wrappedPromise = parentPromise.then(async result => {
1198
1180
  const latency = (Date.now() - startTime) / 1000;
1199
- await sendEventToPosthog({
1200
- client: this.phClient,
1181
+ await captureAiGeneration(this.phClient, {
1201
1182
  ...posthogParams,
1202
1183
  eventType: AIEvent.Embedding,
1203
1184
  model: openAIParams.model,
@@ -1207,7 +1188,7 @@ class WrappedEmbeddings extends Embeddings {
1207
1188
  // Embeddings don't have output content
1208
1189
  latency,
1209
1190
  baseURL: this.baseURL,
1210
- params: body,
1191
+ modelParameters: getModelParams(body),
1211
1192
  httpStatus: 200,
1212
1193
  usage: {
1213
1194
  inputTokens: result.usage?.prompt_tokens ?? 0,
@@ -1217,8 +1198,7 @@ class WrappedEmbeddings extends Embeddings {
1217
1198
  return result;
1218
1199
  }, async error => {
1219
1200
  const httpStatus = error && typeof error === 'object' && 'status' in error ? error.status ?? 500 : 500;
1220
- await sendEventToPosthog({
1221
- client: this.phClient,
1201
+ await captureAiGeneration(this.phClient, {
1222
1202
  eventType: AIEvent.Embedding,
1223
1203
  ...posthogParams,
1224
1204
  model: openAIParams.model,
@@ -1228,12 +1208,12 @@ class WrappedEmbeddings extends Embeddings {
1228
1208
  // Embeddings don't have output content
1229
1209
  latency: 0,
1230
1210
  baseURL: this.baseURL,
1231
- params: body,
1211
+ modelParameters: getModelParams(body),
1232
1212
  httpStatus,
1233
1213
  usage: {
1234
1214
  inputTokens: 0
1235
1215
  },
1236
- error: JSON.stringify(error)
1216
+ error
1237
1217
  });
1238
1218
  throw error;
1239
1219
  });
@@ -1307,8 +1287,7 @@ class WrappedTranscriptions extends Transcriptions {
1307
1287
  const latency = (Date.now() - startTime) / 1000;
1308
1288
  const timeToFirstToken = firstTokenTime !== undefined ? (firstTokenTime - startTime) / 1000 : undefined;
1309
1289
  const availableTools = extractAvailableToolCalls('openai', openAIParams);
1310
- await sendEventToPosthog({
1311
- client: this.phClient,
1290
+ await captureAiGeneration(this.phClient, {
1312
1291
  ...posthogParams,
1313
1292
  model: openAIParams.model,
1314
1293
  provider: 'openai',
@@ -1317,14 +1296,13 @@ class WrappedTranscriptions extends Transcriptions {
1317
1296
  latency,
1318
1297
  timeToFirstToken,
1319
1298
  baseURL: this.baseURL,
1320
- params: body,
1299
+ modelParameters: getModelParams(body),
1321
1300
  httpStatus: 200,
1322
1301
  usage,
1323
1302
  tools: availableTools
1324
1303
  });
1325
1304
  } catch (error) {
1326
- const enrichedError = await sendEventWithErrorToPosthog({
1327
- client: this.phClient,
1305
+ await captureAiGeneration(this.phClient, {
1328
1306
  ...posthogParams,
1329
1307
  model: openAIParams.model,
1330
1308
  provider: 'openai',
@@ -1332,14 +1310,14 @@ class WrappedTranscriptions extends Transcriptions {
1332
1310
  output: [],
1333
1311
  latency: 0,
1334
1312
  baseURL: this.baseURL,
1335
- params: body,
1313
+ modelParameters: getModelParams(body),
1336
1314
  usage: {
1337
1315
  inputTokens: 0,
1338
1316
  outputTokens: 0
1339
1317
  },
1340
- error: error
1318
+ error
1341
1319
  });
1342
- throw enrichedError;
1320
+ throw error;
1343
1321
  }
1344
1322
  })();
1345
1323
  return stream2;
@@ -1350,8 +1328,7 @@ class WrappedTranscriptions extends Transcriptions {
1350
1328
  const wrappedPromise = parentPromise.then(async result => {
1351
1329
  if ('text' in result) {
1352
1330
  const latency = (Date.now() - startTime) / 1000;
1353
- await sendEventToPosthog({
1354
- client: this.phClient,
1331
+ await captureAiGeneration(this.phClient, {
1355
1332
  ...posthogParams,
1356
1333
  model: openAIParams.model,
1357
1334
  provider: 'openai',
@@ -1359,7 +1336,7 @@ class WrappedTranscriptions extends Transcriptions {
1359
1336
  output: result.text,
1360
1337
  latency,
1361
1338
  baseURL: this.baseURL,
1362
- params: body,
1339
+ modelParameters: getModelParams(body),
1363
1340
  httpStatus: 200,
1364
1341
  usage: {
1365
1342
  inputTokens: result.usage?.type === 'tokens' ? result.usage.input_tokens ?? 0 : 0,
@@ -1370,8 +1347,7 @@ class WrappedTranscriptions extends Transcriptions {
1370
1347
  return result;
1371
1348
  }
1372
1349
  }, async error => {
1373
- const enrichedError = await sendEventWithErrorToPosthog({
1374
- client: this.phClient,
1350
+ await captureAiGeneration(this.phClient, {
1375
1351
  ...posthogParams,
1376
1352
  model: openAIParams.model,
1377
1353
  provider: 'openai',
@@ -1379,14 +1355,14 @@ class WrappedTranscriptions extends Transcriptions {
1379
1355
  output: [],
1380
1356
  latency: 0,
1381
1357
  baseURL: this.baseURL,
1382
- params: body,
1358
+ modelParameters: getModelParams(body),
1383
1359
  usage: {
1384
1360
  inputTokens: 0,
1385
1361
  outputTokens: 0
1386
1362
  },
1387
- error: error
1363
+ error
1388
1364
  });
1389
- throw enrichedError;
1365
+ throw error;
1390
1366
  });
1391
1367
  return wrappedPromise;
1392
1368
  }