Convert any return value to a ToolResult. - None returns empty success - Strings pass through directly - ToolResult passes through - Everything else gets JSON-serialized (with Pydantic support)
(result: Any)
| 280 | |
| 281 | |
| 282 | def _normalize_result(result: Any) -> ToolResult: |
| 283 | """ |
| 284 | Convert any return value to a ToolResult. |
| 285 | |
| 286 | - None returns empty success |
| 287 | - Strings pass through directly |
| 288 | - ToolResult passes through |
| 289 | - Everything else gets JSON-serialized (with Pydantic support) |
| 290 | """ |
| 291 | if result is None: |
| 292 | return ToolResult( |
| 293 | text_result_for_llm="", |
| 294 | result_type="success", |
| 295 | ) |
| 296 | |
| 297 | # ToolResult dataclass passes through directly |
| 298 | if isinstance(result, ToolResult): |
| 299 | return result |
| 300 | |
| 301 | # Strings pass through directly |
| 302 | if isinstance(result, str): |
| 303 | return ToolResult( |
| 304 | text_result_for_llm=result, |
| 305 | result_type="success", |
| 306 | ) |
| 307 | |
| 308 | # Everything else gets JSON-serialized (with Pydantic model support) |
| 309 | def default(obj: Any) -> Any: |
| 310 | if isinstance(obj, BaseModel): |
| 311 | return obj.model_dump() |
| 312 | raise TypeError(f"Object of type {type(obj).__name__} is not JSON serializable") |
| 313 | |
| 314 | try: |
| 315 | json_str = json.dumps(result, default=default) |
| 316 | except (TypeError, ValueError) as exc: |
| 317 | raise TypeError(f"Failed to serialize tool result: {exc}") from exc |
| 318 | |
| 319 | return ToolResult( |
| 320 | text_result_for_llm=json_str, |
| 321 | result_type="success", |
| 322 | ) |
| 323 | |
| 324 | |
| 325 | def convert_mcp_call_tool_result(call_result: dict[str, Any]) -> ToolResult: |
searching dependent graphs…