@@ -20,6 +20,7 @@ import { ZodObjectAny } from "../types/zod.js";
20
20
import { MessageContent } from "../messages/base.js" ;
21
21
import { AsyncLocalStorageProviderSingleton } from "../singletons/index.js" ;
22
22
import { _isToolCall , ToolInputParsingException } from "./utils.js" ;
23
+ import { isZodSchema } from "../utils/types/is_zod_schema.js" ;
23
24
24
25
export { ToolInputParsingException } ;
25
26
@@ -319,16 +320,19 @@ export interface DynamicToolInput extends BaseDynamicToolInput {
319
320
* Interface for the input parameters of the DynamicStructuredTool class.
320
321
*/
321
322
export interface DynamicStructuredToolInput <
322
- T extends ZodObjectAny = ZodObjectAny
323
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
324
+ T extends ZodObjectAny | Record < string , any > = ZodObjectAny
323
325
> extends BaseDynamicToolInput {
324
326
func : (
325
327
input : BaseDynamicToolInput [ "responseFormat" ] extends "content_and_artifact"
326
328
? ToolCall
327
- : z . infer < T > ,
329
+ : T extends ZodObjectAny
330
+ ? z . infer < T >
331
+ : T ,
328
332
runManager ?: CallbackManagerForToolRun ,
329
333
config ?: RunnableConfig
330
334
) => Promise < ToolReturnType > ;
331
- schema : T ;
335
+ schema : T extends ZodObjectAny ? T : T ;
332
336
}
333
337
334
338
/**
@@ -382,10 +386,14 @@ export class DynamicTool extends Tool {
382
386
* description, designed to work with structured data. It extends the
383
387
* StructuredTool class and overrides the _call method to execute the
384
388
* provided function when the tool is called.
389
+ *
390
+ * Schema can be passed as Zod or JSON schema. The tool will not validate
391
+ * input if JSON schema is passed.
385
392
*/
386
393
export class DynamicStructuredTool <
387
- T extends ZodObjectAny = ZodObjectAny
388
- > extends StructuredTool < T > {
394
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
395
+ T extends ZodObjectAny | Record < string , any > = ZodObjectAny
396
+ > extends StructuredTool < T extends ZodObjectAny ? T : ZodObjectAny > {
389
397
static lc_name ( ) {
390
398
return "DynamicStructuredTool" ;
391
399
}
@@ -396,22 +404,24 @@ export class DynamicStructuredTool<
396
404
397
405
func : DynamicStructuredToolInput < T > [ "func" ] ;
398
406
399
- schema : T ;
407
+ schema : T extends ZodObjectAny ? T : ZodObjectAny ;
400
408
401
409
constructor ( fields : DynamicStructuredToolInput < T > ) {
402
410
super ( fields ) ;
403
411
this . name = fields . name ;
404
412
this . description = fields . description ;
405
413
this . func = fields . func ;
406
414
this . returnDirect = fields . returnDirect ?? this . returnDirect ;
407
- this . schema = fields . schema ;
415
+ this . schema = (
416
+ isZodSchema ( fields . schema ) ? fields . schema : z . object ( { } )
417
+ ) as T extends ZodObjectAny ? T : ZodObjectAny ;
408
418
}
409
419
410
420
/**
411
421
* @deprecated Use .invoke() instead. Will be removed in 0.3.0.
412
422
*/
413
423
async call (
414
- arg : z . output < T > | ToolCall ,
424
+ arg : ( T extends ZodObjectAny ? z . output < T > : T ) | ToolCall ,
415
425
configArg ?: RunnableConfig | Callbacks ,
416
426
/** @deprecated */
417
427
tags ?: string [ ]
@@ -424,11 +434,12 @@ export class DynamicStructuredTool<
424
434
}
425
435
426
436
protected _call (
427
- arg : z . output < T > | ToolCall ,
437
+ arg : ( T extends ZodObjectAny ? z . output < T > : T ) | ToolCall ,
428
438
runManager ?: CallbackManagerForToolRun ,
429
439
parentConfig ?: RunnableConfig
430
440
) : Promise < ToolReturnType > {
431
- return this . func ( arg , runManager , parentConfig ) ;
441
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
442
+ return this . func ( arg as any , runManager , parentConfig ) ;
432
443
}
433
444
}
434
445
@@ -447,10 +458,16 @@ export abstract class BaseToolkit {
447
458
448
459
/**
449
460
* Parameters for the tool function.
450
- * @template {ZodObjectAny | z.ZodString = ZodObjectAny} RunInput The input schema for the tool. Either any Zod object, or a Zod string.
461
+ * Schema can be provided as Zod or JSON schema.
462
+ * If you pass JSON schema, tool inputs will not be validated.
463
+ * @template {ZodObjectAny | z.ZodString | Record<string, any> = ZodObjectAny} RunInput The input schema for the tool. Either any Zod object, a Zod string, or JSON schema.
451
464
*/
452
465
interface ToolWrapperParams <
453
- RunInput extends ZodObjectAny | z . ZodString = ZodObjectAny
466
+ RunInput extends
467
+ | ZodObjectAny
468
+ | z . ZodString
469
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
470
+ | Record < string , any > = ZodObjectAny
454
471
> extends ToolParams {
455
472
/**
456
473
* The name of the tool. If using with an LLM, this
@@ -483,8 +500,11 @@ interface ToolWrapperParams<
483
500
/**
484
501
* Creates a new StructuredTool instance with the provided function, name, description, and schema.
485
502
*
503
+ * Schema can be provided as Zod or JSON schema.
504
+ * If you pass JSON schema, tool inputs will not be validated.
505
+ *
486
506
* @function
487
- * @template {ZodObjectAny | z.ZodString = ZodObjectAny} T The input schema for the tool. Either any Zod object, or a Zod string.
507
+ * @template {ZodObjectAny | z.ZodString | Record<string, any> = ZodObjectAny} T The input schema for the tool. Either any Zod object, a Zod string, or JSON schema instance .
488
508
*
489
509
* @param {RunnableFunc<z.output<T>, ToolReturnType> } func - The function to invoke when the tool is called.
490
510
* @param {ToolWrapperParams<T> } fields - An object containing the following properties:
@@ -494,18 +514,27 @@ interface ToolWrapperParams<
494
514
*
495
515
* @returns {DynamicStructuredTool<T> } A new StructuredTool instance.
496
516
*/
497
- export function tool < T extends z . ZodString = z . ZodString > (
517
+ export function tool < T extends z . ZodString > (
498
518
func : RunnableFunc < z . output < T > , ToolReturnType > ,
499
519
fields : ToolWrapperParams < T >
500
520
) : DynamicTool ;
501
521
502
- export function tool < T extends ZodObjectAny = ZodObjectAny > (
522
+ export function tool < T extends ZodObjectAny > (
503
523
func : RunnableFunc < z . output < T > , ToolReturnType > ,
504
524
fields : ToolWrapperParams < T >
505
525
) : DynamicStructuredTool < T > ;
506
526
507
- export function tool < T extends ZodObjectAny | z . ZodString = ZodObjectAny > (
508
- func : RunnableFunc < z . output < T > , ToolReturnType > ,
527
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
528
+ export function tool < T extends Record < string , any > > (
529
+ func : RunnableFunc < T , ToolReturnType > ,
530
+ fields : ToolWrapperParams < T >
531
+ ) : DynamicStructuredTool < T > ;
532
+
533
+ export function tool <
534
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
535
+ T extends ZodObjectAny | z . ZodString | Record < string , any > = ZodObjectAny
536
+ > (
537
+ func : RunnableFunc < T extends ZodObjectAny ? z . output < T > : T , ToolReturnType > ,
509
538
fields : ToolWrapperParams < T >
510
539
) :
511
540
| DynamicStructuredTool < T extends ZodObjectAny ? T : ZodObjectAny >
@@ -518,7 +547,9 @@ export function tool<T extends ZodObjectAny | z.ZodString = ZodObjectAny>(
518
547
fields . description ??
519
548
fields . schema ?. description ??
520
549
`${ fields . name } tool` ,
521
- func,
550
+ // TS doesn't restrict the type here based on the guard above
551
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
552
+ func : func as any ,
522
553
} ) ;
523
554
}
524
555
@@ -528,7 +559,8 @@ export function tool<T extends ZodObjectAny | z.ZodString = ZodObjectAny>(
528
559
return new DynamicStructuredTool < T extends ZodObjectAny ? T : ZodObjectAny > ( {
529
560
...fields ,
530
561
description,
531
- schema : fields . schema as T extends ZodObjectAny ? T : ZodObjectAny ,
562
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
563
+ schema : fields . schema as any ,
532
564
// TODO: Consider moving into DynamicStructuredTool constructor
533
565
func : async ( input , runManager , config ) => {
534
566
return new Promise ( ( resolve , reject ) => {
@@ -539,7 +571,9 @@ export function tool<T extends ZodObjectAny | z.ZodString = ZodObjectAny>(
539
571
childConfig ,
540
572
async ( ) => {
541
573
try {
542
- resolve ( func ( input , childConfig ) ) ;
574
+ // TS doesn't restrict the type here based on the guard above
575
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
576
+ resolve ( func ( input as any , childConfig ) ) ;
543
577
} catch ( e ) {
544
578
reject ( e ) ;
545
579
}
0 commit comments