@@ -73,6 +73,8 @@ class Tool(t.Generic[P, R]):
7373 - `True`: Catch all exceptions.
7474 - `list[type[Exception]]`: Catch only the specified exceptions.
7575 """
76+ truncate : int | None = None
77+ """If set, the maximum number of characters to truncate any tool output to."""
7678
7779 _signature : inspect .Signature | None = field (default = None , init = False , repr = False )
7880 _type_adapter : TypeAdapter [t .Any ] | None = field (
@@ -100,6 +102,7 @@ def from_callable(
100102 name : str | None = None ,
101103 description : str | None = None ,
102104 catch : bool | t .Iterable [type [Exception ]] = False ,
105+ truncate : int | None = None ,
103106 ) -> te .Self :
104107 from rigging .prompt import Prompt
105108
@@ -194,6 +197,7 @@ def empty_func(*args, **kwargs): # type: ignore [no-untyped-def] # noqa: ARG001
194197 parameters_schema = schema ,
195198 fn = fn ,
196199 catch = catch if isinstance (catch , bool ) else set (catch ),
200+ truncate = truncate ,
197201 )
198202
199203 self ._signature = signature
@@ -367,6 +371,9 @@ async def handle_tool_call(
367371 else :
368372 message .content_parts = [ContentText (text = str (result ))]
369373
374+ if self .truncate :
375+ message = message .truncate (self .truncate )
376+
370377 # If this is a native tool call, we should wrap up our
371378 # result in a NativeToolResult object to provide clarity to the
372379 # generator. Otherwise we can rely on the `tool` role and associated
@@ -402,6 +409,7 @@ def tool(
402409 name : str | None = None ,
403410 description : str | None = None ,
404411 catch : bool | t .Iterable [type [Exception ]] = False ,
412+ truncate : int | None = None ,
405413) -> t .Callable [[t .Callable [P , R ]], Tool [P , R ]]:
406414 ...
407415
@@ -421,6 +429,7 @@ def tool(
421429 name : str | None = None ,
422430 description : str | None = None ,
423431 catch : bool | t .Iterable [type [Exception ]] = False ,
432+ truncate : int | None = None ,
424433) -> t .Callable [[t .Callable [P , R ]], Tool [P , R ]] | Tool [P , R ]:
425434 """
426435 Decorator for creating a Tool, useful for overriding a name or description.
@@ -433,6 +442,7 @@ def tool(
433442 - `False`: Do not catch exceptions.
434443 - `True`: Catch all exceptions.
435444 - `list[type[Exception]]`: Catch only the specified exceptions.
445+ truncate: If set, the maximum number of characters to truncate any tool output to.
436446
437447 Returns:
438448 The decorated Tool object.
@@ -453,7 +463,13 @@ def make_tool(func: t.Callable[..., t.Any]) -> Tool[P, R]:
453463 stacklevel = 3 ,
454464 )
455465
456- return Tool .from_callable (func , name = name , description = description , catch = catch )
466+ return Tool .from_callable (
467+ func ,
468+ name = name ,
469+ description = description ,
470+ catch = catch ,
471+ truncate = truncate ,
472+ )
457473
458474 if func is not None :
459475 return make_tool (func )
@@ -496,6 +512,7 @@ def tool_method(
496512 name : str | None = None ,
497513 description : str | None = None ,
498514 catch : bool | t .Iterable [type [Exception ]] = False ,
515+ truncate : int | None = None ,
499516) -> t .Callable [[t .Callable [t .Concatenate [t .Any , P ], R ]], ToolMethod [P , R ]]:
500517 ...
501518
@@ -515,6 +532,7 @@ def tool_method(
515532 name : str | None = None ,
516533 description : str | None = None ,
517534 catch : bool | t .Iterable [type [Exception ]] = False ,
535+ truncate : int | None = None ,
518536) -> t .Callable [[t .Callable [t .Concatenate [t .Any , P ], R ]], ToolMethod [P , R ]] | ToolMethod [P , R ]:
519537 """
520538 Decorator for creating a Tool from a class method.
@@ -570,6 +588,7 @@ def wrapper(self: t.Any, *args: P.args, **kwargs: P.kwargs) -> R:
570588 name = name ,
571589 description = description ,
572590 catch = catch ,
591+ truncate = truncate ,
573592 )
574593
575594 if func is not None :
0 commit comments