@posthog/ai 7.16.15 → 7.17.1

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