langsmith 0.2.13 → 0.2.14-beta.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.
package/dist/client.cjs CHANGED
@@ -308,6 +308,12 @@ class Client {
308
308
  writable: true,
309
309
  value: void 0
310
310
  });
311
+ Object.defineProperty(this, "manualFlushMode", {
312
+ enumerable: true,
313
+ configurable: true,
314
+ writable: true,
315
+ value: false
316
+ });
311
317
  const defaultConfig = Client.getDefaultClientConfig();
312
318
  this.tracingSampleRate = getTracingSamplingRate();
313
319
  this.apiUrl = trimQuotes(config.apiUrl ?? defaultConfig.apiUrl) ?? "";
@@ -341,6 +347,7 @@ class Client {
341
347
  config.blockOnRootRunFinalization ?? this.blockOnRootRunFinalization;
342
348
  this.batchSizeBytesLimit = config.batchSizeBytesLimit;
343
349
  this.fetchOptions = config.fetchOptions || {};
350
+ this.manualFlushMode = config.manualFlushMode ?? this.manualFlushMode;
344
351
  }
345
352
  static getDefaultClientConfig() {
346
353
  const apiKey = (0, env_js_1.getLangSmithEnvironmentVariable)("API_KEY");
@@ -538,14 +545,17 @@ class Client {
538
545
  return (serverInfo.instance_flags?.dataset_examples_multipart_enabled ?? false);
539
546
  }
540
547
  drainAutoBatchQueue(batchSizeLimit) {
548
+ const promises = [];
541
549
  while (this.autoBatchQueue.items.length > 0) {
542
550
  const [batch, done] = this.autoBatchQueue.pop(batchSizeLimit);
543
551
  if (!batch.length) {
544
552
  done();
545
553
  break;
546
554
  }
547
- void this._processBatch(batch, done).catch(console.error);
555
+ const batchPromise = this._processBatch(batch, done).catch(console.error);
556
+ promises.push(batchPromise);
548
557
  }
558
+ return Promise.all(promises);
549
559
  }
550
560
  async _processBatch(batch, done) {
551
561
  if (!batch.length) {
@@ -580,14 +590,18 @@ class Client {
580
590
  item.item = mergeRuntimeEnvIntoRunCreate(item.item);
581
591
  }
582
592
  const itemPromise = this.autoBatchQueue.push(item);
593
+ if (this.manualFlushMode) {
594
+ // Rely on manual flushing in serverless environments
595
+ return itemPromise;
596
+ }
583
597
  const sizeLimitBytes = await this._getBatchSizeLimitBytes();
584
598
  if (this.autoBatchQueue.sizeBytes > sizeLimitBytes) {
585
- this.drainAutoBatchQueue(sizeLimitBytes);
599
+ void this.drainAutoBatchQueue(sizeLimitBytes);
586
600
  }
587
601
  if (this.autoBatchQueue.items.length > 0) {
588
602
  this.autoBatchTimeout = setTimeout(() => {
589
603
  this.autoBatchTimeout = undefined;
590
- this.drainAutoBatchQueue(sizeLimitBytes);
604
+ void this.drainAutoBatchQueue(sizeLimitBytes);
591
605
  }, this.autoBatchAggregationDelayMs);
592
606
  }
593
607
  return itemPromise;
@@ -629,6 +643,13 @@ class Client {
629
643
  }
630
644
  return await this.settings;
631
645
  }
646
+ /**
647
+ * Flushes current queued traces.
648
+ */
649
+ async flush() {
650
+ const sizeLimitBytes = await this._getBatchSizeLimitBytes();
651
+ await this.drainAutoBatchQueue(sizeLimitBytes);
652
+ }
632
653
  async createRun(run) {
633
654
  if (!this._filterForSampling([run]).length) {
634
655
  return;
@@ -921,7 +942,8 @@ class Client {
921
942
  data.dotted_order !== undefined) {
922
943
  if (run.end_time !== undefined &&
923
944
  data.parent_run_id === undefined &&
924
- this.blockOnRootRunFinalization) {
945
+ this.blockOnRootRunFinalization &&
946
+ !this.manualFlushMode) {
925
947
  // Trigger batches as soon as a root trace ends and wait to ensure trace finishes
926
948
  // in serverless environments.
927
949
  await this.processRunOperation({ action: "update", item: data }).catch(console.error);
@@ -3052,6 +3074,10 @@ class Client {
3052
3074
  * @returns A promise that resolves once all currently pending traces have sent.
3053
3075
  */
3054
3076
  awaitPendingTraceBatches() {
3077
+ if (this.manualFlushMode) {
3078
+ console.warn("[WARNING]: When tracing in manual flush mode, you must call `await client.flush()` manually to submit trace batches.");
3079
+ return Promise.resolve();
3080
+ }
3055
3081
  return Promise.all([
3056
3082
  ...this.autoBatchQueue.items.map(({ itemPromise }) => itemPromise),
3057
3083
  this.batchIngestCaller.queue.onIdle(),
package/dist/client.d.ts CHANGED
@@ -15,6 +15,11 @@ export interface ClientConfig {
15
15
  blockOnRootRunFinalization?: boolean;
16
16
  traceBatchConcurrency?: number;
17
17
  fetchOptions?: RequestInit;
18
+ /**
19
+ * Whether to require manual .flush() calls before sending traces.
20
+ * Useful if encountering network rate limits at trace high volumes.
21
+ */
22
+ manualFlushMode?: boolean;
18
23
  }
19
24
  /**
20
25
  * Represents the parameters for listing runs (spans) from the Langsmith server.
@@ -207,6 +212,7 @@ export declare class Client implements LangSmithTracingClientInterface {
207
212
  private traceBatchConcurrency;
208
213
  private _serverInfo;
209
214
  private _getServerInfoPromise?;
215
+ private manualFlushMode;
210
216
  constructor(config?: ClientConfig);
211
217
  static getDefaultClientConfig(): {
212
218
  apiUrl: string;
@@ -233,6 +239,10 @@ export declare class Client implements LangSmithTracingClientInterface {
233
239
  protected _getServerInfo(): Promise<any>;
234
240
  protected _ensureServerInfo(): Promise<Record<string, any>>;
235
241
  protected _getSettings(): Promise<LangSmithSettings>;
242
+ /**
243
+ * Flushes current queued traces.
244
+ */
245
+ flush(): Promise<void>;
236
246
  createRun(run: CreateRunParams): Promise<void>;
237
247
  /**
238
248
  * Batch ingest/upsert multiple runs in the Langsmith system.
@@ -804,7 +814,7 @@ export declare class Client implements LangSmithTracingClientInterface {
804
814
  *
805
815
  * @returns A promise that resolves once all currently pending traces have sent.
806
816
  */
807
- awaitPendingTraceBatches(): Promise<[...void[], void]>;
817
+ awaitPendingTraceBatches(): Promise<void> | Promise<[...void[], void]>;
808
818
  }
809
819
  export interface LangSmithTracingClientInterface {
810
820
  createRun: (run: CreateRunParams) => Promise<void>;
package/dist/client.js CHANGED
@@ -280,6 +280,12 @@ export class Client {
280
280
  writable: true,
281
281
  value: void 0
282
282
  });
283
+ Object.defineProperty(this, "manualFlushMode", {
284
+ enumerable: true,
285
+ configurable: true,
286
+ writable: true,
287
+ value: false
288
+ });
283
289
  const defaultConfig = Client.getDefaultClientConfig();
284
290
  this.tracingSampleRate = getTracingSamplingRate();
285
291
  this.apiUrl = trimQuotes(config.apiUrl ?? defaultConfig.apiUrl) ?? "";
@@ -313,6 +319,7 @@ export class Client {
313
319
  config.blockOnRootRunFinalization ?? this.blockOnRootRunFinalization;
314
320
  this.batchSizeBytesLimit = config.batchSizeBytesLimit;
315
321
  this.fetchOptions = config.fetchOptions || {};
322
+ this.manualFlushMode = config.manualFlushMode ?? this.manualFlushMode;
316
323
  }
317
324
  static getDefaultClientConfig() {
318
325
  const apiKey = getLangSmithEnvironmentVariable("API_KEY");
@@ -510,14 +517,17 @@ export class Client {
510
517
  return (serverInfo.instance_flags?.dataset_examples_multipart_enabled ?? false);
511
518
  }
512
519
  drainAutoBatchQueue(batchSizeLimit) {
520
+ const promises = [];
513
521
  while (this.autoBatchQueue.items.length > 0) {
514
522
  const [batch, done] = this.autoBatchQueue.pop(batchSizeLimit);
515
523
  if (!batch.length) {
516
524
  done();
517
525
  break;
518
526
  }
519
- void this._processBatch(batch, done).catch(console.error);
527
+ const batchPromise = this._processBatch(batch, done).catch(console.error);
528
+ promises.push(batchPromise);
520
529
  }
530
+ return Promise.all(promises);
521
531
  }
522
532
  async _processBatch(batch, done) {
523
533
  if (!batch.length) {
@@ -552,14 +562,18 @@ export class Client {
552
562
  item.item = mergeRuntimeEnvIntoRunCreate(item.item);
553
563
  }
554
564
  const itemPromise = this.autoBatchQueue.push(item);
565
+ if (this.manualFlushMode) {
566
+ // Rely on manual flushing in serverless environments
567
+ return itemPromise;
568
+ }
555
569
  const sizeLimitBytes = await this._getBatchSizeLimitBytes();
556
570
  if (this.autoBatchQueue.sizeBytes > sizeLimitBytes) {
557
- this.drainAutoBatchQueue(sizeLimitBytes);
571
+ void this.drainAutoBatchQueue(sizeLimitBytes);
558
572
  }
559
573
  if (this.autoBatchQueue.items.length > 0) {
560
574
  this.autoBatchTimeout = setTimeout(() => {
561
575
  this.autoBatchTimeout = undefined;
562
- this.drainAutoBatchQueue(sizeLimitBytes);
576
+ void this.drainAutoBatchQueue(sizeLimitBytes);
563
577
  }, this.autoBatchAggregationDelayMs);
564
578
  }
565
579
  return itemPromise;
@@ -601,6 +615,13 @@ export class Client {
601
615
  }
602
616
  return await this.settings;
603
617
  }
618
+ /**
619
+ * Flushes current queued traces.
620
+ */
621
+ async flush() {
622
+ const sizeLimitBytes = await this._getBatchSizeLimitBytes();
623
+ await this.drainAutoBatchQueue(sizeLimitBytes);
624
+ }
604
625
  async createRun(run) {
605
626
  if (!this._filterForSampling([run]).length) {
606
627
  return;
@@ -893,7 +914,8 @@ export class Client {
893
914
  data.dotted_order !== undefined) {
894
915
  if (run.end_time !== undefined &&
895
916
  data.parent_run_id === undefined &&
896
- this.blockOnRootRunFinalization) {
917
+ this.blockOnRootRunFinalization &&
918
+ !this.manualFlushMode) {
897
919
  // Trigger batches as soon as a root trace ends and wait to ensure trace finishes
898
920
  // in serverless environments.
899
921
  await this.processRunOperation({ action: "update", item: data }).catch(console.error);
@@ -3024,6 +3046,10 @@ export class Client {
3024
3046
  * @returns A promise that resolves once all currently pending traces have sent.
3025
3047
  */
3026
3048
  awaitPendingTraceBatches() {
3049
+ if (this.manualFlushMode) {
3050
+ console.warn("[WARNING]: When tracing in manual flush mode, you must call `await client.flush()` manually to submit trace batches.");
3051
+ return Promise.resolve();
3052
+ }
3027
3053
  return Promise.all([
3028
3054
  ...this.autoBatchQueue.items.map(({ itemPromise }) => itemPromise),
3029
3055
  this.batchIngestCaller.queue.onIdle(),
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.2.13";
11
+ exports.__version__ = "0.2.14-beta.0";
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.2.13";
5
+ export declare const __version__ = "0.2.14-beta.0";
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.2.13";
5
+ export const __version__ = "0.2.14-beta.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "langsmith",
3
- "version": "0.2.13",
3
+ "version": "0.2.14-beta.0",
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": [