langsmith 0.3.62-rc.2 → 0.3.63

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.
@@ -1,4 +1,4 @@
1
- import { LangSmithMiddleware } from "./middleware.js";
1
+ import { LangSmithMiddleware, } from "./middleware.js";
2
2
  import { convertMessageToTracedFormat } from "./utils.js";
3
3
  import { isTraceableFunction, traceable } from "../../traceable.js";
4
4
  const _wrapTools = (tools, lsConfig) => {
@@ -55,6 +55,69 @@ const _formatTracedInputs = (params) => {
55
55
  return { ...rest, prompt, messages };
56
56
  }
57
57
  };
58
+ const _mergeConfig = (baseConfig, runtimeConfig) => {
59
+ return {
60
+ ...baseConfig,
61
+ ...runtimeConfig,
62
+ metadata: {
63
+ ...baseConfig?.metadata,
64
+ ...runtimeConfig?.metadata,
65
+ },
66
+ };
67
+ };
68
+ const _extractChildRunConfig = (lsConfig) => {
69
+ const { id, name, parent_run_id, start_time, end_time, attachments, dotted_order, processInputs, processOutputs, processChildLLMRunInputs, processChildLLMRunOutputs, ...inheritedConfig } = lsConfig ?? {};
70
+ const childConfig = inheritedConfig;
71
+ if (processChildLLMRunInputs) {
72
+ childConfig.processInputs = processChildLLMRunInputs;
73
+ }
74
+ if (processChildLLMRunOutputs) {
75
+ childConfig.processOutputs = processChildLLMRunOutputs;
76
+ }
77
+ return childConfig;
78
+ };
79
+ const _resolveConfigs = (baseLsConfig, runtimeLsConfig) => {
80
+ const baseChildRunConfig = _extractChildRunConfig(baseLsConfig);
81
+ const runtimeChildLLMRunConfig = _extractChildRunConfig(runtimeLsConfig);
82
+ const resolvedLsConfig = _mergeConfig(baseLsConfig, runtimeLsConfig);
83
+ const resolvedChildLLMRunConfig = _mergeConfig(baseChildRunConfig, runtimeChildLLMRunConfig);
84
+ const { processInputs: _processInputs, processOutputs: _processOutputs, ...resolvedToolConfig } = resolvedChildLLMRunConfig;
85
+ return {
86
+ resolvedLsConfig,
87
+ resolvedChildLLMRunConfig,
88
+ resolvedToolConfig,
89
+ };
90
+ };
91
+ /**
92
+ * Wraps LangSmith config in a way that matches AI SDK provider types.
93
+ *
94
+ * ```ts
95
+ * import { createLangSmithProviderOptions } from "langsmith/experimental/vercel";
96
+ * import * as ai from "ai";
97
+ *
98
+ * const lsConfig = createLangSmithProviderOptions<typeof ai.generateText>({
99
+ * // Will have appropriate typing
100
+ * processInputs: (inputs) => {
101
+ * const { messages } = inputs;
102
+ * return {
103
+ * messages: messages?.map((message) => ({
104
+ * ...message,
105
+ * content: "REDACTED",
106
+ * })),
107
+ * prompt: "REDACTED",
108
+ * };
109
+ * },
110
+ * });
111
+ * ```
112
+ *
113
+ * Note: AI SDK expects only JSON values in an object for
114
+ * provider options, but LangSmith's config may contain non-JSON values.
115
+ * These are not passed to the underlying AI SDK model, so it is safe to
116
+ * cast the typing here.
117
+ */
118
+ export const createLangSmithProviderOptions = (lsConfig) => {
119
+ return (lsConfig ?? {});
120
+ };
58
121
  /**
59
122
  * Wraps Vercel AI SDK 5 functions with LangSmith tracing capabilities.
60
123
  *
@@ -71,8 +134,7 @@ const _formatTracedInputs = (params) => {
71
134
  * @returns returns.streamText - Wrapped streamText function that traces calls to LangSmith
72
135
  * @returns returns.streamObject - Wrapped streamObject function that traces calls to LangSmith
73
136
  */
