@langchain/core 0.3.57 → 0.3.58
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/language_models/base.d.ts +14 -7
- package/dist/language_models/chat_models.cjs +5 -5
- package/dist/language_models/chat_models.d.ts +9 -3
- package/dist/language_models/chat_models.js +5 -5
- package/dist/output_parsers/openai_functions/json_output_functions_parsers.d.ts +1 -1
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.cjs +3 -2
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.d.ts +19 -5
- package/dist/output_parsers/openai_tools/json_output_tools_parsers.js +3 -2
- package/dist/output_parsers/structured.cjs +8 -7
- package/dist/output_parsers/structured.d.ts +10 -9
- package/dist/output_parsers/structured.js +6 -5
- package/dist/runnables/base.cjs +11 -12
- package/dist/runnables/base.d.ts +9 -9
- package/dist/runnables/base.js +8 -9
- package/dist/runnables/graph.cjs +2 -2
- package/dist/runnables/graph.js +2 -2
- package/dist/runnables/types.d.ts +2 -2
- package/dist/tools/index.cjs +9 -11
- package/dist/tools/index.d.ts +11 -8
- package/dist/tools/index.js +7 -9
- package/dist/tools/types.cjs +2 -2
- package/dist/tools/types.d.ts +11 -13
- package/dist/tools/types.js +2 -2
- package/dist/utils/env.cjs +1 -3
- package/dist/utils/env.js +1 -3
- package/dist/utils/function_calling.cjs +4 -6
- package/dist/utils/function_calling.d.ts +4 -6
- package/dist/utils/function_calling.js +4 -6
- package/dist/utils/json_schema.cjs +6 -2
- package/dist/utils/json_schema.d.ts +3 -2
- package/dist/utils/json_schema.js +6 -2
- package/dist/utils/testing/index.d.ts +8 -7
- package/dist/utils/types/index.cjs +1 -1
- package/dist/utils/types/index.d.ts +1 -1
- package/dist/utils/types/index.js +1 -1
- package/dist/utils/types/zod.cjs +424 -0
- package/dist/utils/types/zod.d.ts +173 -0
- package/dist/utils/types/zod.js +404 -0
- package/package.json +2 -2
- package/dist/utils/types/is_zod_schema.cjs +0 -38
- package/dist/utils/types/is_zod_schema.d.ts +0 -8
- package/dist/utils/types/is_zod_schema.js +0 -34
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { TiktokenModel } from "js-tiktoken/lite";
|
|
2
|
-
import {
|
|
2
|
+
import type { ZodType as ZodTypeV3 } from "zod/v3";
|
|
3
|
+
import type { $ZodType as ZodTypeV4 } from "zod/v4/core";
|
|
3
4
|
import { type BaseCache } from "../caches/base.js";
|
|
4
5
|
import { type BasePromptValueInterface } from "../prompt_values.js";
|
|
5
6
|
import { type BaseMessage, type BaseMessageLike, type MessageContent } from "../messages/base.js";
|
|
@@ -9,6 +10,7 @@ import { AsyncCaller, AsyncCallerParams } from "../utils/async_caller.js";
|
|
|
9
10
|
import { Runnable, type RunnableInterface } from "../runnables/base.js";
|
|
10
11
|
import { RunnableConfig } from "../runnables/config.js";
|
|
11
12
|
import { JSONSchema } from "../utils/json_schema.js";
|
|
13
|
+
import { InferInteropZodOutput, InteropZodObject, InteropZodType } from "../utils/types/zod.js";
|
|
12
14
|
export declare const getModelNameForTiktoken: (modelName: string) => TiktokenModel;
|
|
13
15
|
export declare const getEmbeddingContextSize: (modelName?: string) => number;
|
|
14
16
|
export declare const getModelContextSize: (modelName: string) => number;
|
|
@@ -103,7 +105,7 @@ export interface BaseFunctionCallOptions extends BaseLanguageModelCallOptions {
|
|
|
103
105
|
functions?: FunctionDefinition[];
|
|
104
106
|
}
|
|
105
107
|
export type BaseLanguageModelInput = BasePromptValueInterface | string | BaseMessageLike[];
|
|
106
|
-
export type StructuredOutputType =
|
|
108
|
+
export type StructuredOutputType = InferInteropZodOutput<InteropZodObject>;
|
|
107
109
|
export type StructuredOutputMethodOptions<IncludeRaw extends boolean = false> = {
|
|
108
110
|
name?: string;
|
|
109
111
|
method?: "functionCalling" | "jsonMode" | "jsonSchema" | string;
|
|
@@ -114,7 +116,7 @@ export type StructuredOutputMethodOptions<IncludeRaw extends boolean = false> =
|
|
|
114
116
|
/** @deprecated Use StructuredOutputMethodOptions instead */
|
|
115
117
|
export type StructuredOutputMethodParams<RunOutput, IncludeRaw extends boolean = false> = {
|
|
116
118
|
/** @deprecated Pass schema in as the first argument */
|
|
117
|
-
schema:
|
|
119
|
+
schema: InteropZodType<RunOutput> | Record<string, any>;
|
|
118
120
|
name?: string;
|
|
119
121
|
method?: "functionCalling" | "jsonMode";
|
|
120
122
|
includeRaw?: IncludeRaw;
|
|
@@ -192,8 +194,13 @@ export declare abstract class BaseLanguageModel<RunOutput = any, CallOptions ext
|
|
|
192
194
|
* Load an LLM from a json-like object describing it.
|
|
193
195
|
*/
|
|
194
196
|
static deserialize(_data: SerializedLLM): Promise<BaseLanguageModel>;
|
|
195
|
-
withStructuredOutput?<RunOutput extends Record<string, any> = Record<string, any>>(schema:
|
|
196
|
-
withStructuredOutput?<RunOutput extends Record<string, any> = Record<string, any>>(schema:
|
|
197
|
+
withStructuredOutput?<RunOutput extends Record<string, any> = Record<string, any>>(schema: ZodTypeV3<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;
|
|
198
|
+
withStructuredOutput?<RunOutput extends Record<string, any> = Record<string, any>>(schema: ZodTypeV3<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {
|
|
199
|
+
raw: BaseMessage;
|
|
200
|
+
parsed: RunOutput;
|
|
201
|
+
}>;
|
|
202
|
+
withStructuredOutput?<RunOutput extends Record<string, any> = Record<string, any>>(schema: ZodTypeV4<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;
|
|
203
|
+
withStructuredOutput?<RunOutput extends Record<string, any> = Record<string, any>>(schema: ZodTypeV4<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {
|
|
197
204
|
raw: BaseMessage;
|
|
198
205
|
parsed: RunOutput;
|
|
199
206
|
}>;
|
|
@@ -203,14 +210,14 @@ export declare abstract class BaseLanguageModel<RunOutput = any, CallOptions ext
|
|
|
203
210
|
* @template {BaseLanguageModelInput} RunInput The input type for the Runnable, expected to be the same input for the LLM.
|
|
204
211
|
* @template {Record<string, any>} RunOutput The output type for the Runnable, expected to be a Zod schema object for structured output validation.
|
|
205
212
|
*
|
|
206
|
-
* @param {
|
|
213
|
+
* @param {InteropZodType<RunOutput>} schema The schema for the structured output. Either as a Zod schema or a valid JSON schema object.
|
|
207
214
|
* If a Zod schema is passed, the returned attributes will be validated, whereas with JSON schema they will not be.
|
|
208
215
|
* @param {string} name The name of the function to call.
|
|
209
216
|
* @param {"functionCalling" | "jsonMode"} [method=functionCalling] The method to use for getting the structured output. Defaults to "functionCalling".
|
|
210
217
|
* @param {boolean | undefined} [includeRaw=false] Whether to include the raw output in the result. Defaults to false.
|
|
211
218
|
* @returns {Runnable<RunInput, RunOutput> | Runnable<RunInput, { raw: BaseMessage; parsed: RunOutput }>} A new runnable that calls the LLM with structured output.
|
|
212
219
|
*/
|
|
213
|
-
withStructuredOutput?<RunOutput extends Record<string, any> = Record<string, any>>(schema:
|
|
220
|
+
withStructuredOutput?<RunOutput extends Record<string, any> = Record<string, any>>(schema: InteropZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<boolean>): Runnable<BaseLanguageModelInput, RunOutput> | Runnable<BaseLanguageModelInput, {
|
|
214
221
|
raw: BaseMessage;
|
|
215
222
|
parsed: RunOutput;
|
|
216
223
|
}>;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SimpleChatModel = exports.BaseChatModel = exports.createChatMessageChunkEncoderStream = void 0;
|
|
4
|
-
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
5
4
|
const index_js_1 = require("../messages/index.cjs");
|
|
6
5
|
const outputs_js_1 = require("../outputs.cjs");
|
|
7
6
|
const base_js_1 = require("./base.cjs");
|
|
@@ -9,8 +8,9 @@ const manager_js_1 = require("../callbacks/manager.cjs");
|
|
|
9
8
|
const base_js_2 = require("../runnables/base.cjs");
|
|
10
9
|
const stream_js_1 = require("../utils/stream.cjs");
|
|
11
10
|
const passthrough_js_1 = require("../runnables/passthrough.cjs");
|
|
12
|
-
const
|
|
11
|
+
const zod_js_1 = require("../utils/types/zod.cjs");
|
|
13
12
|
const base_js_3 = require("../callbacks/base.cjs");
|
|
13
|
+
const json_schema_js_1 = require("../utils/json_schema.cjs");
|
|
14
14
|
/**
|
|
15
15
|
* Creates a transform stream for encoding chat message chunks.
|
|
16
16
|
* @deprecated Use {@link BytesOutputParser} instead
|
|
@@ -534,7 +534,7 @@ class BaseChatModel extends base_js_1.BaseLanguageModel {
|
|
|
534
534
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
535
535
|
const schema = outputSchema;
|
|
536
536
|
const name = config?.name;
|
|
537
|
-
const description = schema
|
|
537
|
+
const description = (0, zod_js_1.getSchemaDescription)(schema) ?? "A function available to call.";
|
|
538
538
|
const method = config?.method;
|
|
539
539
|
const includeRaw = config?.includeRaw;
|
|
540
540
|
if (method === "jsonMode") {
|
|
@@ -542,14 +542,14 @@ class BaseChatModel extends base_js_1.BaseLanguageModel {
|
|
|
542
542
|
}
|
|
543
543
|
let functionName = name ?? "extract";
|
|
544
544
|
let tools;
|
|
545
|
-
if ((0,
|
|
545
|
+
if ((0, zod_js_1.isInteropZodSchema)(schema)) {
|
|
546
546
|
tools = [
|
|
547
547
|
{
|
|
548
548
|
type: "function",
|
|
549
549
|
function: {
|
|
550
550
|
name: functionName,
|
|
551
551
|
description,
|
|
552
|
-
parameters: (0,
|
|
552
|
+
parameters: (0, json_schema_js_1.toJsonSchema)(schema),
|
|
553
553
|
},
|
|
554
554
|
},
|
|
555
555
|
];
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { ZodType as ZodTypeV3 } from "zod/v3";
|
|
2
|
+
import type { $ZodType as ZodTypeV4 } from "zod/v4/core";
|
|
2
3
|
import { type BaseMessage, BaseMessageChunk, type BaseMessageLike, AIMessageChunk } from "../messages/index.js";
|
|
3
4
|
import type { BasePromptValueInterface } from "../prompt_values.js";
|
|
4
5
|
import { LLMResult, ChatGenerationChunk, type ChatResult, type Generation } from "../outputs.js";
|
|
@@ -183,8 +184,13 @@ export declare abstract class BaseChatModel<CallOptions extends BaseChatModelCal
|
|
|
183
184
|
* @returns A Promise that resolves to a string.
|
|
184
185
|
*/
|
|
185
186
|
predict(text: string, options?: string[] | CallOptions, callbacks?: Callbacks): Promise<string>;
|
|
186
|
-
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema:
|
|
187
|
-
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema:
|
|
187
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: ZodTypeV4<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;
|
|
188
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: ZodTypeV4<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {
|
|
189
|
+
raw: BaseMessage;
|
|
190
|
+
parsed: RunOutput;
|
|
191
|
+
}>;
|
|
192
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: ZodTypeV3<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<false>): Runnable<BaseLanguageModelInput, RunOutput>;
|
|
193
|
+
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: ZodTypeV3<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<true>): Runnable<BaseLanguageModelInput, {
|
|
188
194
|
raw: BaseMessage;
|
|
189
195
|
parsed: RunOutput;
|
|
190
196
|
}>;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
2
1
|
import { AIMessage, HumanMessage, coerceMessageLikeToMessage, isAIMessageChunk, isBaseMessage, isAIMessage, convertToOpenAIImageBlock, isURLContentBlock, isBase64ContentBlock, } from "../messages/index.js";
|
|
3
2
|
import { RUN_KEY, } from "../outputs.js";
|
|
4
3
|
import { BaseLanguageModel, } from "./base.js";
|
|
@@ -6,8 +5,9 @@ import { CallbackManager, } from "../callbacks/manager.js";
|
|
|
6
5
|
import { RunnableLambda, RunnableSequence, } from "../runnables/base.js";
|
|
7
6
|
import { concat } from "../utils/stream.js";
|
|
8
7
|
import { RunnablePassthrough } from "../runnables/passthrough.js";
|
|
9
|
-
import {
|
|
8
|
+
import { getSchemaDescription, isInteropZodSchema, } from "../utils/types/zod.js";
|
|
10
9
|
import { callbackHandlerPrefersStreaming } from "../callbacks/base.js";
|
|
10
|
+
import { toJsonSchema } from "../utils/json_schema.js";
|
|
11
11
|
/**
|
|
12
12
|
* Creates a transform stream for encoding chat message chunks.
|
|
13
13
|
* @deprecated Use {@link BytesOutputParser} instead
|
|
@@ -530,7 +530,7 @@ export class BaseChatModel extends BaseLanguageModel {
|
|
|
530
530
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
531
531
|
const schema = outputSchema;
|
|
532
532
|
const name = config?.name;
|
|
533
|
-
const description = schema
|
|
533
|
+
const description = getSchemaDescription(schema) ?? "A function available to call.";
|
|
534
534
|
const method = config?.method;
|
|
535
535
|
const includeRaw = config?.includeRaw;
|
|
536
536
|
if (method === "jsonMode") {
|
|
@@ -538,14 +538,14 @@ export class BaseChatModel extends BaseLanguageModel {
|
|
|
538
538
|
}
|
|
539
539
|
let functionName = name ?? "extract";
|
|
540
540
|
let tools;
|
|
541
|
-
if (
|
|
541
|
+
if (isInteropZodSchema(schema)) {
|
|
542
542
|
tools = [
|
|
543
543
|
{
|
|
544
544
|
type: "function",
|
|
545
545
|
function: {
|
|
546
546
|
name: functionName,
|
|
547
547
|
description,
|
|
548
|
-
parameters:
|
|
548
|
+
parameters: toJsonSchema(schema),
|
|
549
549
|
},
|
|
550
550
|
},
|
|
551
551
|
];
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { JsonSchema7ObjectType } from "zod-to-json-schema";
|
|
2
1
|
import { Optional } from "../../types/type-utils.js";
|
|
3
2
|
import { Generation, ChatGeneration } from "../../outputs.js";
|
|
4
3
|
import { BaseLLMOutputParser } from "../base.js";
|
|
5
4
|
import { BaseCumulativeTransformOutputParser, BaseCumulativeTransformOutputParserInput } from "../transform.js";
|
|
5
|
+
import { JsonSchema7ObjectType } from "../../utils/json_schema.js";
|
|
6
6
|
import { type Operation as JSONPatchOperation } from "../../utils/json_patch.js";
|
|
7
7
|
/**
|
|
8
8
|
* Represents optional parameters for a function in a JSON Schema.
|
|
@@ -5,6 +5,7 @@ const base_js_1 = require("../base.cjs");
|
|
|
5
5
|
const json_js_1 = require("../json.cjs");
|
|
6
6
|
const transform_js_1 = require("../transform.cjs");
|
|
7
7
|
const ai_js_1 = require("../../messages/ai.cjs");
|
|
8
|
+
const zod_js_1 = require("../../utils/types/zod.cjs");
|
|
8
9
|
function parseToolCall(
|
|
9
10
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
11
|
rawToolCall, options) {
|
|
@@ -215,12 +216,12 @@ class JsonOutputKeyToolsParser extends JsonOutputToolsParser {
|
|
|
215
216
|
if (this.zodSchema === undefined) {
|
|
216
217
|
return result;
|
|
217
218
|
}
|
|
218
|
-
const zodParsedResult = await this.zodSchema
|
|
219
|
+
const zodParsedResult = await (0, zod_js_1.interopSafeParseAsync)(this.zodSchema, result);
|
|
219
220
|
if (zodParsedResult.success) {
|
|
220
221
|
return zodParsedResult.data;
|
|
221
222
|
}
|
|
222
223
|
else {
|
|
223
|
-
throw new base_js_1.OutputParserException(`Failed to parse. Text: "${JSON.stringify(result, null, 2)}". Error: ${JSON.stringify(zodParsedResult.error
|
|
224
|
+
throw new base_js_1.OutputParserException(`Failed to parse. Text: "${JSON.stringify(result, null, 2)}". Error: ${JSON.stringify(zodParsedResult.error?.issues)}`, JSON.stringify(result, null, 2));
|
|
224
225
|
}
|
|
225
226
|
}
|
|
226
227
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import
|
|
1
|
+
import type * as z3 from "zod/v3";
|
|
2
|
+
import type * as z4 from "zod/v4/core";
|
|
2
3
|
import { ChatGeneration, ChatGenerationChunk } from "../../outputs.js";
|
|
3
4
|
import { InvalidToolCall, ToolCall } from "../../messages/tool.js";
|
|
4
5
|
import { BaseCumulativeTransformOutputParser, BaseCumulativeTransformOutputParserInput } from "../transform.js";
|
|
6
|
+
import { type InteropZodType } from "../../utils/types/zod.js";
|
|
5
7
|
export type ParsedToolCall = {
|
|
6
8
|
id?: string;
|
|
7
9
|
type: string;
|
|
@@ -52,11 +54,20 @@ export declare class JsonOutputToolsParser<T> extends BaseCumulativeTransformOut
|
|
|
52
54
|
*/
|
|
53
55
|
parsePartialResult(generations: ChatGenerationChunk[] | ChatGeneration[], partial?: boolean): Promise<any>;
|
|
54
56
|
}
|
|
55
|
-
|
|
57
|
+
type JsonOutputKeyToolsParserParamsBase = {
|
|
56
58
|
keyName: string;
|
|
57
59
|
returnSingle?: boolean;
|
|
58
|
-
zodSchema?: z.ZodType<T>;
|
|
59
60
|
} & JsonOutputToolsParserParams;
|
|
61
|
+
type JsonOutputKeyToolsParserParamsV3<T extends Record<string, any> = Record<string, any>> = {
|
|
62
|
+
zodSchema?: z3.ZodType<T>;
|
|
63
|
+
} & JsonOutputKeyToolsParserParamsBase;
|
|
64
|
+
type JsonOutputKeyToolsParserParamsV4<T extends Record<string, any> = Record<string, any>> = {
|
|
65
|
+
zodSchema?: z4.$ZodType<T, T>;
|
|
66
|
+
} & JsonOutputKeyToolsParserParamsBase;
|
|
67
|
+
export type JsonOutputKeyToolsParserParamsInterop<T extends Record<string, any> = Record<string, any>> = {
|
|
68
|
+
zodSchema?: InteropZodType<T>;
|
|
69
|
+
} & JsonOutputKeyToolsParserParamsBase;
|
|
70
|
+
export type JsonOutputKeyToolsParserParams<T extends Record<string, any> = Record<string, any>> = JsonOutputKeyToolsParserParamsV3<T>;
|
|
60
71
|
/**
|
|
61
72
|
* Class for parsing the output of a tool-calling LLM into a JSON object if you are
|
|
62
73
|
* expecting only a single tool to be called.
|
|
@@ -70,9 +81,12 @@ export declare class JsonOutputKeyToolsParser<T extends Record<string, any> = Re
|
|
|
70
81
|
keyName: string;
|
|
71
82
|
/** Whether to return only the first tool call. */
|
|
72
83
|
returnSingle: boolean;
|
|
73
|
-
zodSchema?:
|
|
74
|
-
constructor(params:
|
|
84
|
+
zodSchema?: InteropZodType<T>;
|
|
85
|
+
constructor(params: JsonOutputKeyToolsParserParamsV3<T>);
|
|
86
|
+
constructor(params: JsonOutputKeyToolsParserParamsV4<T>);
|
|
87
|
+
constructor(params: JsonOutputKeyToolsParserParamsInterop<T>);
|
|
75
88
|
protected _validateResult(result: unknown): Promise<T>;
|
|
76
89
|
parsePartialResult(generations: ChatGeneration[]): Promise<any>;
|
|
77
90
|
parseResult(generations: ChatGeneration[]): Promise<any>;
|
|
78
91
|
}
|
|
92
|
+
export {};
|
|
@@ -2,6 +2,7 @@ import { OutputParserException } from "../base.js";
|
|
|
2
2
|
import { parsePartialJson } from "../json.js";
|
|
3
3
|
import { BaseCumulativeTransformOutputParser, } from "../transform.js";
|
|
4
4
|
import { isAIMessage } from "../../messages/ai.js";
|
|
5
|
+
import { interopSafeParseAsync, } from "../../utils/types/zod.js";
|
|
5
6
|
export function parseToolCall(
|
|
6
7
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
7
8
|
rawToolCall, options) {
|
|
@@ -208,12 +209,12 @@ export class JsonOutputKeyToolsParser extends JsonOutputToolsParser {
|
|
|
208
209
|
if (this.zodSchema === undefined) {
|
|
209
210
|
return result;
|
|
210
211
|
}
|
|
211
|
-
const zodParsedResult = await this.zodSchema
|
|
212
|
+
const zodParsedResult = await interopSafeParseAsync(this.zodSchema, result);
|
|
212
213
|
if (zodParsedResult.success) {
|
|
213
214
|
return zodParsedResult.data;
|
|
214
215
|
}
|
|
215
216
|
else {
|
|
216
|
-
throw new OutputParserException(`Failed to parse. Text: "${JSON.stringify(result, null, 2)}". Error: ${JSON.stringify(zodParsedResult.error
|
|
217
|
+
throw new OutputParserException(`Failed to parse. Text: "${JSON.stringify(result, null, 2)}". Error: ${JSON.stringify(zodParsedResult.error?.issues)}`, JSON.stringify(result, null, 2));
|
|
217
218
|
}
|
|
218
219
|
}
|
|
219
220
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AsymmetricStructuredOutputParser = exports.JsonMarkdownStructuredOutputParser = exports.StructuredOutputParser = void 0;
|
|
4
|
-
const
|
|
5
|
-
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
4
|
+
const v3_1 = require("zod/v3");
|
|
6
5
|
const base_js_1 = require("./base.cjs");
|
|
6
|
+
const zod_js_1 = require("../utils/types/zod.cjs");
|
|
7
|
+
const json_schema_js_1 = require("../utils/json_schema.cjs");
|
|
7
8
|
class StructuredOutputParser extends base_js_1.BaseOutputParser {
|
|
8
9
|
static lc_name() {
|
|
9
10
|
return "StructuredOutputParser";
|
|
@@ -41,7 +42,7 @@ class StructuredOutputParser extends base_js_1.BaseOutputParser {
|
|
|
41
42
|
* @returns A new instance of StructuredOutputParser.
|
|
42
43
|
*/
|
|
43
44
|
static fromNamesAndDescriptions(schemas) {
|
|
44
|
-
const zodSchema =
|
|
45
|
+
const zodSchema = v3_1.z.object(Object.fromEntries(Object.entries(schemas).map(([name, description]) => [name, v3_1.z.string().describe(description)])));
|
|
45
46
|
return new this(zodSchema);
|
|
46
47
|
}
|
|
47
48
|
/**
|
|
@@ -63,7 +64,7 @@ Your output will be parsed and type-checked according to the provided schema ins
|
|
|
63
64
|
|
|
64
65
|
Here is the JSON Schema instance your output must adhere to. Include the enclosing markdown codeblock:
|
|
65
66
|
\`\`\`json
|
|
66
|
-
${JSON.stringify((0,
|
|
67
|
+
${JSON.stringify((0, json_schema_js_1.toJsonSchema)(this.schema))}
|
|
67
68
|
\`\`\`
|
|
68
69
|
`;
|
|
69
70
|
}
|
|
@@ -83,7 +84,7 @@ ${JSON.stringify((0, zod_to_json_schema_1.zodToJsonSchema)(this.schema))}
|
|
|
83
84
|
return `"${escapedInsideQuotes}"`;
|
|
84
85
|
})
|
|
85
86
|
.replace(/\n/g, "");
|
|
86
|
-
return await this.schema
|
|
87
|
+
return await (0, zod_js_1.interopParseAsync)(this.schema, JSON.parse(escapedJson));
|
|
87
88
|
}
|
|
88
89
|
catch (e) {
|
|
89
90
|
throw new base_js_1.OutputParserException(`Failed to parse. Text: "${text}". Error: ${e}`, text);
|
|
@@ -104,7 +105,7 @@ class JsonMarkdownStructuredOutputParser extends StructuredOutputParser {
|
|
|
104
105
|
if (interpolationDepth < 1) {
|
|
105
106
|
throw new Error("f string interpolation depth must be at least 1");
|
|
106
107
|
}
|
|
107
|
-
return `Return a markdown code snippet with a JSON object formatted to look like:\n\`\`\`json\n${this._schemaToInstruction((0,
|
|
108
|
+
return `Return a markdown code snippet with a JSON object formatted to look like:\n\`\`\`json\n${this._schemaToInstruction((0, json_schema_js_1.toJsonSchema)(this.schema))
|
|
108
109
|
.replaceAll("{", "{".repeat(interpolationDepth))
|
|
109
110
|
.replaceAll("}", "}".repeat(interpolationDepth))}\n\`\`\``;
|
|
110
111
|
}
|
|
@@ -159,7 +160,7 @@ class JsonMarkdownStructuredOutputParser extends StructuredOutputParser {
|
|
|
159
160
|
return new this(schema);
|
|
160
161
|
}
|
|
161
162
|
static fromNamesAndDescriptions(schemas) {
|
|
162
|
-
const zodSchema =
|
|
163
|
+
const zodSchema = v3_1.z.object(Object.fromEntries(Object.entries(schemas).map(([name, description]) => [name, v3_1.z.string().describe(description)])));
|
|
163
164
|
return new this(zodSchema);
|
|
164
165
|
}
|
|
165
166
|
}
|
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
1
|
+
import { z } from "zod/v3";
|
|
2
2
|
import { BaseOutputParser, FormatInstructionsOptions } from "./base.js";
|
|
3
|
+
import { type InteropZodType, type InferInteropZodOutput } from "../utils/types/zod.js";
|
|
3
4
|
export type JsonMarkdownStructuredOutputParserInput = {
|
|
4
5
|
interpolationDepth?: number;
|
|
5
6
|
};
|
|
6
7
|
export interface JsonMarkdownFormatInstructionsOptions extends FormatInstructionsOptions {
|
|
7
8
|
interpolationDepth?: number;
|
|
8
9
|
}
|
|
9
|
-
export declare class StructuredOutputParser<T extends
|
|
10
|
+
export declare class StructuredOutputParser<T extends InteropZodType> extends BaseOutputParser<InferInteropZodOutput<T>> {
|
|
10
11
|
schema: T;
|
|
11
12
|
static lc_name(): string;
|
|
12
13
|
lc_namespace: string[];
|
|
@@ -17,7 +18,7 @@ export declare class StructuredOutputParser<T extends z.ZodTypeAny> extends Base
|
|
|
17
18
|
* @param schema The Zod schema which the output should match
|
|
18
19
|
* @returns A new instance of StructuredOutputParser.
|
|
19
20
|
*/
|
|
20
|
-
static fromZodSchema<T extends
|
|
21
|
+
static fromZodSchema<T extends InteropZodType>(schema: T): StructuredOutputParser<T>;
|
|
21
22
|
/**
|
|
22
23
|
* Creates a new StructuredOutputParser from a set of names and
|
|
23
24
|
* descriptions.
|
|
@@ -45,17 +46,17 @@ export declare class StructuredOutputParser<T extends z.ZodTypeAny> extends Base
|
|
|
45
46
|
* @param text The text to parse
|
|
46
47
|
* @returns The parsed output.
|
|
47
48
|
*/
|
|
48
|
-
parse(text: string): Promise<
|
|
49
|
+
parse(text: string): Promise<InferInteropZodOutput<T>>;
|
|
49
50
|
}
|
|
50
51
|
/**
|
|
51
52
|
* A specific type of `StructuredOutputParser` that parses JSON data
|
|
52
53
|
* formatted as a markdown code snippet.
|
|
53
54
|
*/
|
|
54
|
-
export declare class JsonMarkdownStructuredOutputParser<T extends
|
|
55
|
+
export declare class JsonMarkdownStructuredOutputParser<T extends InteropZodType> extends StructuredOutputParser<T> {
|
|
55
56
|
static lc_name(): string;
|
|
56
57
|
getFormatInstructions(options?: JsonMarkdownFormatInstructionsOptions): string;
|
|
57
58
|
private _schemaToInstruction;
|
|
58
|
-
static fromZodSchema<T extends
|
|
59
|
+
static fromZodSchema<T extends InteropZodType>(schema: T): JsonMarkdownStructuredOutputParser<T>;
|
|
59
60
|
static fromNamesAndDescriptions<S extends {
|
|
60
61
|
[key: string]: string;
|
|
61
62
|
}>(schemas: S): JsonMarkdownStructuredOutputParser<z.ZodObject<{
|
|
@@ -66,14 +67,14 @@ export declare class JsonMarkdownStructuredOutputParser<T extends z.ZodTypeAny>
|
|
|
66
67
|
[x: string]: string;
|
|
67
68
|
}>>;
|
|
68
69
|
}
|
|
69
|
-
export interface AsymmetricStructuredOutputParserFields<T extends
|
|
70
|
+
export interface AsymmetricStructuredOutputParserFields<T extends InteropZodType> {
|
|
70
71
|
inputSchema: T;
|
|
71
72
|
}
|
|
72
73
|
/**
|
|
73
74
|
* A type of `StructuredOutputParser` that handles asymmetric input and
|
|
74
75
|
* output schemas.
|
|
75
76
|
*/
|
|
76
|
-
export declare abstract class AsymmetricStructuredOutputParser<T extends
|
|
77
|
+
export declare abstract class AsymmetricStructuredOutputParser<T extends InteropZodType, Y = unknown> extends BaseOutputParser<Y> {
|
|
77
78
|
private structuredInputParser;
|
|
78
79
|
constructor({ inputSchema }: AsymmetricStructuredOutputParserFields<T>);
|
|
79
80
|
/**
|
|
@@ -82,7 +83,7 @@ export declare abstract class AsymmetricStructuredOutputParser<T extends z.ZodTy
|
|
|
82
83
|
* @param input The parsed input
|
|
83
84
|
* @returns The processed output.
|
|
84
85
|
*/
|
|
85
|
-
abstract outputProcessor(input:
|
|
86
|
+
abstract outputProcessor(input: InferInteropZodOutput<T>): Promise<Y>;
|
|
86
87
|
parse(text: string): Promise<Y>;
|
|
87
88
|
getFormatInstructions(): string;
|
|
88
89
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
import { zodToJsonSchema, } from "zod-to-json-schema";
|
|
1
|
+
import { z } from "zod/v3";
|
|
3
2
|
import { BaseOutputParser, OutputParserException, } from "./base.js";
|
|
3
|
+
import { interopParseAsync, } from "../utils/types/zod.js";
|
|
4
|
+
import { toJsonSchema, } from "../utils/json_schema.js";
|
|
4
5
|
export class StructuredOutputParser extends BaseOutputParser {
|
|
5
6
|
static lc_name() {
|
|
6
7
|
return "StructuredOutputParser";
|
|
@@ -60,7 +61,7 @@ Your output will be parsed and type-checked according to the provided schema ins
|
|
|
60
61
|
|
|
61
62
|
Here is the JSON Schema instance your output must adhere to. Include the enclosing markdown codeblock:
|
|
62
63
|
\`\`\`json
|
|
63
|
-
${JSON.stringify(
|
|
64
|
+
${JSON.stringify(toJsonSchema(this.schema))}
|
|
64
65
|
\`\`\`
|
|
65
66
|
`;
|
|
66
67
|
}
|
|
@@ -80,7 +81,7 @@ ${JSON.stringify(zodToJsonSchema(this.schema))}
|
|
|
80
81
|
return `"${escapedInsideQuotes}"`;
|
|
81
82
|
})
|
|
82
83
|
.replace(/\n/g, "");
|
|
83
|
-
return await this.schema
|
|
84
|
+
return await interopParseAsync(this.schema, JSON.parse(escapedJson));
|
|
84
85
|
}
|
|
85
86
|
catch (e) {
|
|
86
87
|
throw new OutputParserException(`Failed to parse. Text: "${text}". Error: ${e}`, text);
|
|
@@ -100,7 +101,7 @@ export class JsonMarkdownStructuredOutputParser extends StructuredOutputParser {
|
|
|
100
101
|
if (interpolationDepth < 1) {
|
|
101
102
|
throw new Error("f string interpolation depth must be at least 1");
|
|
102
103
|
}
|
|
103
|
-
return `Return a markdown code snippet with a JSON object formatted to look like:\n\`\`\`json\n${this._schemaToInstruction(
|
|
104
|
+
return `Return a markdown code snippet with a JSON object formatted to look like:\n\`\`\`json\n${this._schemaToInstruction(toJsonSchema(this.schema))
|
|
104
105
|
.replaceAll("{", "{".repeat(interpolationDepth))
|
|
105
106
|
.replaceAll("}", "}".repeat(interpolationDepth))}\n\`\`\``;
|
|
106
107
|
}
|
package/dist/runnables/base.cjs
CHANGED
|
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.convertRunnableToTool = exports.RunnableToolLike = exports.RunnablePick = exports.RunnableAssign = exports._coerceToRunnable = exports.RunnableWithFallbacks = exports.RunnableParallel = exports.RunnableLambda = exports.RunnableTraceable = exports.RunnableMap = exports.RunnableSequence = exports.RunnableRetry = exports.RunnableEach = exports.RunnableBinding = exports.Runnable = exports._coerceToDict = void 0;
|
|
7
|
-
const
|
|
7
|
+
const v3_1 = require("zod/v3");
|
|
8
8
|
const p_retry_1 = __importDefault(require("p-retry"));
|
|
9
9
|
const uuid_1 = require("uuid");
|
|
10
10
|
const traceable_1 = require("langsmith/singletons/traceable");
|
|
@@ -22,6 +22,7 @@ const graph_js_1 = require("./graph.cjs");
|
|
|
22
22
|
const wrappers_js_1 = require("./wrappers.cjs");
|
|
23
23
|
const iter_js_1 = require("./iter.cjs");
|
|
24
24
|
const utils_js_2 = require("../tools/utils.cjs");
|
|
25
|
+
const zod_js_1 = require("../utils/types/zod.cjs");
|
|
25
26
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
26
27
|
function _coerceToDict(value, defaultKey) {
|
|
27
28
|
return value &&
|
|
@@ -342,13 +343,13 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
342
343
|
// TODO: Add input schema for runnables
|
|
343
344
|
const inputNode = graph.addNode({
|
|
344
345
|
name: `${this.getName()}Input`,
|
|
345
|
-
schema:
|
|
346
|
+
schema: v3_1.z.any(),
|
|
346
347
|
});
|
|
347
348
|
const runnableNode = graph.addNode(this);
|
|
348
349
|
// TODO: Add output schemas for runnables
|
|
349
350
|
const outputNode = graph.addNode({
|
|
350
351
|
name: `${this.getName()}Output`,
|
|
351
|
-
schema:
|
|
352
|
+
schema: v3_1.z.any(),
|
|
352
353
|
});
|
|
353
354
|
graph.addEdge(inputNode, runnableNode);
|
|
354
355
|
graph.addEdge(runnableNode, outputNode);
|
|
@@ -2334,7 +2335,7 @@ class RunnableToolLike extends RunnableBinding {
|
|
|
2334
2335
|
let toolInput;
|
|
2335
2336
|
if ((0, utils_js_2._isToolCall)(input)) {
|
|
2336
2337
|
try {
|
|
2337
|
-
toolInput = await this.schema
|
|
2338
|
+
toolInput = await (0, zod_js_1.interopParseAsync)(this.schema, input.args);
|
|
2338
2339
|
}
|
|
2339
2340
|
catch (e) {
|
|
2340
2341
|
throw new utils_js_2.ToolInputParsingException(`Received tool input did not match expected schema`, JSON.stringify(input.args));
|
|
@@ -2388,20 +2389,18 @@ exports.RunnableToolLike = RunnableToolLike;
|
|
|
2388
2389
|
* @param fields
|
|
2389
2390
|
* @param {string | undefined} [fields.name] The name of the tool. If not provided, it will default to the name of the runnable.
|
|
2390
2391
|
* @param {string | undefined} [fields.description] The description of the tool. Falls back to the description on the Zod schema if not provided, or undefined if neither are provided.
|
|
2391
|
-
* @param {
|
|
2392
|
-
* @returns {RunnableToolLike<
|
|
2392
|
+
* @param {InteropZodType<RunInput>} [fields.schema] The Zod schema for the input of the tool. Infers the Zod type from the input type of the runnable.
|
|
2393
|
+
* @returns {RunnableToolLike<InteropZodType<RunInput>, RunOutput>} An instance of `RunnableToolLike` which is a runnable that can be used as a tool.
|
|
2393
2394
|
*/
|
|
2394
2395
|
function convertRunnableToTool(runnable, fields) {
|
|
2395
2396
|
const name = fields.name ?? runnable.getName();
|
|
2396
|
-
const description = fields.description ?? fields.schema
|
|
2397
|
-
if (fields.schema
|
|
2397
|
+
const description = fields.description ?? (0, zod_js_1.getSchemaDescription)(fields.schema);
|
|
2398
|
+
if ((0, zod_js_1.isSimpleStringZodSchema)(fields.schema)) {
|
|
2398
2399
|
return new RunnableToolLike({
|
|
2399
2400
|
name,
|
|
2400
2401
|
description,
|
|
2401
|
-
schema:
|
|
2402
|
-
.object({
|
|
2403
|
-
input: zod_1.z.string(),
|
|
2404
|
-
})
|
|
2402
|
+
schema: v3_1.z
|
|
2403
|
+
.object({ input: v3_1.z.string() })
|
|
2405
2404
|
.transform((input) => input.input),
|
|
2406
2405
|
bound: runnable,
|
|
2407
2406
|
});
|
package/dist/runnables/base.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
1
|
import { type TraceableFunction } from "langsmith/singletons/traceable";
|
|
3
2
|
import type { RunnableInterface, RunnableBatchOptions, RunnableConfig } from "./types.js";
|
|
4
3
|
import { CallbackManagerForChainRun } from "../callbacks/manager.js";
|
|
@@ -9,6 +8,7 @@ import { IterableReadableStream } from "../utils/stream.js";
|
|
|
9
8
|
import { Run } from "../tracers/base.js";
|
|
10
9
|
import { Graph } from "./graph.js";
|
|
11
10
|
import { ToolCall } from "../messages/tool.js";
|
|
11
|
+
import { InferInteropZodOutput, InteropZodType } from "../utils/types/zod.js";
|
|
12
12
|
export { type RunnableInterface, RunnableBatchOptions };
|
|
13
13
|
export type RunnableFunc<RunInput, RunOutput, CallOptions extends RunnableConfig = RunnableConfig> = (input: RunInput, options: CallOptions | Record<string, any> | (Record<string, any> & CallOptions)) => RunOutput | Promise<RunOutput>;
|
|
14
14
|
export type RunnableMapLike<RunInput, RunOutput> = {
|
|
@@ -310,8 +310,8 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
|
|
|
310
310
|
asTool<T extends RunInput = RunInput>(fields: {
|
|
311
311
|
name?: string;
|
|
312
312
|
description?: string;
|
|
313
|
-
schema:
|
|
314
|
-
}): RunnableToolLike<
|
|
313
|
+
schema: InteropZodType<T>;
|
|
314
|
+
}): RunnableToolLike<InteropZodType<T | ToolCall>, RunOutput>;
|
|
315
315
|
}
|
|
316
316
|
export type RunnableBindingArgs<RunInput, RunOutput, CallOptions extends RunnableConfig = RunnableConfig> = {
|
|
317
317
|
bound: Runnable<RunInput, RunOutput, CallOptions>;
|
|
@@ -910,13 +910,13 @@ export declare class RunnablePick<RunInput extends Record<string, any> = Record<
|
|
|
910
910
|
transform(generator: AsyncGenerator<RunInput>, options?: Partial<RunnableConfig>): AsyncGenerator<RunOutput>;
|
|
911
911
|
stream(input: RunInput, options?: Partial<RunnableConfig>): Promise<IterableReadableStream<RunOutput>>;
|
|
912
912
|
}
|
|
913
|
-
export interface RunnableToolLikeArgs<RunInput extends
|
|
913
|
+
export interface RunnableToolLikeArgs<RunInput extends InteropZodType = InteropZodType, RunOutput = unknown> extends Omit<RunnableBindingArgs<InferInteropZodOutput<RunInput>, RunOutput>, "config"> {
|
|
914
914
|
name: string;
|
|
915
915
|
description?: string;
|
|
916
916
|
schema: RunInput;
|
|
917
917
|
config?: RunnableConfig;
|
|
918
918
|
}
|
|
919
|
-
export declare class RunnableToolLike<RunInput extends
|
|
919
|
+
export declare class RunnableToolLike<RunInput extends InteropZodType = InteropZodType, RunOutput = unknown> extends RunnableBinding<InferInteropZodOutput<RunInput>, RunOutput> {
|
|
920
920
|
name: string;
|
|
921
921
|
description?: string;
|
|
922
922
|
schema: RunInput;
|
|
@@ -933,11 +933,11 @@ export declare class RunnableToolLike<RunInput extends z.ZodType = z.ZodType, Ru
|
|
|
933
933
|
* @param fields
|
|
934
934
|
* @param {string | undefined} [fields.name] The name of the tool. If not provided, it will default to the name of the runnable.
|
|
935
935
|
* @param {string | undefined} [fields.description] The description of the tool. Falls back to the description on the Zod schema if not provided, or undefined if neither are provided.
|
|
936
|
-
* @param {
|
|
937
|
-
* @returns {RunnableToolLike<
|
|
936
|
+
* @param {InteropZodType<RunInput>} [fields.schema] The Zod schema for the input of the tool. Infers the Zod type from the input type of the runnable.
|
|
937
|
+
* @returns {RunnableToolLike<InteropZodType<RunInput>, RunOutput>} An instance of `RunnableToolLike` which is a runnable that can be used as a tool.
|
|
938
938
|
*/
|
|
939
939
|
export declare function convertRunnableToTool<RunInput, RunOutput>(runnable: Runnable<RunInput, RunOutput>, fields: {
|
|
940
940
|
name?: string;
|
|
941
941
|
description?: string;
|
|
942
|
-
schema:
|
|
943
|
-
}): RunnableToolLike<
|
|
942
|
+
schema: InteropZodType<RunInput>;
|
|
943
|
+
}): RunnableToolLike<InteropZodType<RunInput | ToolCall>, RunOutput>;
|
package/dist/runnables/base.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
1
|
+
import { z } from "zod/v3";
|
|
2
2
|
import pRetry from "p-retry";
|
|
3
3
|
import { v4 as uuidv4 } from "uuid";
|
|
4
4
|
import { isTraceableFunction, } from "langsmith/singletons/traceable";
|
|
@@ -16,6 +16,7 @@ import { Graph } from "./graph.js";
|
|
|
16
16
|
import { convertToHttpEventStream } from "./wrappers.js";
|
|
17
17
|
import { consumeAsyncIterableInContext, consumeIteratorInContext, isAsyncIterable, isIterableIterator, isIterator, } from "./iter.js";
|
|
18
18
|
import { _isToolCall, ToolInputParsingException } from "../tools/utils.js";
|
|
19
|
+
import { getSchemaDescription, interopParseAsync, isSimpleStringZodSchema, } from "../utils/types/zod.js";
|
|
19
20
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
20
21
|
export function _coerceToDict(value, defaultKey) {
|
|
21
22
|
return value &&
|
|
@@ -2314,7 +2315,7 @@ export class RunnableToolLike extends RunnableBinding {
|
|
|
2314
2315
|
let toolInput;
|
|
2315
2316
|
if (_isToolCall(input)) {
|
|
2316
2317
|
try {
|
|
2317
|
-
toolInput = await this.schema
|
|
2318
|
+
toolInput = await interopParseAsync(this.schema, input.args);
|
|
2318
2319
|
}
|
|
2319
2320
|
catch (e) {
|
|
2320
2321
|
throw new ToolInputParsingException(`Received tool input did not match expected schema`, JSON.stringify(input.args));
|
|
@@ -2367,20 +2368,18 @@ export class RunnableToolLike extends RunnableBinding {
|
|
|
2367
2368
|
* @param fields
|
|
2368
2369
|
* @param {string | undefined} [fields.name] The name of the tool. If not provided, it will default to the name of the runnable.
|
|
2369
2370
|
* @param {string | undefined} [fields.description] The description of the tool. Falls back to the description on the Zod schema if not provided, or undefined if neither are provided.
|
|
2370
|
-
* @param {
|
|
2371
|
-
* @returns {RunnableToolLike<
|
|
2371
|
+
* @param {InteropZodType<RunInput>} [fields.schema] The Zod schema for the input of the tool. Infers the Zod type from the input type of the runnable.
|
|
2372
|
+
* @returns {RunnableToolLike<InteropZodType<RunInput>, RunOutput>} An instance of `RunnableToolLike` which is a runnable that can be used as a tool.
|
|
2372
2373
|
*/
|
|
2373
2374
|
export function convertRunnableToTool(runnable, fields) {
|
|
2374
2375
|
const name = fields.name ?? runnable.getName();
|
|
2375
|
-
const description = fields.description ?? fields.schema
|
|
2376
|
-
if (fields.schema
|
|
2376
|
+
const description = fields.description ?? getSchemaDescription(fields.schema);
|
|
2377
|
+
if (isSimpleStringZodSchema(fields.schema)) {
|
|
2377
2378
|
return new RunnableToolLike({
|
|
2378
2379
|
name,
|
|
2379
2380
|
description,
|
|
2380
2381
|
schema: z
|
|
2381
|
-
.object({
|
|
2382
|
-
input: z.string(),
|
|
2383
|
-
})
|
|
2382
|
+
.object({ input: z.string() })
|
|
2384
2383
|
.transform((input) => input.input),
|
|
2385
2384
|
bound: runnable,
|
|
2386
2385
|
});
|