langsmith 0.3.12 → 0.3.14

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/client.cjs CHANGED
@@ -57,8 +57,9 @@ function mergeRuntimeEnvIntoRunCreate(run) {
57
57
  return run;
58
58
  }
59
59
  exports.mergeRuntimeEnvIntoRunCreate = mergeRuntimeEnvIntoRunCreate;
60
- const getTracingSamplingRate = () => {
61
- const samplingRateStr = (0, env_js_1.getLangSmithEnvironmentVariable)("TRACING_SAMPLING_RATE");
60
+ const getTracingSamplingRate = (configRate) => {
61
+ const samplingRateStr = configRate?.toString() ??
62
+ (0, env_js_1.getLangSmithEnvironmentVariable)("TRACING_SAMPLING_RATE");
62
63
  if (samplingRateStr === undefined) {
63
64
  return undefined;
64
65
  }
@@ -102,6 +103,13 @@ const handle429 = async (response) => {
102
103
  // Fall back to existing status checks
103
104
  return false;
104
105
  };
106
+ function _formatFeedbackScore(score) {
107
+ if (typeof score === "number") {
108
+ // Truncate at 4 decimal places
109
+ return Number(score.toFixed(4));
110
+ }
111
+ return score;
112
+ }
105
113
  class AutoBatchQueue {
106
114
  constructor() {
107
115
  Object.defineProperty(this, "items", {
@@ -315,7 +323,7 @@ class Client {
315
323
  value: false
316
324
  });
317
325
  const defaultConfig = Client.getDefaultClientConfig();
318
- this.tracingSampleRate = getTracingSamplingRate();
326
+ this.tracingSampleRate = getTracingSamplingRate(config.tracingSamplingRate);
319
327
  this.apiUrl = trimQuotes(config.apiUrl ?? defaultConfig.apiUrl) ?? "";
320
328
  if (this.apiUrl.endsWith("/")) {
321
329
  this.apiUrl = this.apiUrl.slice(0, -1);
@@ -511,6 +519,13 @@ class Client {
511
519
  bodyParams.cursor = cursors.next;
512
520
  }
513
521
  }
522
+ // Allows mocking for tests
523
+ _shouldSample() {
524
+ if (this.tracingSampleRate === undefined) {
525
+ return true;
526
+ }
527
+ return Math.random() < this.tracingSampleRate;
528
+ }
514
529
  _filterForSampling(runs, patch = false) {
515
530
  if (this.tracingSampleRate === undefined) {
516
531
  return runs;
@@ -528,15 +543,26 @@ class Client {
528
543
  return sampled;
529
544
  }
530
545
  else {
546
+ // For new runs, sample at trace level to maintain consistency
531
547
  const sampled = [];
532
548
  for (const run of runs) {
533
- if ((run.id !== run.trace_id &&
534
- !this.filteredPostUuids.has(run.trace_id)) ||
535
- Math.random() < this.tracingSampleRate) {
536
- sampled.push(run);
549
+ const traceId = run.trace_id ?? run.id;
550
+ // If we've already made a decision about this trace, follow it
551
+ if (this.filteredPostUuids.has(traceId)) {
552
+ continue;
553
+ }
554
+ // For new traces, apply sampling
555
+ if (run.id === traceId) {
556
+ if (this._shouldSample()) {
557
+ sampled.push(run);
558
+ }
559
+ else {
560
+ this.filteredPostUuids.add(traceId);
561
+ }
537
562
  }
538
563
  else {
539
- this.filteredPostUuids.add(run.id);
564
+ // Child runs follow their trace's sampling decision
565
+ sampled.push(run);
540
566
  }
541
567
  }
542
568
  return sampled;
@@ -723,8 +749,8 @@ class Client {
723
749
  preparedUpdateParams = standaloneUpdates;
724
750
  }
725
751
  const rawBatch = {
726
- post: this._filterForSampling(preparedCreateParams),
727
- patch: this._filterForSampling(preparedUpdateParams, true),
752
+ post: preparedCreateParams,
753
+ patch: preparedUpdateParams,
728
754
  };
729
755
  if (!rawBatch.post.length && !rawBatch.patch.length) {
730
756
  return;
@@ -738,6 +764,7 @@ class Client {
738
764
  const batchItems = rawBatch[key].reverse();
739
765
  let batchItem = batchItems.pop();
740
766
  while (batchItem !== undefined) {
767
+ // Type is wrong but this is a deprecated code path anyway
741
768
  batchChunks[key].push(batchItem);
742
769
  batchItem = batchItems.pop();
743
770
  }
@@ -2333,7 +2360,7 @@ class Client {
2333
2360
  id: feedbackId ?? uuid.v4(),
2334
2361
  run_id: runId,
2335
2362
  key,
2336
- score,
2363
+ score: _formatFeedbackScore(score),
2337
2364
  value,
2338
2365
  correction,
2339
2366
  comment,
@@ -2356,7 +2383,7 @@ class Client {
2356
2383
  async updateFeedback(feedbackId, { score, value, correction, comment, }) {
2357
2384
  const feedbackUpdate = {};
2358
2385
  if (score !== undefined && score !== null) {
2359
- feedbackUpdate["score"] = score;
2386
+ feedbackUpdate["score"] = _formatFeedbackScore(score);
2360
2387
  }
2361
2388
  if (value !== undefined && value !== null) {
2362
2389
  feedbackUpdate["value"] = value;
package/dist/client.d.ts CHANGED
@@ -20,6 +20,7 @@ export interface ClientConfig {
20
20
  * Useful if encountering network rate limits at trace high volumes.
21
21
  */
22
22
  manualFlushMode?: boolean;
23
+ tracingSamplingRate?: number;
23
24
  }
24
25
  /**
25
26
  * Represents the parameters for listing runs (spans) from the Langsmith server.
@@ -244,6 +245,7 @@ export declare class Client implements LangSmithTracingClientInterface {
244
245
  private _get;
245
246
  private _getPaginated;
246
247
  private _getCursorPaginatedList;
248
+ private _shouldSample;
247
249
  private _filterForSampling;
248
250
  private _getBatchSizeLimitBytes;
249
251
  private _getMultiPartSupport;
package/dist/client.js CHANGED
@@ -30,8 +30,9 @@ export function mergeRuntimeEnvIntoRunCreate(run) {
30
30
  };
31
31
  return run;
32
32
  }
33
- const getTracingSamplingRate = () => {
34
- const samplingRateStr = getLangSmithEnvironmentVariable("TRACING_SAMPLING_RATE");
33
+ const getTracingSamplingRate = (configRate) => {
34
+ const samplingRateStr = configRate?.toString() ??
35
+ getLangSmithEnvironmentVariable("TRACING_SAMPLING_RATE");
35
36
  if (samplingRateStr === undefined) {
36
37
  return undefined;
37
38
  }
@@ -75,6 +76,13 @@ const handle429 = async (response) => {
75
76
  // Fall back to existing status checks
76
77
  return false;
77
78
  };
79
+ function _formatFeedbackScore(score) {
80
+ if (typeof score === "number") {
81
+ // Truncate at 4 decimal places
82
+ return Number(score.toFixed(4));
83
+ }
84
+ return score;
85
+ }
78
86
  export class AutoBatchQueue {
79
87
  constructor() {
80
88
  Object.defineProperty(this, "items", {
@@ -287,7 +295,7 @@ export class Client {
287
295
  value: false
288
296
  });
289
297
  const defaultConfig = Client.getDefaultClientConfig();
290
- this.tracingSampleRate = getTracingSamplingRate();
298
+ this.tracingSampleRate = getTracingSamplingRate(config.tracingSamplingRate);
291
299
  this.apiUrl = trimQuotes(config.apiUrl ?? defaultConfig.apiUrl) ?? "";
292
300
  if (this.apiUrl.endsWith("/")) {
293
301
  this.apiUrl = this.apiUrl.slice(0, -1);
@@ -483,6 +491,13 @@ export class Client {
483
491
  bodyParams.cursor = cursors.next;
484
492
  }
485
493
  }
494
+ // Allows mocking for tests
495
+ _shouldSample() {
496
+ if (this.tracingSampleRate === undefined) {
497
+ return true;
498
+ }
499
+ return Math.random() < this.tracingSampleRate;
500
+ }
486
501
  _filterForSampling(runs, patch = false) {
487
502
  if (this.tracingSampleRate === undefined) {
488
503
  return runs;
@@ -500,15 +515,26 @@ export class Client {
500
515
  return sampled;
501
516
  }
502
517
  else {
518
+ // For new runs, sample at trace level to maintain consistency
503
519
  const sampled = [];
504
520
  for (const run of runs) {
505
- if ((run.id !== run.trace_id &&
506
- !this.filteredPostUuids.has(run.trace_id)) ||
507
- Math.random() < this.tracingSampleRate) {
508
- sampled.push(run);
521
+ const traceId = run.trace_id ?? run.id;
522
+ // If we've already made a decision about this trace, follow it
523
+ if (this.filteredPostUuids.has(traceId)) {
524
+ continue;
525
+ }
526
+ // For new traces, apply sampling
527
+ if (run.id === traceId) {
528
+ if (this._shouldSample()) {
529
+ sampled.push(run);
530
+ }
531
+ else {
532
+ this.filteredPostUuids.add(traceId);
533
+ }
509
534
  }
510
535
  else {
511
- this.filteredPostUuids.add(run.id);
536
+ // Child runs follow their trace's sampling decision
537
+ sampled.push(run);
512
538
  }
513
539
  }
514
540
  return sampled;
@@ -695,8 +721,8 @@ export class Client {
695
721
  preparedUpdateParams = standaloneUpdates;
696
722
  }
697
723
  const rawBatch = {
698
- post: this._filterForSampling(preparedCreateParams),
699
- patch: this._filterForSampling(preparedUpdateParams, true),
724
+ post: preparedCreateParams,
725
+ patch: preparedUpdateParams,
700
726
  };
701
727
  if (!rawBatch.post.length && !rawBatch.patch.length) {
702
728
  return;
@@ -710,6 +736,7 @@ export class Client {
710
736
  const batchItems = rawBatch[key].reverse();
711
737
  let batchItem = batchItems.pop();
712
738
  while (batchItem !== undefined) {
739
+ // Type is wrong but this is a deprecated code path anyway
713
740
  batchChunks[key].push(batchItem);
714
741
  batchItem = batchItems.pop();
715
742
  }
@@ -2305,7 +2332,7 @@ export class Client {
2305
2332
  id: feedbackId ?? uuid.v4(),
2306
2333
  run_id: runId,
2307
2334
  key,
2308
- score,
2335
+ score: _formatFeedbackScore(score),
2309
2336
  value,
2310
2337
  correction,
2311
2338
  comment,
@@ -2328,7 +2355,7 @@ export class Client {
2328
2355
  async updateFeedback(feedbackId, { score, value, correction, comment, }) {
2329
2356
  const feedbackUpdate = {};
2330
2357
  if (score !== undefined && score !== null) {
2331
- feedbackUpdate["score"] = score;
2358
+ feedbackUpdate["score"] = _formatFeedbackScore(score);
2332
2359
  }
2333
2360
  if (value !== undefined && value !== null) {
2334
2361
  feedbackUpdate["value"] = value;
package/dist/index.cjs CHANGED
@@ -8,4 +8,4 @@ Object.defineProperty(exports, "RunTree", { enumerable: true, get: function () {
8
8
  var fetch_js_1 = require("./singletons/fetch.cjs");
9
9
  Object.defineProperty(exports, "overrideFetchImplementation", { enumerable: true, get: function () { return fetch_js_1.overrideFetchImplementation; } });
10
10
  // Update using yarn bump-version
11
- exports.__version__ = "0.3.12";
11
+ exports.__version__ = "0.3.14";
package/dist/index.d.ts CHANGED
@@ -2,4 +2,4 @@ export { Client, type ClientConfig, type LangSmithTracingClientInterface, } from
2
2
  export type { Dataset, Example, TracerSession, Run, Feedback, RetrieverOutput, } from "./schemas.js";
3
3
  export { RunTree, type RunTreeConfig } from "./run_trees.js";
4
4
  export { overrideFetchImplementation } from "./singletons/fetch.js";
5
- export declare const __version__ = "0.3.12";
5
+ export declare const __version__ = "0.3.14";
package/dist/index.js CHANGED
@@ -2,4 +2,4 @@ export { Client, } from "./client.js";
2
2
  export { RunTree } from "./run_trees.js";
3
3
  export { overrideFetchImplementation } from "./singletons/fetch.js";
4
4
  // Update using yarn bump-version
5
- export const __version__ = "0.3.12";
5
+ export const __version__ = "0.3.14";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langsmith",
3
- "version": "0.3.12",
3
+ "version": "0.3.14",
4
4
  "description": "Client library to connect to the LangSmith LLM Tracing and Evaluation Platform.",
5
5
  "packageManager": "yarn@1.22.19",
6
6
  "files": [