@@ -199,21 +199,24 @@ class ContentToolResult(Content):
199199 name : Optional [str ] = None
200200 error : Optional [str ] = None
201201
202- def _get_value_and_language (self ) -> tuple [ str , str ] :
202+ def _get_value (self , pretty : bool = False ) -> str :
203203 if self .error :
204- return f"Tool calling failed with error: '{ self .error } '" , ""
204+ return f"Tool calling failed with error: '{ self .error } '"
205+ if not pretty :
206+ return str (self .value )
205207 try :
206- json_val = json .loads (self .value )
207- return pformat (json_val , indent = 2 , sort_dicts = False ), "python"
208+ json_val = json .loads (self .value ) # type: ignore
209+ return pformat (json_val , indent = 2 , sort_dicts = False )
208210 except : # noqa: E722
209- return str (self .value ), ""
211+ return str (self .value )
210212
213+ # Primarily used for `echo="all"`...
211214 def __str__ (self ):
212215 comment = f"# tool result ({ self .id } )"
213- value , language = self ._get_value_and_language ()
214-
215- return f"""```{ language } \n { comment } \n { value } \n ```"""
216+ value = self ._get_value (pretty = True )
217+ return f"""```python\n { comment } \n { value } \n ```"""
216218
219+ # ... and for displaying in the notebook
217220 def _repr_markdown_ (self ):
218221 return self .__str__ ()
219222
@@ -224,9 +227,9 @@ def __repr__(self, indent: int = 0):
224227 res += f" error='{ self .error } '"
225228 return res + ">"
226229
230+ # The actual value to send to the model
227231 def get_final_value (self ) -> str :
228- value , _language = self ._get_value_and_language ()
229- return value
232+ return self ._get_value ()
230233
231234
232235@dataclass
0 commit comments