3b4e993531
2. use tavily from`langchain-tavily` rather than the older one from `langchain-community` Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
58 lines
1.4 KiB
Python
58 lines
1.4 KiB
Python
# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
import json
|
|
import logging
|
|
from typing import Any
|
|
import json_repair
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def sanitize_args(args: Any) -> str:
|
|
"""
|
|
Sanitize tool call arguments to prevent special character issues.
|
|
|
|
Args:
|
|
args: Tool call arguments string
|
|
|
|
Returns:
|
|
str: Sanitized arguments string
|
|
"""
|
|
if not isinstance(args, str):
|
|
return ""
|
|
else:
|
|
return (
|
|
args.replace("[", "[")
|
|
.replace("]", "]")
|
|
.replace("{", "{")
|
|
.replace("}", "}")
|
|
)
|
|
|
|
|
|
def repair_json_output(content: str) -> str:
|
|
"""
|
|
Repair and normalize JSON output.
|
|
|
|
Args:
|
|
content (str): String content that may contain JSON
|
|
|
|
Returns:
|
|
str: Repaired JSON string, or original content if not JSON
|
|
"""
|
|
content = content.strip()
|
|
|
|
try:
|
|
# Try to repair and parse JSON
|
|
repaired_content = json_repair.loads(content)
|
|
if not isinstance(repaired_content, dict) and not isinstance(
|
|
repaired_content, list
|
|
):
|
|
logger.warning("Repaired content is not a valid JSON object or array.")
|
|
return content
|
|
content = json.dumps(repaired_content, ensure_ascii=False)
|
|
except Exception as e:
|
|
logger.warning(f"JSON repair failed: {e}")
|
|
|
|
return content
|