74
- const wrapAISDK = ({ wrapLanguageModel, generateText, streamText, streamObject, generateObject, }, lsConfig) => {
75
- const { id, name, parent_run_id, start_time, end_time, attachments, dotted_order, ...inheritedConfig } = lsConfig ?? {};
137
+ const wrapAISDK = ({ wrapLanguageModel, generateText, streamText, streamObject, generateObject, }, baseLsConfig) => {
76
138
  /**
77
139
  * Wrapped version of AI SDK 5's generateText with LangSmith tracing.
78
140
  *
@@ -93,6 +155,8 @@ const wrapAISDK = ({ wrapLanguageModel, generateText, streamText, streamObject,
93
155
  */
94
156
  const wrappedGenerateText = async (...args) => {
95
157
  const params = args[0];
158
+ const { langsmith: runtimeLsConfig, ...providerOptions } = params.providerOptions ?? {};
159
+ const { resolvedLsConfig, resolvedChildLLMRunConfig, resolvedToolConfig } = _resolveConfigs(baseLsConfig, runtimeLsConfig);
96
160
  const traceableFunc = traceable(async (...args) => {
97
161
  const [params, ...rest] = args;
98
162
  const wrappedModel = wrapLanguageModel({
@@ -100,23 +164,30 @@ const wrapAISDK = ({ wrapLanguageModel, generateText, streamText, streamObject,
100
164
  middleware: LangSmithMiddleware({
101
165
  name: _getModelDisplayName(params.model),
102
166
  modelId: _getModelId(params.model),
103
- lsConfig: inheritedConfig,
167
+ lsConfig: resolvedChildLLMRunConfig,
104
168
  }),
105
169
  });
106
170
  return generateText({
107
171
  ...params,
108
- tools: _wrapTools(params.tools, inheritedConfig),
172
+ providerOptions,
173
+ tools: _wrapTools(params.tools, resolvedToolConfig),
109
174
  model: wrappedModel,
110
175
  }, ...rest);
111
176
  }, {
112
177
  name: _getModelDisplayName(params.model),
113
- ...lsConfig,
178
+ ...resolvedLsConfig,
114
179
  metadata: {
115
180
  ai_sdk_method: "ai.generateText",
116
- ...lsConfig?.metadata,
181
+ ...resolvedLsConfig?.metadata,
182
+ },
183
+ processInputs: (inputs) => {
184
+ const inputFormatter = resolvedLsConfig?.processInputs ?? _formatTracedInputs;
185
+ return inputFormatter(inputs);
117
186
  },
118
- processInputs: (inputs) => _formatTracedInputs(inputs),
119
187
  processOutputs: (outputs) => {
188
+ if (resolvedLsConfig?.processOutputs) {
189
+ return resolvedLsConfig.processOutputs(outputs);
190
+ }
120
191
  if (outputs.outputs == null || typeof outputs.outputs !== "object") {
121
192
  return outputs;
122
193
  }
@@ -159,6 +230,8 @@ const wrapAISDK = ({ wrapLanguageModel, generateText, streamText, streamObject,
159
230
  */
160
231
  const wrappedGenerateObject = async (...args) => {
161
232
  const params = args[0];
233
+ const { langsmith: runtimeLsConfig, ...providerOptions } = params.providerOptions ?? {};
234
+ const { resolvedLsConfig, resolvedChildLLMRunConfig } = _resolveConfigs(baseLsConfig, runtimeLsConfig);
162
235
  const traceableFunc = traceable(async (...args) => {
163
236
  const [params, ...rest] = args;
164
237
  const wrappedModel = wrapLanguageModel({
@@ -166,22 +239,29 @@ const wrapAISDK = ({ wrapLanguageModel, generateText, streamText, streamObject,
166
239
  middleware: LangSmithMiddleware({
167
240
  name: _getModelDisplayName(params.model),
168
241
  modelId: _getModelId(params.model),
169
- lsConfig: inheritedConfig,
242
+ lsConfig: resolvedChildLLMRunConfig,
170
243
  }),
171
244
  });
172
245
  return generateObject({
173
246
  ...params,
247
+ providerOptions,
174
248
  model: wrappedModel,
175
249
  }, ...rest);
176
250
  }, {
177
251
  name: _getModelDisplayName(params.model),
178
- ...lsConfig,
252
+ ...resolvedLsConfig,
179
253
  metadata: {
180
254
  ai_sdk_method: "ai.generateObject",
181
- ...lsConfig?.metadata,
255
+ ...resolvedLsConfig?.metadata,
256
+ },
257
+ processInputs: (inputs) => {
258
+ const inputFormatter = resolvedLsConfig?.processInputs ?? _formatTracedInputs;
259
+ return inputFormatter(inputs);
182
260
  },
183
- processInputs: (inputs) => _formatTracedInputs(inputs),
184
261
  processOutputs: (outputs) => {
262
+ if (resolvedLsConfig?.processOutputs) {
263
+ return resolvedLsConfig.processOutputs(outputs);
264
+ }
185
265
  if (outputs.outputs == null || typeof outputs.outputs !== "object") {
186
266
  return outputs;
187
267
  }
@@ -211,6 +291,8 @@ const wrapAISDK = ({ wrapLanguageModel, generateText, streamText, streamObject,
211
291
  */
212
292
  const wrappedStreamText = (...args) => {
213
293
  const params = args[0];
294
+ const { langsmith: runtimeLsConfig, ...providerOptions } = params.providerOptions ?? {};
295
+ const { resolvedLsConfig, resolvedChildLLMRunConfig, resolvedToolConfig } = _resolveConfigs(baseLsConfig, runtimeLsConfig);
214
296
  const traceableFunc = traceable((...args) => {
215
297
  const [params, ...rest] = args;
216
298
  const wrappedModel = wrapLanguageModel({
@@ -218,23 +300,30 @@ const wrapAISDK = ({ wrapLanguageModel, generateText, streamText, streamObject,
218
300
  middleware: LangSmithMiddleware({
219
301
  name: _getModelDisplayName(params.model),
220
302
  modelId: _getModelId(params.model),
221
- lsConfig: inheritedConfig,
303
+ lsConfig: resolvedChildLLMRunConfig,
222
304
  }),
223
305
  });
224
306
  return streamText({
225
307
  ...params,
226
- tools: _wrapTools(params.tools, inheritedConfig),
308
+ providerOptions,
309
+ tools: _wrapTools(params.tools, resolvedToolConfig),
227
310
  model: wrappedModel,
228
311
  }, ...rest);
229
312
  }, {
230
313
  name: _getModelDisplayName(params.model),
231
- ...lsConfig,
314
+ ...resolvedLsConfig,
232
315
  metadata: {
233
316
  ai_sdk_method: "ai.streamText",
234
- ...lsConfig?.metadata,
317
+ ...resolvedLsConfig?.metadata,
318
+ },
319
+ processInputs: (inputs) => {
320
+ const inputFormatter = resolvedLsConfig?.processInputs ?? _formatTracedInputs;
321
+ return inputFormatter(inputs);
235
322
  },
236
- processInputs: (inputs) => _formatTracedInputs(inputs),
237
323
  processOutputs: async (outputs) => {
324
+ if (resolvedLsConfig?.processOutputs) {
325
+ return resolvedLsConfig.processOutputs(outputs);
326
+ }
238
327
  if (outputs.outputs == null || typeof outputs.outputs !== "object") {
239
328
  return outputs;
240
329
  }
@@ -271,6 +360,8 @@ const wrapAISDK = ({ wrapLanguageModel, generateText, streamText, streamObject,
271
360
  */
272
361
  const wrappedStreamObject = (...args) => {
273
362
  const params = args[0];
363
+ const { langsmith: runtimeLsConfig, ...providerOptions } = params.providerOptions ?? {};
364
+ const { resolvedLsConfig, resolvedChildLLMRunConfig } = _resolveConfigs(baseLsConfig, runtimeLsConfig);
274
365
  const traceableFunc = traceable((...args) => {
275
366
  const [params, ...rest] = args;
276
367
  const wrappedModel = wrapLanguageModel({
@@ -278,22 +369,29 @@ const wrapAISDK = ({ wrapLanguageModel, generateText, streamText, streamObject,
278
369
  middleware: LangSmithMiddleware({
279
370
  name: _getModelDisplayName(params.model),
280
371
  modelId: _getModelId(params.model),
281
- lsConfig: inheritedConfig,
372
+ lsConfig: resolvedChildLLMRunConfig,
282
373
  }),
283
374
  });
284
375
  return streamObject({
285
376
  ...params,
377
+ providerOptions,
286
378
  model: wrappedModel,
287
379
  }, ...rest);
288
380
  }, {
289
381
  name: _getModelDisplayName(params.model),
290
- ...lsConfig,
382
+ ...resolvedLsConfig,
291
383
  metadata: {
292
384
  ai_sdk_method: "ai.streamObject",
293
- ...lsConfig?.metadata,
385
+ ...resolvedLsConfig?.metadata,
386
+ },
387
+ processInputs: (inputs) => {
388
+ const inputFormatter = resolvedLsConfig?.processInputs ?? _formatTracedInputs;
389
+ return inputFormatter(inputs);
294
390
  },
295
- processInputs: (inputs) => _formatTracedInputs(inputs),
296
391
  processOutputs: async (outputs) => {
392
+ if (resolvedLsConfig?.processOutputs) {
393
+ return resolvedLsConfig.processOutputs(outputs);
394
+ }
297
395
  if (outputs.outputs == null || typeof outputs.outputs !== "object") {
298
396
  return outputs;
299
397
  }
@@ -15,7 +15,6 @@ const _formatTracedInputs = (params) => {
15
15
  }
16
16
  return rest;
17
17
  };
18
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
19
18
  const _formatTracedOutputs = (outputs) => {
20
19
  const formattedOutputs = { ...outputs };
21
20
  if (formattedOutputs.role == null) {
@@ -75,11 +74,13 @@ function LangSmithMiddleware(config) {
75
74
  },
76
75
  processInputs: (inputs) => {
77
76
  const typedInputs = inputs;
78
- return _formatTracedInputs(typedInputs);
77
+ const inputFormatter = lsConfig?.processInputs ?? _formatTracedInputs;
78
+ return inputFormatter(typedInputs);
79
79
  },
80
80
  processOutputs: (outputs) => {
81
81
  const typedOutputs = outputs;
82
- return _formatTracedOutputs(typedOutputs);
82
+ const outputFormatter = lsConfig?.processOutputs ?? _formatTracedOutputs;
83
+ return outputFormatter(typedOutputs);
83
84
  },
84
85
  });
85
86
  const res = await traceableFunc(params);
@@ -91,6 +92,8 @@ function LangSmithMiddleware(config) {
91
92
  if (parentRunTree != null &&
92
93
  typeof parentRunTree === "object" &&
93
94
  typeof parentRunTree.createChild === "function") {
95
+ const inputFormatter = lsConfig?.processInputs ?? _formatTracedInputs;
96
+ const formattedInputs = inputFormatter(params);
94
97
  runTree = parentRunTree?.createChild({
95
98
  ...lsConfig,
96
99
  name: name ?? "ai.doStream",
@@ -100,7 +103,7 @@ function LangSmithMiddleware(config) {
100
103
  ai_sdk_method: "ai.doStream",
101
104
  ...lsConfig?.metadata,
102
105
  },
103
- inputs: _formatTracedInputs(params),
106
+ inputs: formattedInputs,
104
107
  });
105
108
  }
106
109
  await runTree?.postRun();
@@ -170,17 +173,21 @@ function LangSmithMiddleware(config) {
170
173
  }
171
174
  }, {
172
175
  content: "",
173
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
176
+ role: "assistant",
174
177
  tool_calls: [],
175
178
  });
176
- await runTree?.end(_formatTracedOutputs(output));
179
+ const outputFormatter = lsConfig?.processOutputs ?? utils_js_1.convertMessageToTracedFormat;
180
+ const formattedOutputs = outputFormatter(output);
181
+ await runTree?.end(formattedOutputs);
177
182
  }
178
183
  catch (error) {
179
184
  await runTree?.end(undefined, error.message ?? String(error));
180
185
  throw error;
181
186
  }
182
187
  finally {
183
- await runTree?.patchRun();
188
+ await runTree?.patchRun({
189
+ excludeInputs: true,
190
+ });
184
191
  }
185
192
  },
186
193
  });
@@ -191,7 +198,9 @@ function LangSmithMiddleware(config) {
191
198
  }
192
199
  catch (error) {
193
200
  await runTree?.end(undefined, error.message ?? String(error));
194
- await runTree?.patchRun();
201
+ await runTree?.patchRun({
202
+ excludeInputs: true,
203
+ });
195
204
  throw error;
196
205
  }
197
206
  },
@@ -1,10 +1,27 @@
1
- import type { LanguageModelV2Middleware } from "@ai-sdk/provider";
1
+ import type { LanguageModelV2Middleware, SharedV2ProviderMetadata, LanguageModelV2FinishReason } from "@ai-sdk/provider";
2
2
  import type { RunTreeConfig } from "../../run_trees.js";
3
+ export type AggregatedDoStreamOutput = {
4
+ content: string;
5
+ role: "assistant";
6
+ tool_calls: {
7
+ id: string;
8
+ type: "function";
9
+ function: {
10
+ name: string;
11
+ arguments: string;
12
+ };
13
+ }[];
14
+ providerMetadata?: SharedV2ProviderMetadata;
15
+ finishReason?: LanguageModelV2FinishReason;
16
+ };
3
17
  /**
4
18
  * AI SDK middleware that wraps an AI SDK 5 model and adds LangSmith tracing.
5
19
  */
6
20
  export declare function LangSmithMiddleware(config?: {
7
21
  name: string;
8
22
  modelId?: string;
9
- lsConfig?: Partial<Omit<RunTreeConfig, "inputs" | "outputs" | "run_type">>;
23
+ lsConfig?: Partial<Omit<RunTreeConfig, "inputs" | "outputs" | "run_type">> & {
24
+ processInputs?: (inputs: Record<string, unknown>) => Record<string, unknown>;
25
+ processOutputs?: (outputs: Record<string, unknown>) => Record<string, unknown>;
26
+ };
10
27
  }): LanguageModelV2Middleware;
@@ -12,7 +12,6 @@ const _formatTracedInputs = (params) => {
12
12
  }
13
13
  return rest;
14
14
  };
15
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
15
  const _formatTracedOutputs = (outputs) => {
17
16
  const formattedOutputs = { ...outputs };
18
17
  if (formattedOutputs.role == null) {
@@ -72,11 +71,13 @@ export function LangSmithMiddleware(config) {
72
71
  },
73
72
  processInputs: (inputs) => {
74
73
  const typedInputs = inputs;
75
- return _formatTracedInputs(typedInputs);
74
+ const inputFormatter = lsConfig?.processInputs ?? _formatTracedInputs;
75
+ return inputFormatter(typedInputs);
76
76
  },
77
77
  processOutputs: (outputs) => {
78
78
  const typedOutputs = outputs;
79
- return _formatTracedOutputs(typedOutputs);
79
+ const outputFormatter = lsConfig?.processOutputs ?? _formatTracedOutputs;
80
+ return outputFormatter(typedOutputs);
80
81
  },
81
82
  });
82
83
  const res = await traceableFunc(params);
@@ -88,6 +89,8 @@ export function LangSmithMiddleware(config) {
88
89
  if (parentRunTree != null &&
89
90
  typeof parentRunTree === "object" &&
90
91
  typeof parentRunTree.createChild === "function") {
92
+ const inputFormatter = lsConfig?.processInputs ?? _formatTracedInputs;
93
+ const formattedInputs = inputFormatter(params);
91
94
  runTree = parentRunTree?.createChild({
92
95
  ...lsConfig,
93
96
  name: name ?? "ai.doStream",
@@ -97,7 +100,7 @@ export function LangSmithMiddleware(config) {
97
100
  ai_sdk_method: "ai.doStream",
98
101
  ...lsConfig?.metadata,
99
102
  },
100
- inputs: _formatTracedInputs(params),
103
+ inputs: formattedInputs,
101
104
  });
102
105
  }
103
106
  await runTree?.postRun();
@@ -167,17 +170,21 @@ export function LangSmithMiddleware(config) {
167
170
  }
168
171
  }, {
169
172
  content: "",
170
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
173
+ role: "assistant",
171
174
  tool_calls: [],
172
175
  });
173
- await runTree?.end(_formatTracedOutputs(output));
176
+ const outputFormatter = lsConfig?.processOutputs ?? convertMessageToTracedFormat;
177
+ const formattedOutputs = outputFormatter(output);
178
+ await runTree?.end(formattedOutputs);
174
179
  }
175
180
  catch (error) {
176
181
  await runTree?.end(undefined, error.message ?? String(error));
177
182
  throw error;
178
183
  }
179
184
  finally {
180
- await runTree?.patchRun();
185
+ await runTree?.patchRun({
186
+ excludeInputs: true,
187
+ });
181
188
  }
182
189
  },
183
190
  });
@@ -188,7 +195,9 @@ export function LangSmithMiddleware(config) {
188
195
  }
189
196
  catch (error) {
190
197
  await runTree?.end(undefined, error.message ?? String(error));
191
- await runTree?.patchRun();
198
+ await runTree?.patchRun({
199
+ excludeInputs: true,
200
+ });
192
201
  throw error;
193
202
  }
194
203
  },
package/dist/index.cjs CHANGED
@@ -10,4 +10,4 @@ Object.defineProperty(exports, "overrideFetchImplementation", { enumerable: true
10
10
  var project_js_1 = require("./utils/project.cjs");
11
11
  Object.defineProperty(exports, "getDefaultProjectName", { enumerable: true, get: function () { return project_js_1.getDefaultProjectName; } });
12
12
  // Update using yarn bump-version
13
- exports.__version__ = "0.3.62-rc.2";
13
+ exports.__version__ = "0.3.63";
package/dist/index.d.ts CHANGED
@@ -3,4 +3,4 @@ export type { Dataset, Example, TracerSession, Run, Feedback, RetrieverOutput, }
3
3
  export { RunTree, type RunTreeConfig } from "./run_trees.js";
4
4
  export { overrideFetchImplementation } from "./singletons/fetch.js";
5
5
  export { getDefaultProjectName } from "./utils/project.js";
6
- export declare const __version__ = "0.3.62-rc.2";
6
+ export declare const __version__ = "0.3.63";
package/dist/index.js CHANGED
@@ -3,4 +3,4 @@ export { RunTree } from "./run_trees.js";
3
3
  export { overrideFetchImplementation } from "./singletons/fetch.js";
4
4
  export { getDefaultProjectName } from "./utils/project.js";
5
5
  // Update using yarn bump-version
6
- export const __version__ = "0.3.62-rc.2";
6
+ export const __version__ = "0.3.63";
@@ -81,6 +81,7 @@ async function getLangchainCallbacks(currentRunTree) {
81
81
  /**
82
82
  * RunnableTraceable is a Runnable that wraps a traceable function.
83
83
  * This allows adding Langsmith traced functions into LangChain sequences.
84
+ * @deprecated Wrap or pass directly instead.
84
85
  */
85
86
  class RunnableTraceable extends runnables_1.Runnable {
86
87
  constructor(fields) {
@@ -111,6 +112,9 @@ class RunnableTraceable extends runnables_1.Runnable {
111
112
  async invoke(input, options) {
112
113
  const [config] = this._getOptionsList(options ?? {}, 1);
113
114
  const callbacks = await (0, runnables_1.getCallbackManagerForConfig)(config);
115
+ // Avoid start time ties - this is old, deprecated code used only in tests
116
+ // and recent perf improvements have made this necessary.
117
+ await new Promise((resolve) => setImmediate(resolve));
114
118
  return (await this.func((0, runnables_1.patchConfig)(config, { callbacks }), input));
115
119
  }
116
120
  async *_streamIterator(input, options) {
@@ -15,6 +15,7 @@ type AnyTraceableFunction = TraceableFunction<(...any: any[]) => any>;
15
15
  /**
16
16
  * RunnableTraceable is a Runnable that wraps a traceable function.
17
17
  * This allows adding Langsmith traced functions into LangChain sequences.
18
+ * @deprecated Wrap or pass directly instead.
18
19
  */
19
20
  export declare class RunnableTraceable<RunInput, RunOutput> extends Runnable<RunInput, RunOutput> {
20
21
  lc_serializable: boolean;
package/dist/langchain.js CHANGED
@@ -77,6 +77,7 @@ export async function getLangchainCallbacks(currentRunTree) {
77
77
  /**
78
78
  * RunnableTraceable is a Runnable that wraps a traceable function.
79
79
  * This allows adding Langsmith traced functions into LangChain sequences.
80
+ * @deprecated Wrap or pass directly instead.
80
81
  */
81
82
  export class RunnableTraceable extends Runnable {
82
83
  constructor(fields) {
@@ -107,6 +108,9 @@ export class RunnableTraceable extends Runnable {
107
108
  async invoke(input, options) {
108
109
  const [config] = this._getOptionsList(options ?? {}, 1);
109
110
  const callbacks = await getCallbackManagerForConfig(config);
111
+ // Avoid start time ties - this is old, deprecated code used only in tests
112
+ // and recent perf improvements have made this necessary.
113
+ await new Promise((resolve) => setImmediate(resolve));
110
114
  return (await this.func(patchConfig(config, { callbacks }), input));
111
115
  }
112
116
  async *_streamIterator(input, options) {
@@ -547,12 +547,12 @@ class RunTree {
547
547
  console.error(`Error in postRun for run ${this.id}:`, error);
548
548
  }
549
549
  }
550
- async patchRun() {
550
+ async patchRun(options) {
551
551
  if (this.replicas && this.replicas.length > 0) {
552
552
  for (const { projectName, apiKey, apiUrl, updates } of this.replicas) {
553
553
  const runData = this._remapForProject(projectName ?? this.project_name);
554
- await this.client.updateRun(runData.id, {
555
- inputs: runData.inputs,
554
+ const updatePayload = {
555
+ id: runData.id,
556
556
  outputs: runData.outputs,
557
557
  error: runData.error,
558
558
  parent_run_id: runData.parent_run_id,
@@ -566,7 +566,14 @@ class RunTree {
566
566
  extra: runData.extra,
567
567
  attachments: this.attachments,
568
568
  ...updates,
569
- }, {
569
+ };
570
+ // Important that inputs is not a key in the run update
571
+ // if excluded because it will overwrite the run create if the
572
+ // two operations are merged during batching
573
+ if (!options?.excludeInputs) {
574
+ updatePayload.inputs = runData.inputs;
575
+ }
576
+ await this.client.updateRun(runData.id, updatePayload, {
570
577
  apiKey,
571
578
  apiUrl,
572
579
  });
@@ -577,7 +584,6 @@ class RunTree {
577
584
  const runUpdate = {
578
585
  end_time: this.end_time,
579
586
  error: this.error,
580
- inputs: this.inputs,
581
587
  outputs: this.outputs,
582
588
  parent_run_id: this.parent_run?.id ?? this.parent_run_id,
583
589
  reference_example_id: this.reference_example_id,
@@ -589,6 +595,12 @@ class RunTree {
589
595
  attachments: this.attachments,
590
596
  session_name: this.project_name,
591
597
  };
598
+ // Important that inputs is not a key in the run update
599
+ // if excluded because it will overwrite the run create if the
600
+ // two operations are merged during batching
601
+ if (!options?.excludeInputs) {
602
+ runUpdate.inputs = this.inputs;
603
+ }
592
604
  await this.client.updateRun(this.id, runUpdate);
593
605
  }
594
606
  catch (error) {
@@ -107,7 +107,9 @@ export declare class RunTree implements BaseRun {
107
107
  private _convertToCreate;
108
108
  private _remapForProject;
109
109
  postRun(excludeChildRuns?: boolean): Promise<void>;
110
- patchRun(): Promise<void>;
110
+ patchRun(options?: {
111
+ excludeInputs?: boolean;
112
+ }): Promise<void>;
111
113
  toJSON(): RunCreate & {
112
114
  id: string;
113
115
  };
package/dist/run_trees.js CHANGED
@@ -508,12 +508,12 @@ export class RunTree {
508
508
  console.error(`Error in postRun for run ${this.id}:`, error);
509
509
  }
510
510
  }
511
- async patchRun() {
511
+ async patchRun(options) {
512
512
  if (this.replicas && this.replicas.length > 0) {
513
513
  for (const { projectName, apiKey, apiUrl, updates } of this.replicas) {
514
514
  const runData = this._remapForProject(projectName ?? this.project_name);
515
- await this.client.updateRun(runData.id, {
516
- inputs: runData.inputs,
515
+ const updatePayload = {
516
+ id: runData.id,
517
517
  outputs: runData.outputs,
518
518
  error: runData.error,
519
519
  parent_run_id: runData.parent_run_id,
@@ -527,7 +527,14 @@ export class RunTree {
527
527
  extra: runData.extra,
528
528
  attachments: this.attachments,
529
529
  ...updates,
530
- }, {
530
+ };
531
+ // Important that inputs is not a key in the run update
532
+ // if excluded because it will overwrite the run create if the
533
+ // two operations are merged during batching
534
+ if (!options?.excludeInputs) {
535
+ updatePayload.inputs = runData.inputs;
536
+ }
537
+ await this.client.updateRun(runData.id, updatePayload, {
531
538
  apiKey,
532
539
  apiUrl,
533
540
  });
@@ -538,7 +545,6 @@ export class RunTree {
538
545
  const runUpdate = {
539
546
  end_time: this.end_time,
540
547
  error: this.error,
541
- inputs: this.inputs,
542
548
  outputs: this.outputs,
543
549
  parent_run_id: this.parent_run?.id ?? this.parent_run_id,
544
550
  reference_example_id: this.reference_example_id,
@@ -550,6 +556,12 @@ export class RunTree {
550
556
  attachments: this.attachments,
551
557
  session_name: this.project_name,
552
558
  };
559
+ // Important that inputs is not a key in the run update
560
+ // if excluded because it will overwrite the run create if the
561
+ // two operations are merged during batching
562
+ if (!options?.excludeInputs) {
563
+ runUpdate.inputs = this.inputs;
564
+ }
553
565
  await this.client.updateRun(this.id, runUpdate);
554
566
  }
555
567
  catch (error) {
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports._getFetchImplementation = exports._globalFetchImplementationIsNodeFetch = exports.overrideFetchImplementation = void 0;
3
+ exports._getFetchImplementation = exports._globalFetchImplementationIsNodeFetch = exports.clearFetchImplementation = exports.overrideFetchImplementation = void 0;
4
4
  const env_js_1 = require("../utils/env.cjs");
5
5
  // Wrap the default fetch call due to issues with illegal invocations
6
6
  // in some environments:
@@ -18,6 +18,10 @@ const overrideFetchImplementation = (fetch) => {
18
18
  globalThis[LANGSMITH_FETCH_IMPLEMENTATION_KEY] = fetch;
19
19
  };
20
20
  exports.overrideFetchImplementation = overrideFetchImplementation;
21
+ const clearFetchImplementation = () => {
22
+ delete globalThis[LANGSMITH_FETCH_IMPLEMENTATION_KEY];
23
+ };
24
+ exports.clearFetchImplementation = clearFetchImplementation;
21
25
  const _globalFetchImplementationIsNodeFetch = () => {
22
26
  const fetchImpl = globalThis[LANGSMITH_FETCH_IMPLEMENTATION_KEY];
23
27
  if (!fetchImpl)
@@ -5,4 +5,5 @@
5
5
  * @param fetch The new fetch functino to use.
6
6
  */
7
7
  export declare const overrideFetchImplementation: (fetch: (...args: any[]) => any) => void;
8
+ export declare const clearFetchImplementation: () => void;
8
9
  export declare const _globalFetchImplementationIsNodeFetch: () => boolean;