@langchain/core 0.3.58-rc.0 → 0.3.59-rc.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.
@@ -63,7 +63,25 @@ export interface BaseLanguageModelParams extends AsyncCallerParams, BaseLangChai
63
63
  callbackManager?: CallbackManager;
64
64
  cache?: BaseCache | boolean;
65
65
  }
66
- export interface BaseLanguageModelCallOptions extends RunnableConfig {
66
+ export interface BaseLanguageModelTracingCallOptions {
67
+ /**
68
+ * Describes the format of structured outputs.
69
+ * This should be provided if an output is considered to be structured
70
+ */
71
+ ls_structured_output_format?: {
72
+ /**
73
+ * An object containing the method used for structured output (e.g., "jsonMode").
74
+ */
75
+ kwargs: {
76
+ method: string;
77
+ };
78
+ /**
79
+ * The JSON schema describing the expected output structure.
80
+ */
81
+ schema?: JSONSchema;
82
+ };
83
+ }
84
+ export interface BaseLanguageModelCallOptions extends RunnableConfig, BaseLanguageModelTracingCallOptions {
67
85
  /**
68
86
  * Stop tokens to use for this call.
69
87
  * If not provided, the default stop tokens for the model will be used.
@@ -38,7 +38,7 @@ class AsyncLocalStorageProvider {
38
38
  const langChainTracer = callbackManager?.handlers?.find((handler) => handler?.name === "langchain_tracer");
39
39
  let runTree;
40
40
  if (langChainTracer && parentRunId) {
41
- runTree = langChainTracer.convertToRunTree(parentRunId);
41
+ runTree = langChainTracer.getRunTreeWithTracingConfig(parentRunId);
42
42
  }
43
43
  else if (!avoidCreatingRootRunTree) {
44
44
  runTree = new langsmith_1.RunTree({
@@ -34,7 +34,7 @@ class AsyncLocalStorageProvider {
34
34
  const langChainTracer = callbackManager?.handlers?.find((handler) => handler?.name === "langchain_tracer");
35
35
  let runTree;
36
36
  if (langChainTracer && parentRunId) {
37
- runTree = langChainTracer.convertToRunTree(parentRunId);
37
+ runTree = langChainTracer.getRunTreeWithTracingConfig(parentRunId);
38
38
  }
39
39
  else if (!avoidCreatingRootRunTree) {
40
40
  runTree = new RunTree({
@@ -1,7 +1,41 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BaseTracer = exports.isBaseTracer = void 0;
4
+ const run_trees_1 = require("langsmith/run_trees");
4
5
  const base_js_1 = require("../callbacks/base.cjs");
6
+ const env_js_1 = require("../utils/env.cjs");
7
+ // TODO: Remove and just use base LangSmith Run type
8
+ const convertRunTreeToRun = (runTree) => {
9
+ if (!runTree) {
10
+ return undefined;
11
+ }
12
+ // Important that we return the raw run tree object since the reference
13
+ // is mutated in other places.
14
+ // TODO: Remove places where this is being done.
15
+ // eslint-disable-next-line no-param-reassign
16
+ runTree.events = runTree.events ?? [];
17
+ // eslint-disable-next-line no-param-reassign
18
+ runTree.child_runs = runTree.child_runs ?? [];
19
+ // TODO: Remove this cast and just use the LangSmith RunTree type.
20
+ return runTree;
21
+ };
22
+ function convertRunToRunTree(run, parentRun) {
23
+ if (!run) {
24
+ return undefined;
25
+ }
26
+ return new run_trees_1.RunTree({
27
+ ...run,
28
+ parent_run: convertRunToRunTree(parentRun),
29
+ child_runs: run.child_runs
30
+ .map((r) => convertRunToRunTree(r))
31
+ .filter((r) => r !== undefined),
32
+ extra: {
33
+ ...run.extra,
34
+ runtime: (0, env_js_1.getRuntimeEnvironmentSync)(),
35
+ },
36
+ tracingEnabled: false,
37
+ });
38
+ }
5
39
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
6
40
  function _coerceToDict(value, defaultKey) {
7
41
  return value && !Array.isArray(value) && typeof value === "object"
@@ -22,16 +56,37 @@ exports.isBaseTracer = isBaseTracer;
22
56
  class BaseTracer extends base_js_1.BaseCallbackHandler {
23
57
  constructor(_fields) {
24
58
  super(...arguments);
59
+ /** @deprecated Use `runTreeMap` instead. */
25
60
  Object.defineProperty(this, "runMap", {
26
61
  enumerable: true,
27
62
  configurable: true,
28
63
  writable: true,
29
64
  value: new Map()
30
65
  });
66
+ Object.defineProperty(this, "runTreeMap", {
67
+ enumerable: true,
68
+ configurable: true,
69
+ writable: true,
70
+ value: new Map()
71
+ });
72
+ Object.defineProperty(this, "usesRunTreeMap", {
73
+ enumerable: true,
74
+ configurable: true,
75
+ writable: true,
76
+ value: false
77
+ });
31
78
  }
32
79
  copy() {
33
80
  return this;
34
81
  }
82
+ getRunById(runId) {
83
+ if (runId === undefined) {
84
+ return undefined;
85
+ }
86
+ return this.usesRunTreeMap
87
+ ? convertRunTreeToRun(this.runTreeMap.get(runId))
88
+ : this.runMap.get(runId);
89
+ }
35
90
  stringifyError(error) {
36
91
  // eslint-disable-next-line no-instanceof/no-instanceof
37
92
  if (error instanceof Error) {
@@ -48,8 +103,8 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
48
103
  _addRunToRunMap(run) {
49
104
  const currentDottedOrder = convertToDottedOrderFormat(run.start_time, run.id, run.execution_order);
50
105
  const storedRun = { ...run };
106
+ const parentRun = this.getRunById(storedRun.parent_run_id);
51
107
  if (storedRun.parent_run_id !== undefined) {
52
- const parentRun = this.runMap.get(storedRun.parent_run_id);
53
108
  if (parentRun) {
54
109
  this._addChildRun(parentRun, storedRun);
55
110
  parentRun.child_execution_order = Math.max(parentRun.child_execution_order, storedRun.child_execution_order);
@@ -76,22 +131,35 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
76
131
  storedRun.trace_id = storedRun.id;
77
132
  storedRun.dotted_order = currentDottedOrder;
78
133
  }
79
- this.runMap.set(storedRun.id, storedRun);
134
+ if (this.usesRunTreeMap) {
135
+ const runTree = convertRunToRunTree(storedRun, parentRun);
136
+ if (runTree !== undefined) {
137
+ this.runTreeMap.set(storedRun.id, runTree);
138
+ }
139
+ }
140
+ else {
141
+ this.runMap.set(storedRun.id, storedRun);
142
+ }
80
143
  return storedRun;
81
144
  }
82
145
  async _endTrace(run) {
83
- const parentRun = run.parent_run_id !== undefined && this.runMap.get(run.parent_run_id);
146
+ const parentRun = run.parent_run_id !== undefined && this.getRunById(run.parent_run_id);
84
147
  if (parentRun) {
85
148
  parentRun.child_execution_order = Math.max(parentRun.child_execution_order, run.child_execution_order);
86
149
  }
87
150
  else {
88
151
  await this.persistRun(run);
89
152
  }
90
- this.runMap.delete(run.id);
91
153
  await this.onRunUpdate?.(run);
154
+ if (this.usesRunTreeMap) {
155
+ this.runTreeMap.delete(run.id);
156
+ }
157
+ else {
158
+ this.runMap.delete(run.id);
159
+ }
92
160
  }
93
161
  _getExecutionOrder(parentRunId) {
94
- const parentRun = parentRunId !== undefined && this.runMap.get(parentRunId);
162
+ const parentRun = parentRunId !== undefined && this.getRunById(parentRunId);
95
163
  // If a run has no parent then execution order is 1
96
164
  if (!parentRun) {
97
165
  return 1;
@@ -132,7 +200,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
132
200
  return this._addRunToRunMap(run);
133
201
  }
134
202
  async handleLLMStart(llm, prompts, runId, parentRunId, extraParams, tags, metadata, name) {
135
- const run = this.runMap.get(runId) ??
203
+ const run = this.getRunById(runId) ??
136
204
  this._createRunForLLMStart(llm, prompts, runId, parentRunId, extraParams, tags, metadata, name);
137
205
  await this.onRunCreate?.(run);
138
206
  await this.onLLMStart?.(run);
@@ -172,14 +240,14 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
172
240
  return this._addRunToRunMap(run);
173
241
  }
174
242
  async handleChatModelStart(llm, messages, runId, parentRunId, extraParams, tags, metadata, name) {
175
- const run = this.runMap.get(runId) ??
243
+ const run = this.getRunById(runId) ??
176
244
  this._createRunForChatModelStart(llm, messages, runId, parentRunId, extraParams, tags, metadata, name);
177
245
  await this.onRunCreate?.(run);
178
246
  await this.onLLMStart?.(run);
179
247
  return run;
180
248
  }
181
249
  async handleLLMEnd(output, runId, _parentRunId, _tags, extraParams) {
182
- const run = this.runMap.get(runId);
250
+ const run = this.getRunById(runId);
183
251
  if (!run || run?.run_type !== "llm") {
184
252
  throw new Error("No LLM run to end.");
185
253
  }
@@ -195,7 +263,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
195
263
  return run;
196
264
  }
197
265
  async handleLLMError(error, runId, _parentRunId, _tags, extraParams) {
198
- const run = this.runMap.get(runId);
266
+ const run = this.getRunById(runId);
199
267
  if (!run || run?.run_type !== "llm") {
200
268
  throw new Error("No LLM run to end.");
201
269
  }
@@ -241,14 +309,14 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
241
309
  return this._addRunToRunMap(run);
242
310
  }
243
311
  async handleChainStart(chain, inputs, runId, parentRunId, tags, metadata, runType, name) {
244
- const run = this.runMap.get(runId) ??
312
+ const run = this.getRunById(runId) ??
245
313
  this._createRunForChainStart(chain, inputs, runId, parentRunId, tags, metadata, runType, name);
246
314
  await this.onRunCreate?.(run);
247
315
  await this.onChainStart?.(run);
248
316
  return run;
249
317
  }
250
318
  async handleChainEnd(outputs, runId, _parentRunId, _tags, kwargs) {
251
- const run = this.runMap.get(runId);
319
+ const run = this.getRunById(runId);
252
320
  if (!run) {
253
321
  throw new Error("No chain run to end.");
254
322
  }
@@ -266,7 +334,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
266
334
  return run;
267
335
  }
268
336
  async handleChainError(error, runId, _parentRunId, _tags, kwargs) {
269
- const run = this.runMap.get(runId);
337
+ const run = this.getRunById(runId);
270
338
  if (!run) {
271
339
  throw new Error("No chain run to end.");
272
340
  }
@@ -314,7 +382,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
314
382
  return this._addRunToRunMap(run);
315
383
  }
316
384
  async handleToolStart(tool, input, runId, parentRunId, tags, metadata, name) {
317
- const run = this.runMap.get(runId) ??
385
+ const run = this.getRunById(runId) ??
318
386
  this._createRunForToolStart(tool, input, runId, parentRunId, tags, metadata, name);
319
387
  await this.onRunCreate?.(run);
320
388
  await this.onToolStart?.(run);
@@ -322,7 +390,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
322
390
  }
323
391
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
324
392
  async handleToolEnd(output, runId) {
325
- const run = this.runMap.get(runId);
393
+ const run = this.getRunById(runId);
326
394
  if (!run || run?.run_type !== "tool") {
327
395
  throw new Error("No tool run to end");
328
396
  }
@@ -337,7 +405,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
337
405
  return run;
338
406
  }
339
407
  async handleToolError(error, runId) {
340
- const run = this.runMap.get(runId);
408
+ const run = this.getRunById(runId);
341
409
  if (!run || run?.run_type !== "tool") {
342
410
  throw new Error("No tool run to end");
343
411
  }
@@ -352,7 +420,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
352
420
  return run;
353
421
  }
354
422
  async handleAgentAction(action, runId) {
355
- const run = this.runMap.get(runId);
423
+ const run = this.getRunById(runId);
356
424
  if (!run || run?.run_type !== "chain") {
357
425
  return;
358
426
  }
@@ -367,7 +435,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
367
435
  await this.onAgentAction?.(run);
368
436
  }
369
437
  async handleAgentEnd(action, runId) {
370
- const run = this.runMap.get(runId);
438
+ const run = this.getRunById(runId);
371
439
  if (!run || run?.run_type !== "chain") {
372
440
  return;
373
441
  }
@@ -409,14 +477,14 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
409
477
  return this._addRunToRunMap(run);
410
478
  }
411
479
  async handleRetrieverStart(retriever, query, runId, parentRunId, tags, metadata, name) {
412
- const run = this.runMap.get(runId) ??
480
+ const run = this.getRunById(runId) ??
413
481
  this._createRunForRetrieverStart(retriever, query, runId, parentRunId, tags, metadata, name);
414
482
  await this.onRunCreate?.(run);
415
483
  await this.onRetrieverStart?.(run);
416
484
  return run;
417
485
  }
418
486
  async handleRetrieverEnd(documents, runId) {
419
- const run = this.runMap.get(runId);
487
+ const run = this.getRunById(runId);
420
488
  if (!run || run?.run_type !== "retriever") {
421
489
  throw new Error("No retriever run to end");
422
490
  }
@@ -431,7 +499,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
431
499
  return run;
432
500
  }
433
501
  async handleRetrieverError(error, runId) {
434
- const run = this.runMap.get(runId);
502
+ const run = this.getRunById(runId);
435
503
  if (!run || run?.run_type !== "retriever") {
436
504
  throw new Error("No retriever run to end");
437
505
  }
@@ -446,7 +514,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
446
514
  return run;
447
515
  }
448
516
  async handleText(text, runId) {
449
- const run = this.runMap.get(runId);
517
+ const run = this.getRunById(runId);
450
518
  if (!run || run?.run_type !== "chain") {
451
519
  return;
452
520
  }
@@ -458,7 +526,7 @@ class BaseTracer extends base_js_1.BaseCallbackHandler {
458
526
  await this.onText?.(run);
459
527
  }
460
528
  async handleLLMNewToken(token, idx, runId, _parentRunId, _tags, fields) {
461
- const run = this.runMap.get(runId);
529
+ const run = this.getRunById(runId);
462
530
  if (!run || run?.run_type !== "llm") {
463
531
  throw new Error(`Invalid "runId" provided to "handleLLMNewToken" callback.`);
464
532
  }
@@ -1,4 +1,5 @@
1
1
  import { KVMap, BaseRun } from "langsmith/schemas";
2
+ import { RunTree } from "langsmith/run_trees";
2
3
  import type { ChainValues } from "../utils/types/index.js";
3
4
  import type { AgentAction, AgentFinish } from "../agents.js";
4
5
  import type { LLMResult } from "../outputs.js";
@@ -26,9 +27,13 @@ export interface AgentRun extends Run {
26
27
  }
27
28
  export declare function isBaseTracer(x: BaseCallbackHandler): x is BaseTracer;
28
29
  export declare abstract class BaseTracer extends BaseCallbackHandler {
30
+ /** @deprecated Use `runTreeMap` instead. */
29
31
  protected runMap: Map<string, Run>;
32
+ protected runTreeMap: Map<string, RunTree>;
33
+ protected usesRunTreeMap: boolean;
30
34
  constructor(_fields?: BaseCallbackHandlerInput);
31
35
  copy(): this;
36
+ protected getRunById(runId?: string): Run | undefined;
32
37
  protected stringifyError(error: unknown): string;
33
38
  protected abstract persistRun(run: Run): Promise<void>;
34
39
  protected _addChildRun(parentRun: Run, childRun: Run): void;
@@ -1,4 +1,38 @@
1
+ import { RunTree } from "langsmith/run_trees";
1
2
  import { BaseCallbackHandler, } from "../callbacks/base.js";
3
+ import { getRuntimeEnvironmentSync } from "../utils/env.js";
4
+ // TODO: Remove and just use base LangSmith Run type
5
+ const convertRunTreeToRun = (runTree) => {
6
+ if (!runTree) {
7
+ return undefined;
8
+ }
9
+ // Important that we return the raw run tree object since the reference
10
+ // is mutated in other places.
11
+ // TODO: Remove places where this is being done.
12
+ // eslint-disable-next-line no-param-reassign
13
+ runTree.events = runTree.events ?? [];
14
+ // eslint-disable-next-line no-param-reassign
15
+ runTree.child_runs = runTree.child_runs ?? [];
16
+ // TODO: Remove this cast and just use the LangSmith RunTree type.
17
+ return runTree;
18
+ };
19
+ function convertRunToRunTree(run, parentRun) {
20
+ if (!run) {
21
+ return undefined;
22
+ }
23
+ return new RunTree({
24
+ ...run,
25
+ parent_run: convertRunToRunTree(parentRun),
26
+ child_runs: run.child_runs
27
+ .map((r) => convertRunToRunTree(r))
28
+ .filter((r) => r !== undefined),
29
+ extra: {
30
+ ...run.extra,
31
+ runtime: getRuntimeEnvironmentSync(),
32
+ },
33
+ tracingEnabled: false,
34
+ });
35
+ }
2
36
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
3
37
  function _coerceToDict(value, defaultKey) {
4
38
  return value && !Array.isArray(value) && typeof value === "object"
@@ -18,16 +52,37 @@ export function isBaseTracer(x) {
18
52
  export class BaseTracer extends BaseCallbackHandler {
19
53
  constructor(_fields) {
20
54
  super(...arguments);
55
+ /** @deprecated Use `runTreeMap` instead. */
21
56
  Object.defineProperty(this, "runMap", {
22
57
  enumerable: true,
23
58
  configurable: true,
24
59
  writable: true,
25
60
  value: new Map()
26
61
  });
62
+ Object.defineProperty(this, "runTreeMap", {
63
+ enumerable: true,
64
+ configurable: true,
65
+ writable: true,
66
+ value: new Map()
67
+ });
68
+ Object.defineProperty(this, "usesRunTreeMap", {
69
+ enumerable: true,
70
+ configurable: true,
71
+ writable: true,
72
+ value: false
73
+ });
27
74
  }
28
75
  copy() {
29
76
  return this;
30
77
  }
78
+ getRunById(runId) {
79
+ if (runId === undefined) {
80
+ return undefined;
81
+ }
82
+ return this.usesRunTreeMap
83
+ ? convertRunTreeToRun(this.runTreeMap.get(runId))
84
+ : this.runMap.get(runId);
85
+ }
31
86
  stringifyError(error) {
32
87
  // eslint-disable-next-line no-instanceof/no-instanceof
33
88
  if (error instanceof Error) {
@@ -44,8 +99,8 @@ export class BaseTracer extends BaseCallbackHandler {
44
99
  _addRunToRunMap(run) {
45
100
  const currentDottedOrder = convertToDottedOrderFormat(run.start_time, run.id, run.execution_order);
46
101
  const storedRun = { ...run };
102
+ const parentRun = this.getRunById(storedRun.parent_run_id);
47
103
  if (storedRun.parent_run_id !== undefined) {
48
- const parentRun = this.runMap.get(storedRun.parent_run_id);
49
104
  if (parentRun) {
50
105
  this._addChildRun(parentRun, storedRun);
51
106
  parentRun.child_execution_order = Math.max(parentRun.child_execution_order, storedRun.child_execution_order);
@@ -72,22 +127,35 @@ export class BaseTracer extends BaseCallbackHandler {
72
127
  storedRun.trace_id = storedRun.id;
73
128
  storedRun.dotted_order = currentDottedOrder;
74
129
  }
75
- this.runMap.set(storedRun.id, storedRun);
130
+ if (this.usesRunTreeMap) {
131
+ const runTree = convertRunToRunTree(storedRun, parentRun);
132
+ if (runTree !== undefined) {
133
+ this.runTreeMap.set(storedRun.id, runTree);
134
+ }
135
+ }
136
+ else {
137
+ this.runMap.set(storedRun.id, storedRun);
138
+ }
76
139
  return storedRun;
77
140
  }
78
141
  async _endTrace(run) {
79
- const parentRun = run.parent_run_id !== undefined && this.runMap.get(run.parent_run_id);
142
+ const parentRun = run.parent_run_id !== undefined && this.getRunById(run.parent_run_id);
80
143
  if (parentRun) {
81
144
  parentRun.child_execution_order = Math.max(parentRun.child_execution_order, run.child_execution_order);
82
145
  }
83
146
  else {
84
147
  await this.persistRun(run);
85
148
  }
86
- this.runMap.delete(run.id);
87
149
  await this.onRunUpdate?.(run);
150
+ if (this.usesRunTreeMap) {
151
+ this.runTreeMap.delete(run.id);
152
+ }
153
+ else {
154
+ this.runMap.delete(run.id);
155
+ }
88
156
  }
89
157
  _getExecutionOrder(parentRunId) {
90
- const parentRun = parentRunId !== undefined && this.runMap.get(parentRunId);
158
+ const parentRun = parentRunId !== undefined && this.getRunById(parentRunId);
91
159
  // If a run has no parent then execution order is 1
92
160
  if (!parentRun) {
93
161
  return 1;
@@ -128,7 +196,7 @@ export class BaseTracer extends BaseCallbackHandler {
128
196
  return this._addRunToRunMap(run);
129
197
  }
130
198
  async handleLLMStart(llm, prompts, runId, parentRunId, extraParams, tags, metadata, name) {
131
- const run = this.runMap.get(runId) ??
199
+ const run = this.getRunById(runId) ??
132
200
  this._createRunForLLMStart(llm, prompts, runId, parentRunId, extraParams, tags, metadata, name);
133
201
  await this.onRunCreate?.(run);
134
202
  await this.onLLMStart?.(run);
@@ -168,14 +236,14 @@ export class BaseTracer extends BaseCallbackHandler {
168
236
  return this._addRunToRunMap(run);
169
237
  }
170
238
  async handleChatModelStart(llm, messages, runId, parentRunId, extraParams, tags, metadata, name) {
171
- const run = this.runMap.get(runId) ??
239
+ const run = this.getRunById(runId) ??
172
240
  this._createRunForChatModelStart(llm, messages, runId, parentRunId, extraParams, tags, metadata, name);
173
241
  await this.onRunCreate?.(run);
174
242
  await this.onLLMStart?.(run);
175
243
  return run;
176
244
  }
177
245
  async handleLLMEnd(output, runId, _parentRunId, _tags, extraParams) {
178
- const run = this.runMap.get(runId);
246
+ const run = this.getRunById(runId);
179
247
  if (!run || run?.run_type !== "llm") {
180
248
  throw new Error("No LLM run to end.");
181
249
  }
@@ -191,7 +259,7 @@ export class BaseTracer extends BaseCallbackHandler {
191
259
  return run;
192
260
  }
193
261
  async handleLLMError(error, runId, _parentRunId, _tags, extraParams) {
194
- const run = this.runMap.get(runId);
262
+ const run = this.getRunById(runId);
195
263
  if (!run || run?.run_type !== "llm") {
196
264
  throw new Error("No LLM run to end.");
197
265
  }
@@ -237,14 +305,14 @@ export class BaseTracer extends BaseCallbackHandler {
237
305
  return this._addRunToRunMap(run);
238
306
  }
239
307
  async handleChainStart(chain, inputs, runId, parentRunId, tags, metadata, runType, name) {
240
- const run = this.runMap.get(runId) ??
308
+ const run = this.getRunById(runId) ??
241
309
  this._createRunForChainStart(chain, inputs, runId, parentRunId, tags, metadata, runType, name);
242
310
  await this.onRunCreate?.(run);
243
311
  await this.onChainStart?.(run);
244
312
  return run;
245
313
  }
246
314
  async handleChainEnd(outputs, runId, _parentRunId, _tags, kwargs) {
247
- const run = this.runMap.get(runId);
315
+ const run = this.getRunById(runId);
248
316
  if (!run) {
249
317
  throw new Error("No chain run to end.");
250
318
  }
@@ -262,7 +330,7 @@ export class BaseTracer extends BaseCallbackHandler {
262
330
  return run;
263
331
  }
264
332
  async handleChainError(error, runId, _parentRunId, _tags, kwargs) {
265
- const run = this.runMap.get(runId);
333
+ const run = this.getRunById(runId);
266
334
  if (!run) {
267
335
  throw new Error("No chain run to end.");
268
336
  }
@@ -310,7 +378,7 @@ export class BaseTracer extends BaseCallbackHandler {
310
378
  return this._addRunToRunMap(run);
311
379
  }
312
380
  async handleToolStart(tool, input, runId, parentRunId, tags, metadata, name) {
313
- const run = this.runMap.get(runId) ??
381
+ const run = this.getRunById(runId) ??
314
382
  this._createRunForToolStart(tool, input, runId, parentRunId, tags, metadata, name);
315
383
  await this.onRunCreate?.(run);
316
384
  await this.onToolStart?.(run);
@@ -318,7 +386,7 @@ export class BaseTracer extends BaseCallbackHandler {
318
386
  }
319
387
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
320
388
  async handleToolEnd(output, runId) {
321
- const run = this.runMap.get(runId);
389
+ const run = this.getRunById(runId);
322
390
  if (!run || run?.run_type !== "tool") {
323
391
  throw new Error("No tool run to end");
324
392
  }
@@ -333,7 +401,7 @@ export class BaseTracer extends BaseCallbackHandler {
333
401
  return run;
334
402
  }
335
403
  async handleToolError(error, runId) {
336
- const run = this.runMap.get(runId);
404
+ const run = this.getRunById(runId);
337
405
  if (!run || run?.run_type !== "tool") {
338
406
  throw new Error("No tool run to end");
339
407
  }
@@ -348,7 +416,7 @@ export class BaseTracer extends BaseCallbackHandler {
348
416
  return run;
349
417
  }
350
418
  async handleAgentAction(action, runId) {
351
- const run = this.runMap.get(runId);
419
+ const run = this.getRunById(runId);
352
420
  if (!run || run?.run_type !== "chain") {
353
421
  return;
354
422
  }
@@ -363,7 +431,7 @@ export class BaseTracer extends BaseCallbackHandler {
363
431
  await this.onAgentAction?.(run);
364
432
  }
365
433
  async handleAgentEnd(action, runId) {
366
- const run = this.runMap.get(runId);
434
+ const run = this.getRunById(runId);
367
435
  if (!run || run?.run_type !== "chain") {
368
436
  return;
369
437
  }
@@ -405,14 +473,14 @@ export class BaseTracer extends BaseCallbackHandler {
405
473
  return this._addRunToRunMap(run);
406
474
  }
407
475
  async handleRetrieverStart(retriever, query, runId, parentRunId, tags, metadata, name) {
408
- const run = this.runMap.get(runId) ??
476
+ const run = this.getRunById(runId) ??
409
477
  this._createRunForRetrieverStart(retriever, query, runId, parentRunId, tags, metadata, name);
410
478
  await this.onRunCreate?.(run);
411
479
  await this.onRetrieverStart?.(run);
412
480
  return run;
413
481
  }
414
482
  async handleRetrieverEnd(documents, runId) {
415
- const run = this.runMap.get(runId);
483
+ const run = this.getRunById(runId);
416
484
  if (!run || run?.run_type !== "retriever") {
417
485
  throw new Error("No retriever run to end");
418
486
  }
@@ -427,7 +495,7 @@ export class BaseTracer extends BaseCallbackHandler {
427
495
  return run;
428
496
  }
429
497
  async handleRetrieverError(error, runId) {
430
- const run = this.runMap.get(runId);
498
+ const run = this.getRunById(runId);
431
499
  if (!run || run?.run_type !== "retriever") {
432
500
  throw new Error("No retriever run to end");
433
501
  }
@@ -442,7 +510,7 @@ export class BaseTracer extends BaseCallbackHandler {
442
510
  return run;
443
511
  }
444
512
  async handleText(text, runId) {
445
- const run = this.runMap.get(runId);
513
+ const run = this.getRunById(runId);
446
514
  if (!run || run?.run_type !== "chain") {
447
515
  return;
448
516
  }
@@ -454,7 +522,7 @@ export class BaseTracer extends BaseCallbackHandler {
454
522
  await this.onText?.(run);
455
523
  }
456
524
  async handleLLMNewToken(token, idx, runId, _parentRunId, _tags, fields) {
457
- const run = this.runMap.get(runId);
525
+ const run = this.getRunById(runId);
458
526
  if (!run || run?.run_type !== "llm") {
459
527
  throw new Error(`Invalid "runId" provided to "handleLLMNewToken" callback.`);
460
528
  }
@@ -33,11 +33,24 @@ class LangChainTracer extends base_js_1.BaseTracer {
33
33
  writable: true,
34
34
  value: void 0
35
35
  });
36
- const { exampleId, projectName, client } = fields;
36
+ Object.defineProperty(this, "replicas", {
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true,
40
+ value: void 0
41
+ });
42
+ Object.defineProperty(this, "usesRunTreeMap", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: true
47
+ });
48
+ const { exampleId, projectName, client, replicas } = fields;
37
49
  this.projectName =
38
50
  projectName ??
39
51
  (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_PROJECT") ??
40
52
  (0, env_js_1.getEnvironmentVariable)("LANGCHAIN_SESSION");
53
+ this.replicas = replicas;
41
54
  this.exampleId = exampleId;
42
55
  this.client = client ?? (0, tracer_js_1.getDefaultLangChainClientSingleton)();
43
56
  const traceableTree = LangChainTracer.getTraceableRunTree();
@@ -45,40 +58,17 @@ class LangChainTracer extends base_js_1.BaseTracer {
45
58
  this.updateFromRunTree(traceableTree);
46
59
  }
47
60
  }
48
- async _convertToCreate(run, example_id = undefined) {
49
- return {
50
- ...run,
51
- extra: {
52
- ...run.extra,
53
- runtime: await (0, env_js_1.getRuntimeEnvironment)(),
54
- },
55
- child_runs: undefined,
56
- session_name: this.projectName,
57
- reference_example_id: run.parent_run_id ? undefined : example_id,
58
- };
59
- }
60
61
  async persistRun(_run) { }
61
62
  async onRunCreate(run) {
62
- const persistedRun = await this._convertToCreate(run, this.exampleId);
63
- await this.client.createRun(persistedRun);
63
+ const runTree = this.getRunTreeWithTracingConfig(run.id);
64
+ await runTree?.postRun();
64
65
  }
65
66
  async onRunUpdate(run) {
66
- const runUpdate = {
67
- end_time: run.end_time,
68
- error: run.error,
69
- outputs: run.outputs,
70
- events: run.events,
71
- inputs: run.inputs,
72
- trace_id: run.trace_id,
73
- dotted_order: run.dotted_order,
74
- parent_run_id: run.parent_run_id,
75
- extra: run.extra,
76
- session_name: this.projectName,
77
- };
78
- await this.client.updateRun(run.id, runUpdate);
67
+ const runTree = this.getRunTreeWithTracingConfig(run.id);
68
+ await runTree?.patchRun();
79
69
  }
80
70
  getRun(id) {
81
- return this.runMap.get(id);
71
+ return this.runTreeMap.get(id);
82
72
  }
83
73
  updateFromRunTree(runTree) {
84
74
  let rootRun = runTree;
@@ -98,56 +88,28 @@ class LangChainTracer extends base_js_1.BaseTracer {
98
88
  if (!current || visited.has(current.id))
99
89
  continue;
100
90
  visited.add(current.id);
101
- // @ts-expect-error Types of property 'events' are incompatible.
102
- this.runMap.set(current.id, current);
91
+ this.runTreeMap.set(current.id, current);
103
92
  if (current.child_runs) {
104
93
  queue.push(...current.child_runs);
105
94
  }
106
95
  }
107
96
  this.client = runTree.client ?? this.client;
97
+ this.replicas = runTree.replicas ?? this.replicas;
108
98
  this.projectName = runTree.project_name ?? this.projectName;
109
99
  this.exampleId = runTree.reference_example_id ?? this.exampleId;
110
100
  }
111
- convertToRunTree(id) {
112
- const runTreeMap = {};
113
- const runTreeList = [];
114
- for (const [id, run] of this.runMap) {
115
- // by converting the run map to a run tree, we are doing a copy
116
- // thus, any mutation performed on the run tree will not be reflected
117
- // back in the run map
118
- // TODO: Stop using `this.runMap` in favour of LangSmith's `RunTree`
119
- const runTree = new run_trees_1.RunTree({
120
- ...run,
121
- child_runs: [],
122
- parent_run: undefined,
123
- // inherited properties
124
- client: this.client,
125
- project_name: this.projectName,
126
- reference_example_id: this.exampleId,
127
- tracingEnabled: true,
128
- });
129
- runTreeMap[id] = runTree;
130
- runTreeList.push([id, run.dotted_order]);
131
- }
132
- runTreeList.sort((a, b) => {
133
- if (!a[1] || !b[1])
134
- return 0;
135
- return a[1].localeCompare(b[1]);
101
+ getRunTreeWithTracingConfig(id) {
102
+ const runTree = this.runTreeMap.get(id);
103
+ if (!runTree)
104
+ return undefined;
105
+ return new run_trees_1.RunTree({
106
+ ...runTree,
107
+ client: this.client,
108
+ project_name: this.projectName,
109
+ replicas: this.replicas,
110
+ reference_example_id: this.exampleId,
111
+ tracingEnabled: true,
136
112
  });
137
- for (const [id] of runTreeList) {
138
- const run = this.runMap.get(id);
139
- const runTree = runTreeMap[id];
140
- if (!run || !runTree)
141
- continue;
142
- if (run.parent_run_id) {
143
- const parentRunTree = runTreeMap[run.parent_run_id];
144
- if (parentRunTree) {
145
- parentRunTree.child_runs.push(runTree);
146
- runTree.parent_run = parentRunTree;
147
- }
148
- }
149
- }
150
- return runTreeMap[id];
151
113
  }
152
114
  static getTraceableRunTree() {
153
115
  try {
@@ -24,19 +24,21 @@ export interface LangChainTracerFields extends BaseCallbackHandlerInput {
24
24
  exampleId?: string;
25
25
  projectName?: string;
26
26
  client?: LangSmithTracingClientInterface;
27
+ replicas?: RunTree["replicas"];
27
28
  }
28
29
  export declare class LangChainTracer extends BaseTracer implements LangChainTracerFields {
29
30
  name: string;
30
31
  projectName?: string;
31
32
  exampleId?: string;
32
33
  client: LangSmithTracingClientInterface;
34
+ replicas?: RunTree["replicas"];
35
+ usesRunTreeMap: boolean;
33
36
  constructor(fields?: LangChainTracerFields);
34
- private _convertToCreate;
35
37
  protected persistRun(_run: Run): Promise<void>;
36
38
  onRunCreate(run: Run): Promise<void>;
37
39
  onRunUpdate(run: Run): Promise<void>;
38
40
  getRun(id: string): Run | undefined;
39
41
  updateFromRunTree(runTree: RunTree): void;
40
- convertToRunTree(id: string): RunTree | undefined;
42
+ getRunTreeWithTracingConfig(id: string): RunTree | undefined;
41
43
  static getTraceableRunTree(): RunTree | undefined;
42
44
  }
@@ -1,6 +1,6 @@
1
1
  import { RunTree } from "langsmith/run_trees";
2
2
  import { getCurrentRunTree } from "langsmith/singletons/traceable";
3
- import { getEnvironmentVariable, getRuntimeEnvironment } from "../utils/env.js";
3
+ import { getEnvironmentVariable } from "../utils/env.js";
4
4
  import { BaseTracer } from "./base.js";
5
5
  import { getDefaultLangChainClientSingleton } from "../singletons/tracer.js";
6
6
  export class LangChainTracer extends BaseTracer {
@@ -30,11 +30,24 @@ export class LangChainTracer extends BaseTracer {
30
30
  writable: true,
31
31
  value: void 0
32
32
  });
33
- const { exampleId, projectName, client } = fields;
33
+ Object.defineProperty(this, "replicas", {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: void 0
38
+ });
39
+ Object.defineProperty(this, "usesRunTreeMap", {
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true,
43
+ value: true
44
+ });
45
+ const { exampleId, projectName, client, replicas } = fields;
34
46
  this.projectName =
35
47
  projectName ??
36
48
  getEnvironmentVariable("LANGCHAIN_PROJECT") ??
37
49
  getEnvironmentVariable("LANGCHAIN_SESSION");
50
+ this.replicas = replicas;
38
51
  this.exampleId = exampleId;
39
52
  this.client = client ?? getDefaultLangChainClientSingleton();
40
53
  const traceableTree = LangChainTracer.getTraceableRunTree();
@@ -42,40 +55,17 @@ export class LangChainTracer extends BaseTracer {
42
55
  this.updateFromRunTree(traceableTree);
43
56
  }
44
57
  }
45
- async _convertToCreate(run, example_id = undefined) {
46
- return {
47
- ...run,
48
- extra: {
49
- ...run.extra,
50
- runtime: await getRuntimeEnvironment(),
51
- },
52
- child_runs: undefined,
53
- session_name: this.projectName,
54
- reference_example_id: run.parent_run_id ? undefined : example_id,
55
- };
56
- }
57
58
  async persistRun(_run) { }
58
59
  async onRunCreate(run) {
59
- const persistedRun = await this._convertToCreate(run, this.exampleId);
60
- await this.client.createRun(persistedRun);
60
+ const runTree = this.getRunTreeWithTracingConfig(run.id);
61
+ await runTree?.postRun();
61
62
  }
62
63
  async onRunUpdate(run) {
63
- const runUpdate = {
64
- end_time: run.end_time,
65
- error: run.error,
66
- outputs: run.outputs,
67
- events: run.events,
68
- inputs: run.inputs,
69
- trace_id: run.trace_id,
70
- dotted_order: run.dotted_order,
71
- parent_run_id: run.parent_run_id,
72
- extra: run.extra,
73
- session_name: this.projectName,
74
- };
75
- await this.client.updateRun(run.id, runUpdate);
64
+ const runTree = this.getRunTreeWithTracingConfig(run.id);
65
+ await runTree?.patchRun();
76
66
  }
77
67
  getRun(id) {
78
- return this.runMap.get(id);
68
+ return this.runTreeMap.get(id);
79
69
  }
80
70
  updateFromRunTree(runTree) {
81
71
  let rootRun = runTree;
@@ -95,56 +85,28 @@ export class LangChainTracer extends BaseTracer {
95
85
  if (!current || visited.has(current.id))
96
86
  continue;
97
87
  visited.add(current.id);
98
- // @ts-expect-error Types of property 'events' are incompatible.
99
- this.runMap.set(current.id, current);
88
+ this.runTreeMap.set(current.id, current);
100
89
  if (current.child_runs) {
101
90
  queue.push(...current.child_runs);
102
91
  }
103
92
  }
104
93
  this.client = runTree.client ?? this.client;
94
+ this.replicas = runTree.replicas ?? this.replicas;
105
95
  this.projectName = runTree.project_name ?? this.projectName;
106
96
  this.exampleId = runTree.reference_example_id ?? this.exampleId;
107
97
  }
108
- convertToRunTree(id) {
109
- const runTreeMap = {};
110
- const runTreeList = [];
111
- for (const [id, run] of this.runMap) {
112
- // by converting the run map to a run tree, we are doing a copy
113
- // thus, any mutation performed on the run tree will not be reflected
114
- // back in the run map
115
- // TODO: Stop using `this.runMap` in favour of LangSmith's `RunTree`
116
- const runTree = new RunTree({
117
- ...run,
118
- child_runs: [],
119
- parent_run: undefined,
120
- // inherited properties
121
- client: this.client,
122
- project_name: this.projectName,
123
- reference_example_id: this.exampleId,
124
- tracingEnabled: true,
125
- });
126
- runTreeMap[id] = runTree;
127
- runTreeList.push([id, run.dotted_order]);
128
- }
129
- runTreeList.sort((a, b) => {
130
- if (!a[1] || !b[1])
131
- return 0;
132
- return a[1].localeCompare(b[1]);
98
+ getRunTreeWithTracingConfig(id) {
99
+ const runTree = this.runTreeMap.get(id);
100
+ if (!runTree)
101
+ return undefined;
102
+ return new RunTree({
103
+ ...runTree,
104
+ client: this.client,
105
+ project_name: this.projectName,
106
+ replicas: this.replicas,
107
+ reference_example_id: this.exampleId,
108
+ tracingEnabled: true,
133
109
  });
134
- for (const [id] of runTreeList) {
135
- const run = this.runMap.get(id);
136
- const runTree = runTreeMap[id];
137
- if (!run || !runTree)
138
- continue;
139
- if (run.parent_run_id) {
140
- const parentRunTree = runTreeMap[run.parent_run_id];
141
- if (parentRunTree) {
142
- parentRunTree.child_runs.push(runTree);
143
- runTree.parent_run = parentRunTree;
144
- }
145
- }
146
- }
147
- return runTreeMap[id];
148
110
  }
149
111
  static getTraceableRunTree() {
150
112
  try {
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getEnvironmentVariable = exports.getRuntimeEnvironment = exports.getEnv = exports.isNode = exports.isDeno = exports.isJsDom = exports.isWebWorker = exports.isBrowser = void 0;
3
+ exports.getEnvironmentVariable = exports.getRuntimeEnvironmentSync = exports.getRuntimeEnvironment = exports.getEnv = exports.isNode = exports.isDeno = exports.isJsDom = exports.isWebWorker = exports.isBrowser = void 0;
4
4
  const isBrowser = () => typeof window !== "undefined" && typeof window.document !== "undefined";
5
5
  exports.isBrowser = isBrowser;
6
6
  const isWebWorker = () => typeof globalThis === "object" &&
@@ -8,9 +8,7 @@ const isWebWorker = () => typeof globalThis === "object" &&
8
8
  globalThis.constructor.name === "DedicatedWorkerGlobalScope";
9
9
  exports.isWebWorker = isWebWorker;
10
10
  const isJsDom = () => (typeof window !== "undefined" && window.name === "nodejs") ||
11
- (typeof navigator !== "undefined" &&
12
- (navigator.userAgent.includes("Node.js") ||
13
- navigator.userAgent.includes("jsdom")));
11
+ (typeof navigator !== "undefined" && navigator.userAgent.includes("jsdom"));
14
12
  exports.isJsDom = isJsDom;
15
13
  // Supabase Edge Function provides a `Deno` global object
16
14
  // without `version` property
@@ -46,7 +44,14 @@ const getEnv = () => {
46
44
  };
47
45
  exports.getEnv = getEnv;
48
46
  let runtimeEnvironment;
47
+ /**
48
+ * @deprecated Use getRuntimeEnvironmentSync instead
49
+ */
49
50
  async function getRuntimeEnvironment() {
51
+ return getRuntimeEnvironmentSync();
52
+ }
53
+ exports.getRuntimeEnvironment = getRuntimeEnvironment;
54
+ function getRuntimeEnvironmentSync() {
50
55
  if (runtimeEnvironment === undefined) {
51
56
  const env = (0, exports.getEnv)();
52
57
  runtimeEnvironment = {
@@ -56,7 +61,7 @@ async function getRuntimeEnvironment() {
56
61
  }
57
62
  return runtimeEnvironment;
58
63
  }
59
- exports.getRuntimeEnvironment = getRuntimeEnvironment;
64
+ exports.getRuntimeEnvironmentSync = getRuntimeEnvironmentSync;
60
65
  function getEnvironmentVariable(name) {
61
66
  // Certain Deno setups will throw an error if you try to access environment variables
62
67
  // https://github.com/langchain-ai/langchainjs/issues/1412
@@ -20,5 +20,9 @@ export type RuntimeEnvironment = {
20
20
  runtime: string;
21
21
  runtimeVersion?: string;
22
22
  };
23
+ /**
24
+ * @deprecated Use getRuntimeEnvironmentSync instead
25
+ */
23
26
  export declare function getRuntimeEnvironment(): Promise<RuntimeEnvironment>;
27
+ export declare function getRuntimeEnvironmentSync(): RuntimeEnvironment;
24
28
  export declare function getEnvironmentVariable(name: string): string | undefined;
package/dist/utils/env.js CHANGED
@@ -3,9 +3,7 @@ export const isWebWorker = () => typeof globalThis === "object" &&
3
3
  globalThis.constructor &&
4
4
  globalThis.constructor.name === "DedicatedWorkerGlobalScope";
5
5
  export const isJsDom = () => (typeof window !== "undefined" && window.name === "nodejs") ||
6
- (typeof navigator !== "undefined" &&
7
- (navigator.userAgent.includes("Node.js") ||
8
- navigator.userAgent.includes("jsdom")));
6
+ (typeof navigator !== "undefined" && navigator.userAgent.includes("jsdom"));
9
7
  // Supabase Edge Function provides a `Deno` global object
10
8
  // without `version` property
11
9
  export const isDeno = () => typeof Deno !== "undefined";
@@ -37,7 +35,13 @@ export const getEnv = () => {
37
35
  return env;
38
36
  };
39
37
  let runtimeEnvironment;
38
+ /**
39
+ * @deprecated Use getRuntimeEnvironmentSync instead
40
+ */
40
41
  export async function getRuntimeEnvironment() {
42
+ return getRuntimeEnvironmentSync();
43
+ }
44
+ export function getRuntimeEnvironmentSync() {
41
45
  if (runtimeEnvironment === undefined) {
42
46
  const env = getEnv();
43
47
  runtimeEnvironment = {
@@ -344,11 +344,9 @@ exports.getInteropZodObjectShape = getInteropZodObjectShape;
344
344
  */
345
345
  function extendInteropZodObject(schema, extension) {
346
346
  if (isZodSchemaV3(schema)) {
347
- // z3: .extend exists and works as expected
348
347
  return schema.extend(extension);
349
348
  }
350
349
  if (isZodSchemaV4(schema)) {
351
- // z4: .extend exists and works as expected
352
350
  return core_1.util.extend(schema, extension);
353
351
  }
354
352
  throw new Error("Schema must be an instance of z3.ZodObject or z4.$ZodObject");
@@ -328,11 +328,9 @@ export function getInteropZodObjectShape(schema) {
328
328
  */
329
329
  export function extendInteropZodObject(schema, extension) {
330
330
  if (isZodSchemaV3(schema)) {
331
- // z3: .extend exists and works as expected
332
331
  return schema.extend(extension);
333
332
  }
334
333
  if (isZodSchemaV4(schema)) {
335
- // z4: .extend exists and works as expected
336
334
  return util.extend(schema, extension);
337
335
  }
338
336
  throw new Error("Schema must be an instance of z3.ZodObject or z4.$ZodObject");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/core",
3
- "version": "0.3.58-rc.0",
3
+ "version": "0.3.59-rc.0",
4
4
  "description": "Core LangChain.js abstractions and schemas",
5
5
  "type": "module",
6
6
  "engines": {
@@ -38,7 +38,7 @@
38
38
  "camelcase": "6",
39
39
  "decamelize": "1.2.0",
40
40
  "js-tiktoken": "^1.0.12",
41
- "langsmith": "^0.3.29",
41
+ "langsmith": "^0.3.32",
42
42
  "mustache": "^4.2.0",
43
43
  "p-queue": "^6.6.2",
44
44
  "p-retry": "4",