braintrust 0.2.5 → 0.3.6

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.
package/dist/browser.js CHANGED
@@ -166,7 +166,7 @@ var ResponseFormatJsonSchema = _zod.z.object({
166
166
  schema: _zod.z.union([_zod.z.object({}).partial().passthrough(), _zod.z.string()]).optional(),
167
167
  strict: _zod.z.union([_zod.z.boolean(), _zod.z.null()]).optional()
168
168
  });
169
- var ResponseFormat = _zod.z.union([
169
+ var ResponseFormatNullish = _zod.z.union([
170
170
  _zod.z.object({ type: _zod.z.literal("json_object") }),
171
171
  _zod.z.object({
172
172
  type: _zod.z.literal("json_schema"),
@@ -182,7 +182,7 @@ var AnyModelParams = _zod.z.object({
182
182
  max_completion_tokens: _zod.z.number().optional(),
183
183
  frequency_penalty: _zod.z.number().optional(),
184
184
  presence_penalty: _zod.z.number().optional(),
185
- response_format: ResponseFormat.optional(),
185
+ response_format: ResponseFormatNullish.optional(),
186
186
  tool_choice: _zod.z.union([
187
187
  _zod.z.literal("auto"),
188
188
  _zod.z.literal("none"),
@@ -220,6 +220,23 @@ var ApiKey = _zod.z.object({
220
220
  user_family_name: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional(),
221
221
  org_id: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional()
222
222
  });
223
+ var AsyncScoringState = _zod.z.union([
224
+ _zod.z.object({
225
+ status: _zod.z.literal("enabled"),
226
+ token: _zod.z.string(),
227
+ function_ids: _zod.z.array(_zod.z.unknown()).min(1),
228
+ skip_logging: _zod.z.union([_zod.z.boolean(), _zod.z.null()]).optional()
229
+ }),
230
+ _zod.z.object({ status: _zod.z.literal("disabled") }),
231
+ _zod.z.null(),
232
+ _zod.z.null()
233
+ ]);
234
+ var AsyncScoringControl = _zod.z.union([
235
+ _zod.z.object({ kind: _zod.z.literal("score_update"), token: _zod.z.string() }),
236
+ _zod.z.object({ kind: _zod.z.literal("state_override"), state: AsyncScoringState }),
237
+ _zod.z.object({ kind: _zod.z.literal("state_force_reselect") }),
238
+ _zod.z.object({ kind: _zod.z.literal("state_enabled_force_rescore") })
239
+ ]);
223
240
  var BraintrustAttachmentReference = _zod.z.object({
224
241
  type: _zod.z.literal("braintrust_attachment"),
225
242
  filename: _zod.z.string().min(1),
@@ -422,7 +439,7 @@ var Dataset = _zod.z.object({
422
439
  user_id: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional(),
423
440
  metadata: _zod.z.union([_zod.z.object({}).partial().passthrough(), _zod.z.null()]).optional()
424
441
  });
425
- var ObjectReference = _zod.z.union([
442
+ var ObjectReferenceNullish = _zod.z.union([
426
443
  _zod.z.object({
427
444
  object_type: _zod.z.enum([
428
445
  "project_logs",
@@ -456,7 +473,7 @@ var DatasetEvent = _zod.z.object({
456
473
  span_id: _zod.z.string(),
457
474
  root_span_id: _zod.z.string(),
458
475
  is_root: _zod.z.union([_zod.z.boolean(), _zod.z.null()]).optional(),
459
- origin: ObjectReference.optional()
476
+ origin: ObjectReferenceNullish.optional()
460
477
  });
461
478
  var EnvVar = _zod.z.object({
462
479
  id: _zod.z.string().uuid(),
@@ -536,7 +553,7 @@ var ExperimentEvent = _zod.z.object({
536
553
  root_span_id: _zod.z.string(),
537
554
  span_attributes: SpanAttributes.optional(),
538
555
  is_root: _zod.z.union([_zod.z.boolean(), _zod.z.null()]).optional(),
539
- origin: ObjectReference.optional()
556
+ origin: ObjectReferenceNullish.optional()
540
557
  });
541
558
  var ExtendedSavedFunctionId = _zod.z.union([
542
559
  _zod.z.object({ type: _zod.z.literal("function"), id: _zod.z.string() }),
@@ -565,7 +582,7 @@ var ModelParams = _zod.z.union([
565
582
  max_completion_tokens: _zod.z.number(),
566
583
  frequency_penalty: _zod.z.number(),
567
584
  presence_penalty: _zod.z.number(),
568
- response_format: ResponseFormat,
585
+ response_format: ResponseFormatNullish,
569
586
  tool_choice: _zod.z.union([
570
587
  _zod.z.literal("auto"),
571
588
  _zod.z.literal("none"),
@@ -888,6 +905,20 @@ var MessageRole = _zod.z.enum([
888
905
  "model",
889
906
  "developer"
890
907
  ]);
908
+ var ObjectReference = _zod.z.object({
909
+ object_type: _zod.z.enum([
910
+ "project_logs",
911
+ "experiment",
912
+ "dataset",
913
+ "prompt",
914
+ "function",
915
+ "prompt_session"
916
+ ]),
917
+ object_id: _zod.z.string().uuid(),
918
+ id: _zod.z.string(),
919
+ _xact_id: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional(),
920
+ created: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional()
921
+ });
891
922
  var OnlineScoreConfig = _zod.z.union([
892
923
  _zod.z.object({
893
924
  sampling_rate: _zod.z.number().gte(0).lte(1),
@@ -1020,7 +1051,7 @@ var ProjectLogsEvent = _zod.z.object({
1020
1051
  root_span_id: _zod.z.string(),
1021
1052
  is_root: _zod.z.union([_zod.z.boolean(), _zod.z.null()]).optional(),
1022
1053
  span_attributes: SpanAttributes.optional(),
1023
- origin: ObjectReference.optional()
1054
+ origin: ObjectReferenceNullish.optional()
1024
1055
  });
1025
1056
  var ProjectScoreType = _zod.z.enum([
1026
1057
  "slider",
@@ -1102,6 +1133,14 @@ var PromptSessionEvent = _zod.z.object({
1102
1133
  completion: _zod.z.unknown().optional(),
1103
1134
  tags: _zod.z.union([_zod.z.array(_zod.z.string()), _zod.z.null()]).optional()
1104
1135
  });
1136
+ var ResponseFormat = _zod.z.union([
1137
+ _zod.z.object({ type: _zod.z.literal("json_object") }),
1138
+ _zod.z.object({
1139
+ type: _zod.z.literal("json_schema"),
1140
+ json_schema: ResponseFormatJsonSchema
1141
+ }),
1142
+ _zod.z.object({ type: _zod.z.literal("text") })
1143
+ ]);
1105
1144
  var Role = _zod.z.object({
1106
1145
  id: _zod.z.string().uuid(),
1107
1146
  org_id: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional(),
@@ -1161,10 +1200,9 @@ var ServiceToken = _zod.z.object({
1161
1200
  created: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional(),
1162
1201
  name: _zod.z.string(),
1163
1202
  preview_name: _zod.z.string(),
1164
- user_id: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional(),
1165
- user_email: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional(),
1166
- user_given_name: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional(),
1167
- user_family_name: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional(),
1203
+ service_account_id: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional(),
1204
+ service_account_email: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional(),
1205
+ service_account_name: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional(),
1168
1206
  org_id: _zod.z.union([_zod.z.string(), _zod.z.null()]).optional()
1169
1207
  });
1170
1208
  var SpanIFrame = _zod.z.object({
@@ -1185,7 +1223,7 @@ var SSEConsoleEventData = _zod.z.object({
1185
1223
  var SSEProgressEventData = _zod.z.object({
1186
1224
  id: _zod.z.string(),
1187
1225
  object_type: FunctionObjectType,
1188
- origin: ObjectReference.and(_zod.z.unknown()).optional(),
1226
+ origin: ObjectReferenceNullish.and(_zod.z.unknown()).optional(),
1189
1227
  format: FunctionFormat,
1190
1228
  output_type: FunctionOutputType,
1191
1229
  name: _zod.z.string(),
@@ -6375,19 +6413,15 @@ function responsesCreateProxy(target) {
6375
6413
  return proxyCreate(target, hooks);
6376
6414
  }
6377
6415
  function parseSpanFromResponseCreateParams(params) {
6378
- const input = [{ role: "user", content: params.input }];
6379
- if (params.instructions) {
6380
- input.push({ role: "system", content: params.instructions });
6381
- }
6382
6416
  const spanArgs = {
6383
6417
  name: "openai.responses.create",
6384
6418
  spanAttributes: {
6385
6419
  type: "llm"
6386
6420
  },
6387
6421
  event: {
6388
- input,
6422
+ input: params.input,
6389
6423
  metadata: {
6390
- ...filterFrom(params, ["input", "instructions"]),
6424
+ ...filterFrom(params, ["input"]),
6391
6425
  provider: "openai"
6392
6426
  }
6393
6427
  },
@@ -6399,25 +6433,29 @@ function parseSpanFromResponseCreateParams(params) {
6399
6433
  };
6400
6434
  }
6401
6435
  function parseEventFromResponseCreateResult(result) {
6402
- return {
6403
- output: _optionalChain([result, 'optionalAccess', _84 => _84.output_text]) || "",
6404
- metrics: parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _85 => _85.usage]))
6405
- };
6436
+ const data = {};
6437
+ if (_optionalChain([result, 'optionalAccess', _84 => _84.output]) !== void 0) {
6438
+ data.output = result.output;
6439
+ }
6440
+ if (result) {
6441
+ const { output, usage, ...metadata } = result;
6442
+ if (Object.keys(metadata).length > 0) {
6443
+ data.metadata = metadata;
6444
+ }
6445
+ }
6446
+ data.metrics = parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _85 => _85.usage]));
6447
+ return data;
6406
6448
  }
6407
6449
  function parseSpanFromResponseParseParams(params) {
6408
- const input = [{ role: "user", content: params.input }];
6409
- if (params.instructions) {
6410
- input.push({ role: "system", content: params.instructions });
6411
- }
6412
6450
  const spanArgs = {
6413
6451
  name: "openai.responses.parse",
6414
6452
  spanAttributes: {
6415
6453
  type: "llm"
6416
6454
  },
6417
6455
  event: {
6418
- input,
6456
+ input: params.input,
6419
6457
  metadata: {
6420
- ...filterFrom(params, ["input", "instructions"]),
6458
+ ...filterFrom(params, ["input"]),
6421
6459
  provider: "openai"
6422
6460
  }
6423
6461
  },
@@ -6429,10 +6467,18 @@ function parseSpanFromResponseParseParams(params) {
6429
6467
  };
6430
6468
  }
6431
6469
  function parseEventFromResponseParseResult(result) {
6432
- return {
6433
- output: _optionalChain([result, 'optionalAccess', _86 => _86.output_parsed]) || _optionalChain([result, 'optionalAccess', _87 => _87.output_text]) || "",
6434
- metrics: parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _88 => _88.usage]))
6435
- };
6470
+ const data = {};
6471
+ if (_optionalChain([result, 'optionalAccess', _86 => _86.output]) !== void 0) {
6472
+ data.output = result.output;
6473
+ }
6474
+ if (result) {
6475
+ const { output, usage, ...metadata } = result;
6476
+ if (Object.keys(metadata).length > 0) {
6477
+ data.metadata = metadata;
6478
+ }
6479
+ }
6480
+ data.metrics = parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _87 => _87.usage]));
6481
+ return data;
6436
6482
  }
6437
6483
  function traceResponseCreateStream(stream, timedSpan) {
6438
6484
  const span = timedSpan.span;
@@ -6448,7 +6494,7 @@ function traceResponseCreateStream(stream, timedSpan) {
6448
6494
  return result;
6449
6495
  }
6450
6496
  const item = result.value;
6451
- if (!item || !_optionalChain([item, 'optionalAccess', _89 => _89.type]) || !_optionalChain([item, 'optionalAccess', _90 => _90.response])) {
6497
+ if (!item || !_optionalChain([item, 'optionalAccess', _88 => _88.type]) || !_optionalChain([item, 'optionalAccess', _89 => _89.response])) {
6452
6498
  return result;
6453
6499
  }
6454
6500
  const event = parseLogFromItem(item);
@@ -6459,24 +6505,24 @@ function traceResponseCreateStream(stream, timedSpan) {
6459
6505
  };
6460
6506
  }
6461
6507
  function parseLogFromItem(item) {
6462
- if (!item || !_optionalChain([item, 'optionalAccess', _91 => _91.type]) || !_optionalChain([item, 'optionalAccess', _92 => _92.response])) {
6508
+ if (!item || !_optionalChain([item, 'optionalAccess', _90 => _90.type]) || !_optionalChain([item, 'optionalAccess', _91 => _91.response])) {
6463
6509
  return {};
6464
6510
  }
6465
6511
  const response = item.response;
6466
6512
  switch (item.type) {
6467
6513
  case "response.completed":
6468
- const texts = [];
6469
- for (const output of _optionalChain([response, 'optionalAccess', _93 => _93.output]) || []) {
6470
- for (const content of _optionalChain([output, 'optionalAccess', _94 => _94.content]) || []) {
6471
- if (_optionalChain([content, 'optionalAccess', _95 => _95.type]) === "output_text") {
6472
- texts.push(content.text);
6473
- }
6514
+ const data = {};
6515
+ if (_optionalChain([response, 'optionalAccess', _92 => _92.output]) !== void 0) {
6516
+ data.output = response.output;
6517
+ }
6518
+ if (response) {
6519
+ const { usage, output, ...metadata } = response;
6520
+ if (Object.keys(metadata).length > 0) {
6521
+ data.metadata = metadata;
6474
6522
  }
6475
6523
  }
6476
- return {
6477
- output: texts.join(""),
6478
- metrics: parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _96 => _96.usage]))
6479
- };
6524
+ data.metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _93 => _93.usage]));
6525
+ return data;
6480
6526
  default:
6481
6527
  return {};
6482
6528
  }
@@ -6659,8 +6705,8 @@ function wrapOpenAIv4(openai) {
6659
6705
  const embeddingProxy = createEndpointProxy(openai.embeddings, wrapEmbeddings);
6660
6706
  const moderationProxy = createEndpointProxy(openai.moderations, wrapModerations);
6661
6707
  let betaProxy;
6662
- if (_optionalChain([openai, 'access', _97 => _97.beta, 'optionalAccess', _98 => _98.chat, 'optionalAccess', _99 => _99.completions, 'optionalAccess', _100 => _100.stream])) {
6663
- const betaChatCompletionProxy = new Proxy(_optionalChain([openai, 'optionalAccess', _101 => _101.beta, 'optionalAccess', _102 => _102.chat, 'access', _103 => _103.completions]), {
6708
+ if (_optionalChain([openai, 'access', _94 => _94.beta, 'optionalAccess', _95 => _95.chat, 'optionalAccess', _96 => _96.completions, 'optionalAccess', _97 => _97.stream])) {
6709
+ const betaChatCompletionProxy = new Proxy(_optionalChain([openai, 'optionalAccess', _98 => _98.beta, 'optionalAccess', _99 => _99.chat, 'access', _100 => _100.completions]), {
6664
6710
  get(target, name, receiver) {
6665
6711
  const baseVal = Reflect.get(target, name, receiver);
6666
6712
  if (name === "parse") {
@@ -6708,7 +6754,7 @@ function wrapOpenAIv4(openai) {
6708
6754
  });
6709
6755
  }
6710
6756
  function logCompletionResponse(startTime, response, span) {
6711
- const metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _104 => _104.usage]));
6757
+ const metrics = parseMetricsFromUsage(_optionalChain([response, 'optionalAccess', _101 => _101.usage]));
6712
6758
  metrics.time_to_first_token = getCurrentUnixTimestamp() - startTime;
6713
6759
  span.log({
6714
6760
  output: response.choices,
@@ -6803,55 +6849,80 @@ function logHeaders(response, span) {
6803
6849
  }
6804
6850
  }
6805
6851
  function wrapChatCompletion(completion) {
6806
- return async (allParams, options) => {
6852
+ return (allParams, options) => {
6807
6853
  const { span_info: _, ...params } = allParams;
6808
- const span = startSpan(
6809
- _core.mergeDicts.call(void 0,
6810
- {
6811
- name: "Chat Completion",
6812
- spanAttributes: {
6813
- type: _core.SpanTypeAttribute.LLM
6814
- }
6815
- },
6816
- parseChatCompletionParams(allParams)
6817
- )
6818
- );
6819
- const startTime = getCurrentUnixTimestamp();
6820
- if (params.stream) {
6821
- const { data: ret, response } = await completion(
6822
- // We could get rid of this type coercion if we could somehow enforce
6823
- // that `P extends ChatParams` BUT does not have the property
6824
- // `span_info`.
6825
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
6826
- params,
6827
- options
6828
- ).withResponse();
6829
- logHeaders(response, span);
6830
- const wrapperStream = new WrapperStream(span, startTime, ret.iterator());
6831
- ret.iterator = () => wrapperStream[Symbol.asyncIterator]();
6832
- return ret;
6833
- } else {
6834
- try {
6835
- const completionResponse = completion(
6836
- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
6837
- params,
6838
- options
6839
- );
6840
- const { data: ret, response } = await completionResponse.withResponse();
6841
- logHeaders(response, span);
6842
- const { messages, ...rest } = params;
6843
- span.log({
6844
- input: messages,
6845
- metadata: {
6846
- ...rest
6854
+ let executionPromise = null;
6855
+ const executeWrapped = () => {
6856
+ if (!executionPromise) {
6857
+ executionPromise = (async () => {
6858
+ const span = startSpan(
6859
+ _core.mergeDicts.call(void 0,
6860
+ {
6861
+ name: "Chat Completion",
6862
+ spanAttributes: {
6863
+ type: _core.SpanTypeAttribute.LLM
6864
+ }
6865
+ },
6866
+ parseChatCompletionParams(allParams)
6867
+ )
6868
+ );
6869
+ const startTime = getCurrentUnixTimestamp();
6870
+ if (params.stream) {
6871
+ const { data: ret, response } = await completion(
6872
+ // We could get rid of this type coercion if we could somehow enforce
6873
+ // that `P extends ChatParams` BUT does not have the property
6874
+ // `span_info`.
6875
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
6876
+ params,
6877
+ options
6878
+ ).withResponse();
6879
+ logHeaders(response, span);
6880
+ const wrapperStream = new WrapperStream(
6881
+ span,
6882
+ startTime,
6883
+ ret.iterator()
6884
+ );
6885
+ ret.iterator = () => wrapperStream[Symbol.asyncIterator]();
6886
+ return { data: ret, response };
6887
+ } else {
6888
+ try {
6889
+ const completionResponse = completion(
6890
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
6891
+ params,
6892
+ options
6893
+ );
6894
+ const { data: ret, response } = await completionResponse.withResponse();
6895
+ logHeaders(response, span);
6896
+ const { messages, ...rest } = params;
6897
+ span.log({
6898
+ input: messages,
6899
+ metadata: {
6900
+ ...rest
6901
+ }
6902
+ });
6903
+ logCompletionResponse(startTime, ret, span);
6904
+ return { data: ret, response };
6905
+ } finally {
6906
+ span.end();
6907
+ }
6847
6908
  }
6848
- });
6849
- logCompletionResponse(startTime, ret, span);
6850
- return ret;
6851
- } finally {
6852
- span.end();
6909
+ })();
6853
6910
  }
6854
- }
6911
+ return executionPromise;
6912
+ };
6913
+ const dataPromise = executeWrapped().then((result) => result.data);
6914
+ return new Proxy(dataPromise, {
6915
+ get(target, prop, receiver) {
6916
+ if (prop === "withResponse") {
6917
+ return executeWrapped;
6918
+ }
6919
+ const value = Reflect.get(target, prop, receiver);
6920
+ if (typeof value === "function") {
6921
+ return value.bind(target);
6922
+ }
6923
+ return value;
6924
+ }
6925
+ });
6855
6926
  };
6856
6927
  }
6857
6928
  function parseBaseParams(allParams, inputField) {
@@ -6910,7 +6981,7 @@ function parseChatCompletionParams(params) {
6910
6981
  function processEmbeddingResponse(result, span) {
6911
6982
  span.log({
6912
6983
  output: { embedding_length: result.data[0].embedding.length },
6913
- metrics: parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _105 => _105.usage]))
6984
+ metrics: parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _102 => _102.usage]))
6914
6985
  });
6915
6986
  }
6916
6987
  function processModerationResponse(result, span) {
@@ -6940,10 +7011,10 @@ function postprocessStreamingResults(allResults) {
6940
7011
  if (result.usage) {
6941
7012
  metrics = {
6942
7013
  ...metrics,
6943
- ...parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _106 => _106.usage]))
7014
+ ...parseMetricsFromUsage(_optionalChain([result, 'optionalAccess', _103 => _103.usage]))
6944
7015
  };
6945
7016
  }
6946
- const delta = _optionalChain([result, 'access', _107 => _107.choices, 'optionalAccess', _108 => _108[0], 'optionalAccess', _109 => _109.delta]);
7017
+ const delta = _optionalChain([result, 'access', _104 => _104.choices, 'optionalAccess', _105 => _105[0], 'optionalAccess', _106 => _106.delta]);
6947
7018
  if (!delta) {
6948
7019
  continue;
6949
7020
  }