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.
- package/dist/client.cjs +627 -424
- package/dist/client.d.ts +7 -1
- package/dist/client.js +626 -423
- package/dist/evaluation/evaluate_comparative.cjs +1 -0
- package/dist/evaluation/evaluate_comparative.d.ts +1 -0
- package/dist/evaluation/evaluate_comparative.js +1 -0
- package/dist/experimental/vercel/index.cjs +120 -21
- package/dist/experimental/vercel/index.d.ts +211 -1
- package/dist/experimental/vercel/index.js +119 -21
- package/dist/experimental/vercel/middleware.cjs +17 -8
- package/dist/experimental/vercel/middleware.d.ts +19 -2
- package/dist/experimental/vercel/middleware.js +17 -8
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/langchain.cjs +4 -0
- package/dist/langchain.d.ts +1 -0
- package/dist/langchain.js +4 -0
- package/dist/run_trees.cjs +17 -5
- package/dist/run_trees.d.ts +3 -1
- package/dist/run_trees.js +17 -5
- package/dist/singletons/fetch.cjs +5 -1
- package/dist/singletons/fetch.d.ts +1 -0
- package/dist/singletons/fetch.js +3 -0
- package/dist/traceable.cjs +24 -11
- package/dist/traceable.js +24 -11
- package/dist/utils/async_caller.cjs +15 -35
- package/dist/utils/async_caller.d.ts +0 -2
- package/dist/utils/async_caller.js +15 -35
- package/dist/utils/error.cjs +4 -4
- package/dist/utils/error.d.ts +1 -1
- package/dist/utils/error.js +4 -4
- package/package.json +2 -2
|
@@ -45,6 +45,7 @@ async function loadTraces(client, experiment, options) {
|
|
|
45
45
|
}
|
|
46
46
|
return results;
|
|
47
47
|
}
|
|
48
|
+
/** @deprecated Use `evaluate` and pass two experiments as targets. */
|
|
48
49
|
async function evaluateComparative(experiments, options) {
|
|
49
50
|
if (experiments.length < 2) {
|
|
50
51
|
throw new Error("Comparative evaluation requires at least 2 experiments.");
|
|
@@ -57,5 +57,6 @@ export interface ComparisonEvaluationResults {
|
|
|
57
57
|
experimentName: string;
|
|
58
58
|
results: ComparisonEvaluationResultRow[];
|
|
59
59
|
}
|
|
60
|
+
/** @deprecated Use `evaluate` and pass two experiments as targets. */
|
|
60
61
|
export declare function evaluateComparative(experiments: Array<string> | Array<Promise<ExperimentResults> | ExperimentResults>, options: EvaluateComparativeOptions): Promise<ComparisonEvaluationResults>;
|
|
61
62
|
export {};
|
|
@@ -39,6 +39,7 @@ async function loadTraces(client, experiment, options) {
|
|
|
39
39
|
}
|
|
40
40
|
return results;
|
|
41
41
|
}
|
|
42
|
+
/** @deprecated Use `evaluate` and pass two experiments as targets. */
|
|
42
43
|
export async function evaluateComparative(experiments, options) {
|
|
43
44
|
if (experiments.length < 2) {
|
|
44
45
|
throw new Error("Comparative evaluation requires at least 2 experiments.");
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.wrapAISDK = void 0;
|
|
3
|
+
exports.wrapAISDK = exports.createLangSmithProviderOptions = void 0;
|
|
4
4
|
const middleware_js_1 = require("./middleware.cjs");
|
|
5
5
|
const utils_js_1 = require("./utils.cjs");
|
|
6
6
|
const traceable_js_1 = require("../../traceable.cjs");
|
|
@@ -58,6 +58,70 @@ const _formatTracedInputs = (params) => {
|
|
|
58
58
|
return { ...rest, prompt, messages };
|
|
59
59
|
}
|
|
60
60
|
};
|
|
61
|
+
const _mergeConfig = (baseConfig, runtimeConfig) => {
|
|
62
|
+
return {
|
|
63
|
+
...baseConfig,
|
|
64
|
+
...runtimeConfig,
|
|
65
|
+
metadata: {
|
|
66
|
+
...baseConfig?.metadata,
|
|
67
|
+
...runtimeConfig?.metadata,
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
};
|
|
71
|
+
const _extractChildRunConfig = (lsConfig) => {
|
|
72
|
+
const { id, name, parent_run_id, start_time, end_time, attachments, dotted_order, processInputs, processOutputs, processChildLLMRunInputs, processChildLLMRunOutputs, ...inheritedConfig } = lsConfig ?? {};
|
|
73
|
+
const childConfig = inheritedConfig;
|
|
74
|
+
if (processChildLLMRunInputs) {
|
|
75
|
+
childConfig.processInputs = processChildLLMRunInputs;
|
|
76
|
+
}
|
|
77
|
+
if (processChildLLMRunOutputs) {
|
|
78
|
+
childConfig.processOutputs = processChildLLMRunOutputs;
|
|
79
|
+
}
|
|
80
|
+
return childConfig;
|
|
81
|
+
};
|
|
82
|
+
const _resolveConfigs = (baseLsConfig, runtimeLsConfig) => {
|
|
83
|
+
const baseChildRunConfig = _extractChildRunConfig(baseLsConfig);
|
|
84
|
+
const runtimeChildLLMRunConfig = _extractChildRunConfig(runtimeLsConfig);
|
|
85
|
+
const resolvedLsConfig = _mergeConfig(baseLsConfig, runtimeLsConfig);
|
|
86
|
+
const resolvedChildLLMRunConfig = _mergeConfig(baseChildRunConfig, runtimeChildLLMRunConfig);
|
|
87
|
+
const { processInputs: _processInputs, processOutputs: _processOutputs, ...resolvedToolConfig } = resolvedChildLLMRunConfig;
|
|
88
|
+
return {
|
|
89
|
+
resolvedLsConfig,
|
|
90
|
+
resolvedChildLLMRunConfig,
|
|
91
|
+
resolvedToolConfig,
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Wraps LangSmith config in a way that matches AI SDK provider types.
|
|
96
|
+
*
|
|
97
|
+
* ```ts
|
|
98
|
+
* import { createLangSmithProviderOptions } from "langsmith/experimental/vercel";
|
|
99
|
+
* import * as ai from "ai";
|
|
100
|
+
*
|
|
101
|
+
* const lsConfig = createLangSmithProviderOptions<typeof ai.generateText>({
|
|
102
|
+
* // Will have appropriate typing
|
|
103
|
+
* processInputs: (inputs) => {
|
|
104
|
+
* const { messages } = inputs;
|
|
105
|
+
* return {
|
|
106
|
+
* messages: messages?.map((message) => ({
|
|
107
|
+
* ...message,
|
|
108
|
+
* content: "REDACTED",
|
|
109
|
+
* })),
|
|
110
|
+
* prompt: "REDACTED",
|
|
111
|
+
* };
|
|
112
|
+
* },
|
|
113
|
+
* });
|
|
114
|
+
* ```
|
|
115
|
+
*
|
|
116
|
+
* Note: AI SDK expects only JSON values in an object for
|
|
117
|
+
* provider options, but LangSmith's config may contain non-JSON values.
|
|
118
|
+
* These are not passed to the underlying AI SDK model, so it is safe to
|
|
119
|
+
* cast the typing here.
|
|
120
|
+
*/
|
|
121
|
+
const createLangSmithProviderOptions = (lsConfig) => {
|
|
122
|
+
return (lsConfig ?? {});
|
|
123
|
+
};
|
|
124
|
+
exports.createLangSmithProviderOptions = createLangSmithProviderOptions;
|
|
61
125
|
/**
|
|
62
126
|
* Wraps Vercel AI SDK 5 functions with LangSmith tracing capabilities.
|
|
63
127
|
*
|
|
@@ -74,8 +138,7 @@ const _formatTracedInputs = (params) => {
|
|
|
74
138
|
* @returns returns.streamText - Wrapped streamText function that traces calls to LangSmith
|
|
75
139
|
* @returns returns.streamObject - Wrapped streamObject function that traces calls to LangSmith
|
|
76
140
|
*/
|
|
77
|
-
const wrapAISDK = ({ wrapLanguageModel, generateText, streamText, streamObject, generateObject, },
|
|
78
|
-
const { id, name, parent_run_id, start_time, end_time, attachments, dotted_order, ...inheritedConfig } = lsConfig ?? {};
|
|
141
|
+
const wrapAISDK = ({ wrapLanguageModel, generateText, streamText, streamObject, generateObject, }, baseLsConfig) => {
|
|
79
142
|
/**
|
|
80
143
|
* Wrapped version of AI SDK 5's generateText with LangSmith tracing.
|
|
81
144
|
*
|
|
@@ -96,6 +159,8 @@ const wrapAISDK = ({ wrapLanguageModel, generateText, streamText, streamObject,
|
|
|
96
159
|
*/
|
|
97
160
|
const wrappedGenerateText = async (...args) => {
|
|
98
161
|
const params = args[0];
|
|
162
|
+
const { langsmith: runtimeLsConfig, ...providerOptions } = params.providerOptions ?? {};
|
|
163
|
+
const { resolvedLsConfig, resolvedChildLLMRunConfig, resolvedToolConfig } = _resolveConfigs(baseLsConfig, runtimeLsConfig);
|
|
99
164
|
const traceableFunc = (0, traceable_js_1.traceable)(async (...args) => {
|
|
100
165
|
const [params, ...rest] = args;
|
|
101
166
|
const wrappedModel = wrapLanguageModel({
|
|
@@ -103,23 +168,30 @@ const wrapAISDK = ({ wrapLanguageModel, generateText, streamText, streamObject,
|
|
|
103
168
|
middleware: (0, middleware_js_1.LangSmithMiddleware)({
|
|
104
169
|
name: _getModelDisplayName(params.model),
|
|
105
170
|
modelId: _getModelId(params.model),
|
|
106
|
-
lsConfig:
|
|
171
|
+
lsConfig: resolvedChildLLMRunConfig,
|
|
107
172
|
}),
|
|
108
173
|
});
|
|
109
174
|
return generateText({
|
|
110
175
|
...params,
|
|
111
|
-
|
|
176
|
+
providerOptions,
|
|
177
|
+
tools: _wrapTools(params.tools, resolvedToolConfig),
|
|
112
178
|
model: wrappedModel,
|
|
113
179
|
}, ...rest);
|
|
114
180
|
}, {
|
|
115
181
|
name: _getModelDisplayName(params.model),
|
|
116
|
-
...
|
|
182
|
+
...resolvedLsConfig,
|
|
117
183
|
metadata: {
|
|
118
184
|
ai_sdk_method: "ai.generateText",
|
|
119
|
-
...
|
|
185
|
+
...resolvedLsConfig?.metadata,
|
|
186
|
+
},
|
|
187
|
+
processInputs: (inputs) => {
|
|
188
|
+
const inputFormatter = resolvedLsConfig?.processInputs ?? _formatTracedInputs;
|
|
189
|
+
return inputFormatter(inputs);
|
|
120
190
|
},
|
|
121
|
-
processInputs: (inputs) => _formatTracedInputs(inputs),
|
|
122
191
|
processOutputs: (outputs) => {
|
|
192
|
+
if (resolvedLsConfig?.processOutputs) {
|
|
193
|
+
return resolvedLsConfig.processOutputs(outputs);
|
|
194
|
+
}
|
|
123
195
|
if (outputs.outputs == null || typeof outputs.outputs !== "object") {
|
|
124
196
|
return outputs;
|
|
125
197
|
}
|
|
@@ -162,6 +234,8 @@ const wrapAISDK = ({ wrapLanguageModel, generateText, streamText, streamObject,
|
|
|
162
234
|
*/
|
|
163
235
|
const wrappedGenerateObject = async (...args) => {
|
|
164
236
|
const params = args[0];
|
|
237
|
+
const { langsmith: runtimeLsConfig, ...providerOptions } = params.providerOptions ?? {};
|
|
238
|
+
const { resolvedLsConfig, resolvedChildLLMRunConfig } = _resolveConfigs(baseLsConfig, runtimeLsConfig);
|
|
165
239
|
const traceableFunc = (0, traceable_js_1.traceable)(async (...args) => {
|
|
166
240
|
const [params, ...rest] = args;
|
|
167
241
|
const wrappedModel = wrapLanguageModel({
|
|
@@ -169,22 +243,29 @@ const wrapAISDK = ({ wrapLanguageModel, generateText, streamText, streamObject,
|
|
|
169
243
|
middleware: (0, middleware_js_1.LangSmithMiddleware)({
|
|
170
244
|
name: _getModelDisplayName(params.model),
|
|
171
245
|
modelId: _getModelId(params.model),
|
|
172
|
-
lsConfig:
|
|
246
|
+
lsConfig: resolvedChildLLMRunConfig,
|
|
173
247
|
}),
|
|
174
248
|
});
|
|
175
249
|
return generateObject({
|
|
176
250
|
...params,
|
|
251
|
+
providerOptions,
|
|
177
252
|
model: wrappedModel,
|
|
178
253
|
}, ...rest);
|
|
179
254
|
}, {
|
|
180
255
|
name: _getModelDisplayName(params.model),
|
|
181
|
-
...
|
|
256
|
+
...resolvedLsConfig,
|
|
182
257
|
metadata: {
|
|
183
258
|
ai_sdk_method: "ai.generateObject",
|
|
184
|
-
...
|
|
259
|
+
...resolvedLsConfig?.metadata,
|
|
260
|
+
},
|
|
261
|
+
processInputs: (inputs) => {
|
|
262
|
+
const inputFormatter = resolvedLsConfig?.processInputs ?? _formatTracedInputs;
|
|
263
|
+
return inputFormatter(inputs);
|
|
185
264
|
},
|
|
186
|
-
processInputs: (inputs) => _formatTracedInputs(inputs),
|
|
187
265
|
processOutputs: (outputs) => {
|
|
266
|
+
if (resolvedLsConfig?.processOutputs) {
|
|
267
|
+
return resolvedLsConfig.processOutputs(outputs);
|
|
268
|
+
}
|
|
188
269
|
if (outputs.outputs == null || typeof outputs.outputs !== "object") {
|
|
189
270
|
return outputs;
|
|
190
271
|
}
|
|
@@ -214,6 +295,8 @@ const wrapAISDK = ({ wrapLanguageModel, generateText, streamText, streamObject,
|
|
|
214
295
|
*/
|
|
215
296
|
const wrappedStreamText = (...args) => {
|
|
216
297
|
const params = args[0];
|
|
298
|
+
const { langsmith: runtimeLsConfig, ...providerOptions } = params.providerOptions ?? {};
|
|
299
|
+
const { resolvedLsConfig, resolvedChildLLMRunConfig, resolvedToolConfig } = _resolveConfigs(baseLsConfig, runtimeLsConfig);
|
|
217
300
|
const traceableFunc = (0, traceable_js_1.traceable)((...args) => {
|
|
218
301
|
const [params, ...rest] = args;
|
|
219
302
|
const wrappedModel = wrapLanguageModel({
|
|
@@ -221,23 +304,30 @@ const wrapAISDK = ({ wrapLanguageModel, generateText, streamText, streamObject,
|
|
|
221
304
|
middleware: (0, middleware_js_1.LangSmithMiddleware)({
|
|
222
305
|
name: _getModelDisplayName(params.model),
|
|
223
306
|
modelId: _getModelId(params.model),
|
|
224
|
-
lsConfig:
|
|
307
|
+
lsConfig: resolvedChildLLMRunConfig,
|
|
225
308
|
}),
|
|
226
309
|
});
|
|
227
310
|
return streamText({
|
|
228
311
|
...params,
|
|
229
|
-
|
|
312
|
+
providerOptions,
|
|
313
|
+
tools: _wrapTools(params.tools, resolvedToolConfig),
|
|
230
314
|
model: wrappedModel,
|
|
231
315
|
}, ...rest);
|
|
232
316
|
}, {
|
|
233
317
|
name: _getModelDisplayName(params.model),
|
|
234
|
-
...
|
|
318
|
+
...resolvedLsConfig,
|
|
235
319
|
metadata: {
|
|
236
320
|
ai_sdk_method: "ai.streamText",
|
|
237
|
-
...
|
|
321
|
+
...resolvedLsConfig?.metadata,
|
|
322
|
+
},
|
|
323
|
+
processInputs: (inputs) => {
|
|
324
|
+
const inputFormatter = resolvedLsConfig?.processInputs ?? _formatTracedInputs;
|
|
325
|
+
return inputFormatter(inputs);
|
|
238
326
|
},
|
|
239
|
-
processInputs: (inputs) => _formatTracedInputs(inputs),
|
|
240
327
|
processOutputs: async (outputs) => {
|
|
328
|
+
if (resolvedLsConfig?.processOutputs) {
|
|
329
|
+
return resolvedLsConfig.processOutputs(outputs);
|
|
330
|
+
}
|
|
241
331
|
if (outputs.outputs == null || typeof outputs.outputs !== "object") {
|
|
242
332
|
return outputs;
|
|
243
333
|
}
|
|
@@ -274,6 +364,8 @@ const wrapAISDK = ({ wrapLanguageModel, generateText, streamText, streamObject,
|
|
|
274
364
|
*/
|
|
275
365
|
const wrappedStreamObject = (...args) => {
|
|
276
366
|
const params = args[0];
|
|
367
|
+
const { langsmith: runtimeLsConfig, ...providerOptions } = params.providerOptions ?? {};
|
|
368
|
+
const { resolvedLsConfig, resolvedChildLLMRunConfig } = _resolveConfigs(baseLsConfig, runtimeLsConfig);
|
|
277
369
|
const traceableFunc = (0, traceable_js_1.traceable)((...args) => {
|
|
278
370
|
const [params, ...rest] = args;
|
|
279
371
|
const wrappedModel = wrapLanguageModel({
|
|
@@ -281,22 +373,29 @@ const wrapAISDK = ({ wrapLanguageModel, generateText, streamText, streamObject,
|
|
|
281
373
|
middleware: (0, middleware_js_1.LangSmithMiddleware)({
|
|
282
374
|
name: _getModelDisplayName(params.model),
|
|
283
375
|
modelId: _getModelId(params.model),
|
|
284
|
-
lsConfig:
|
|
376
|
+
lsConfig: resolvedChildLLMRunConfig,
|
|
285
377
|
}),
|
|
286
378
|
});
|
|
287
379
|
return streamObject({
|
|
288
380
|
...params,
|
|
381
|
+
providerOptions,
|
|
289
382
|
model: wrappedModel,
|
|
290
383
|
}, ...rest);
|
|
291
384
|
}, {
|
|
292
385
|
name: _getModelDisplayName(params.model),
|
|
293
|
-
...
|
|
386
|
+
...resolvedLsConfig,
|
|
294
387
|
metadata: {
|
|
295
388
|
ai_sdk_method: "ai.streamObject",
|
|
296
|
-
...
|
|
389
|
+
...resolvedLsConfig?.metadata,
|
|
390
|
+
},
|
|
391
|
+
processInputs: (inputs) => {
|
|
392
|
+
const inputFormatter = resolvedLsConfig?.processInputs ?? _formatTracedInputs;
|
|
393
|
+
return inputFormatter(inputs);
|
|
297
394
|
},
|
|
298
|
-
processInputs: (inputs) => _formatTracedInputs(inputs),
|
|
299
395
|
processOutputs: async (outputs) => {
|
|
396
|
+
if (resolvedLsConfig?.processOutputs) {
|
|
397
|
+
return resolvedLsConfig.processOutputs(outputs);
|
|
398
|
+
}
|
|
300
399
|
if (outputs.outputs == null || typeof outputs.outputs !== "object") {
|
|
301
400
|
return outputs;
|
|
302
401
|
}
|
|
@@ -1,4 +1,214 @@
|
|
|
1
|
+
import type { JSONValue } from "ai";
|
|
2
|
+
import type { LanguageModelV2, LanguageModelV2CallOptions } from "@ai-sdk/provider";
|
|
3
|
+
import { type AggregatedDoStreamOutput } from "./middleware.js";
|
|
1
4
|
import { RunTreeConfig } from "../../run_trees.js";
|
|
5
|
+
export type { AggregatedDoStreamOutput };
|
|
6
|
+
export type WrapAISDKConfig<T extends (...args: any[]) => any = (...args: any[]) => any> = Partial<Omit<RunTreeConfig, "inputs" | "outputs" | "run_type" | "child_runs" | "parent_run" | "error" | "serialized">> & {
|
|
7
|
+
/**
|
|
8
|
+
* Apply transformations to AI SDK inputs before logging.
|
|
9
|
+
* This function should NOT mutate the inputs.
|
|
10
|
+
* Receives both "raw" and LangSmith-suggested "formatted" inputs,
|
|
11
|
+
* and should combine them into a single LangSmith-formatted input.
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import {
|
|
15
|
+
* wrapAISDK,
|
|
16
|
+
* createLangSmithProviderOptions,
|
|
17
|
+
* } from "langsmith/experimental/vercel";
|
|
18
|
+
* import * as ai from "ai";
|
|
19
|
+
* import { openai } from "@ai-sdk/openai";
|
|
20
|
+
*
|
|
21
|
+
* const { generateText } = wrapAISDK(ai);
|
|
22
|
+
*
|
|
23
|
+
* const lsConfig = createLangSmithProviderOptions<typeof ai.generateText>({
|
|
24
|
+
* processInputs: (inputs) => {
|
|
25
|
+
* const { messages } = inputs;
|
|
26
|
+
* return {
|
|
27
|
+
* messages: messages?.map((message) => ({
|
|
28
|
+
* providerMetadata: message.providerOptions,
|
|
29
|
+
* role: "assistant",
|
|
30
|
+
* content: "REDACTED",
|
|
31
|
+
* })),
|
|
32
|
+
* prompt: "REDACTED",
|
|
33
|
+
* };
|
|
34
|
+
* },
|
|
35
|
+
* });
|
|
36
|
+
* const { text } = await generateText({
|
|
37
|
+
* model: openai("gpt-5-nano"),
|
|
38
|
+
* prompt: "What is the capital of France?",
|
|
39
|
+
* providerOptions: {
|
|
40
|
+
* langsmith: lsConfig,
|
|
41
|
+
* },
|
|
42
|
+
* });
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* This function is not inherited by nested LLM runs or tool calls.
|
|
46
|
+
* Pass `processChildLLMRunInputs` to override child LLM run
|
|
47
|
+
* input processing or wrap your tool's `execute` method in a
|
|
48
|
+
* separate `traceable` for tool calls.
|
|
49
|
+
*
|
|
50
|
+
* @param inputs Key-value map of the function inputs.
|
|
51
|
+
* @param inputs.formatted - Inputs formatted for LangSmith.
|
|
52
|
+
* @param inputs.raw - Raw inputs from the AI SDK.
|
|
53
|
+
* @returns A single combined key-value map of processed inputs.
|
|
54
|
+
*/
|
|
55
|
+
processInputs?: (inputs: Parameters<T>[0]) => Record<string, unknown>;
|
|
56
|
+
/**
|
|
57
|
+
* Apply transformations to AI SDK outputs before logging.
|
|
58
|
+
* This function should NOT mutate the outputs.
|
|
59
|
+
* Receives both "raw" and LangSmith-suggested "formatted" outputs,
|
|
60
|
+
* and should combine them into a single LangSmith-formatted output.
|
|
61
|
+
*
|
|
62
|
+
* ```ts
|
|
63
|
+
* import {
|
|
64
|
+
* wrapAISDK,
|
|
65
|
+
* createLangSmithProviderOptions,
|
|
66
|
+
* } from "langsmith/experimental/vercel";
|
|
67
|
+
* import * as ai from "ai";
|
|
68
|
+
* import { openai } from "@ai-sdk/openai";
|
|
69
|
+
*
|
|
70
|
+
* const { generateText } = wrapAISDK(ai);
|
|
71
|
+
*
|
|
72
|
+
* const lsConfig = createLangSmithProviderOptions<typeof ai.generateText>({
|
|
73
|
+
* processOutputs: (outputs) => {
|
|
74
|
+
* return {
|
|
75
|
+
* providerMetadata: outputs.providerMetadata,
|
|
76
|
+
* role: "assistant",
|
|
77
|
+
* content: "REDACTED",
|
|
78
|
+
* };
|
|
79
|
+
* },
|
|
80
|
+
* });
|
|
81
|
+
* const { text } = await generateText({
|
|
82
|
+
* model: openai("gpt-5-nano"),
|
|
83
|
+
* prompt: "What is the capital of France?",
|
|
84
|
+
* providerOptions: {
|
|
85
|
+
* langsmith: lsConfig,
|
|
86
|
+
* },
|
|
87
|
+
* });
|
|
88
|
+
* ```
|
|
89
|
+
*
|
|
90
|
+
* This function is not inherited by nested LLM runs or tool calls.
|
|
91
|
+
* Pass `processChildLLMRunOutputs` to override child LLM run
|
|
92
|
+
* output processing or wrap your tool's `execute` method in a
|
|
93
|
+
* separate `traceable` for tool calls.
|
|
94
|
+
*
|
|
95
|
+
* @param outputs Key-value map of the function inputs.
|
|
96
|
+
* @param outputs.formatted - Outputs formatted for LangSmith.
|
|
97
|
+
* @param outputs.raw - Raw outputs from the AI SDK.
|
|
98
|
+
* @returns A single combined key-value map of processed outputs.
|
|
99
|
+
*/
|
|
100
|
+
processOutputs?: (outputs: Awaited<ReturnType<T>>) => Record<string, unknown>;
|
|
101
|
+
/**
|
|
102
|
+
* Apply transformations to AI SDK child LLM run inputs before logging.
|
|
103
|
+
* This function should NOT mutate the inputs.
|
|
104
|
+
* Receives both "raw" and LangSmith-suggested "formatted" inputs,
|
|
105
|
+
* and should combine them into a single LangSmith-formatted input.
|
|
106
|
+
*
|
|
107
|
+
* ```ts
|
|
108
|
+
* import {
|
|
109
|
+
* wrapAISDK,
|
|
110
|
+
* createLangSmithProviderOptions,
|
|
111
|
+
* } from "langsmith/experimental/vercel";
|
|
112
|
+
* import * as ai from "ai";
|
|
113
|
+
* import { openai } from "@ai-sdk/openai";
|
|
114
|
+
*
|
|
115
|
+
* const { generateText } = wrapAISDK(ai);
|
|
116
|
+
*
|
|
117
|
+
* const lsConfig = createLangSmithProviderOptions<typeof ai.generateText>({
|
|
118
|
+
* processChildLLMRunInputs: (inputs) => {
|
|
119
|
+
* const { prompt } = inputs;
|
|
120
|
+
* return {
|
|
121
|
+
* messages: prompt.map((message) => ({
|
|
122
|
+
* ...message,
|
|
123
|
+
* content: "REDACTED CHILD INPUTS",
|
|
124
|
+
* })),
|
|
125
|
+
* };
|
|
126
|
+
* },
|
|
127
|
+
* });
|
|
128
|
+
* const { text } = await generateText({
|
|
129
|
+
* model: openai("gpt-5-nano"),
|
|
130
|
+
* prompt: "What is the capital of France?",
|
|
131
|
+
* providerOptions: {
|
|
132
|
+
* langsmith: lsConfig,
|
|
133
|
+
* },
|
|
134
|
+
* });
|
|
135
|
+
* ```
|
|
136
|
+
*
|
|
137
|
+
* @param inputs Key-value map of the function inputs.
|
|
138
|
+
* @param inputs.formatted - Inputs formatted for LangSmith.
|
|
139
|
+
* @param inputs.raw - Raw inputs from the AI SDK.
|
|
140
|
+
* @returns A single combined key-value map of processed inputs.
|
|
141
|
+
*/
|
|
142
|
+
processChildLLMRunInputs?: (inputs: LanguageModelV2CallOptions) => Record<string, unknown>;
|
|
143
|
+
/**
|
|
144
|
+
* Apply transformations to AI SDK child LLM run outputs before logging.
|
|
145
|
+
* This function should NOT mutate the outputs.
|
|
146
|
+
* Receives both "raw" and LangSmith-suggested "formatted" outputs,
|
|
147
|
+
* and should combine them into a single LangSmith-formatted output.
|
|
148
|
+
*
|
|
149
|
+
* ```ts
|
|
150
|
+
* import {
|
|
151
|
+
* wrapAISDK,
|
|
152
|
+
* createLangSmithProviderOptions,
|
|
153
|
+
* } from "langsmith/experimental/vercel";
|
|
154
|
+
* import * as ai from "ai";
|
|
155
|
+
* import { openai } from "@ai-sdk/openai";
|
|
156
|
+
*
|
|
157
|
+
* const { generateText } = wrapAISDK(ai);
|
|
158
|
+
*
|
|
159
|
+
* const lsConfig = createLangSmithProviderOptions<typeof ai.generateText>({
|
|
160
|
+
* processChildLLMRunOutputs: (outputs) => {
|
|
161
|
+
* return {
|
|
162
|
+
* providerMetadata: outputs.providerMetadata,
|
|
163
|
+
* content: "REDACTED CHILD OUTPUTS",
|
|
164
|
+
* role: "assistant",
|
|
165
|
+
* };
|
|
166
|
+
* },
|
|
167
|
+
* });
|
|
168
|
+
* const { text } = await generateText({
|
|
169
|
+
* model: openai("gpt-5-nano"),
|
|
170
|
+
* prompt: "What is the capital of France?",
|
|
171
|
+
* providerOptions: {
|
|
172
|
+
* langsmith: lsConfig,
|
|
173
|
+
* },
|
|
174
|
+
* });
|
|
175
|
+
* ```
|
|
176
|
+
*
|
|
177
|
+
* @param outputs Key-value map of the function inputs.
|
|
178
|
+
* @param outputs.formatted - Outputs formatted for LangSmith.
|
|
179
|
+
* @param outputs.raw - Raw outputs from the AI SDK.
|
|
180
|
+
* @returns A single combined key-value map of processed outputs.
|
|
181
|
+
*/
|
|
182
|
+
processChildLLMRunOutputs?: (outputs: "fullStream" extends keyof Awaited<ReturnType<T>> ? AggregatedDoStreamOutput : Awaited<ReturnType<LanguageModelV2["doGenerate"]>>) => Record<string, unknown>;
|
|
183
|
+
};
|
|
184
|
+
/**
|
|
185
|
+
* Wraps LangSmith config in a way that matches AI SDK provider types.
|
|
186
|
+
*
|
|
187
|
+
* ```ts
|
|
188
|
+
* import { createLangSmithProviderOptions } from "langsmith/experimental/vercel";
|
|
189
|
+
* import * as ai from "ai";
|
|
190
|
+
*
|
|
191
|
+
* const lsConfig = createLangSmithProviderOptions<typeof ai.generateText>({
|
|
192
|
+
* // Will have appropriate typing
|
|
193
|
+
* processInputs: (inputs) => {
|
|
194
|
+
* const { messages } = inputs;
|
|
195
|
+
* return {
|
|
196
|
+
* messages: messages?.map((message) => ({
|
|
197
|
+
* ...message,
|
|
198
|
+
* content: "REDACTED",
|
|
199
|
+
* })),
|
|
200
|
+
* prompt: "REDACTED",
|
|
201
|
+
* };
|
|
202
|
+
* },
|
|
203
|
+
* });
|
|
204
|
+
* ```
|
|
205
|
+
*
|
|
206
|
+
* Note: AI SDK expects only JSON values in an object for
|
|
207
|
+
* provider options, but LangSmith's config may contain non-JSON values.
|
|
208
|
+
* These are not passed to the underlying AI SDK model, so it is safe to
|
|
209
|
+
* cast the typing here.
|
|
210
|
+
*/
|
|
211
|
+
export declare const createLangSmithProviderOptions: <T extends (...args: any[]) => any>(lsConfig?: WrapAISDKConfig<T>) => Record<string, JSONValue>;
|
|
2
212
|
/**
|
|
3
213
|
* Wraps Vercel AI SDK 5 functions with LangSmith tracing capabilities.
|
|
4
214
|
*
|
|
@@ -21,7 +231,7 @@ declare const wrapAISDK: <GenerateTextType extends (...args: any[]) => any, Stre
|
|
|
21
231
|
streamText: StreamTextType;
|
|
22
232
|
streamObject: StreamObjectType;
|
|
23
233
|
generateObject: GenerateObjectType;
|
|
24
|
-
},
|
|
234
|
+
}, baseLsConfig?: WrapAISDKConfig) => {
|
|
25
235
|
generateText: GenerateTextType;
|
|
26
236
|
generateObject: GenerateObjectType;
|
|
27
237
|
streamText: StreamTextType;
|