File size: 14,844 Bytes
22087d0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | """
Native Tool Call Parser Module
This module provides utilities for parsing OpenAI-style native tool calls from LLM responses.
"""
import json
import uuid
from typing import Dict, Any, List, Optional
import logging
logger = logging.getLogger(__name__)
def extract_tool_call_chunk_data(tool_call_chunk: Any) -> Dict[str, Any]:
"""
Extract tool call chunk data from a LiteLLM tool_call_chunk object.
Args:
tool_call_chunk: LiteLLM tool_call_chunk object (may have model_dump or manual attributes)
Returns:
Dictionary with 'id', 'index', 'type', 'function' keys
"""
tool_call_data_chunk = {}
if hasattr(tool_call_chunk, 'model_dump'):
tool_call_data_chunk = tool_call_chunk.model_dump()
else:
# Manual extraction for compatibility
if hasattr(tool_call_chunk, 'id'):
tool_call_data_chunk['id'] = tool_call_chunk.id
if hasattr(tool_call_chunk, 'index'):
tool_call_data_chunk['index'] = tool_call_chunk.index
if hasattr(tool_call_chunk, 'type'):
tool_call_data_chunk['type'] = tool_call_chunk.type
if hasattr(tool_call_chunk, 'function'):
tool_call_data_chunk['function'] = {}
if hasattr(tool_call_chunk.function, 'name'):
tool_call_data_chunk['function']['name'] = tool_call_chunk.function.name
if hasattr(tool_call_chunk.function, 'arguments'):
args = tool_call_chunk.function.arguments
if isinstance(args, str):
tool_call_data_chunk['function']['arguments'] = args
else:
# Use ensure_ascii=False to preserve Unicode characters without escaping
tool_call_data_chunk['function']['arguments'] = json.dumps(args, ensure_ascii=False)
return tool_call_data_chunk
def is_tool_call_complete(tool_call_buffer_entry: Dict[str, Any]) -> bool:
"""
Check if a buffered tool call is complete (has all required fields and valid JSON arguments).
Args:
tool_call_buffer_entry: Dictionary from tool_calls_buffer with 'id', 'function' keys
Returns:
True if tool call is complete and ready to execute
"""
if not tool_call_buffer_entry:
return False
if not (tool_call_buffer_entry.get('id') and
tool_call_buffer_entry.get('function', {}).get('name') and
tool_call_buffer_entry.get('function', {}).get('arguments')):
return False
# Verify JSON arguments are complete and parse to a dict
try:
from core.utils.json_helpers import safe_json_parse
parsed = safe_json_parse(tool_call_buffer_entry['function']['arguments'])
# Must parse to a dict (not a string) to be considered complete
return isinstance(parsed, dict)
except (json.JSONDecodeError, TypeError):
return False
def _normalize_json_string_values(value: Any) -> Any:
"""
Recursively normalize JSON string values within a data structure.
LLMs often pass arrays/objects as JSON strings instead of native types.
For example: {"query": "[\"a\", \"b\"]"} instead of {"query": ["a", "b"]}
This function recursively traverses dicts/lists and parses any string
that looks like a JSON array or object. It also attempts to repair
malformed JSON that LLMs sometimes produce (e.g., missing closing brackets).
Args:
value: Any value to normalize
Returns:
Normalized value with JSON strings parsed into native types
"""
from core.utils.json_helpers import repair_json
if isinstance(value, dict):
return {k: _normalize_json_string_values(v) for k, v in value.items()}
if isinstance(value, list):
return [_normalize_json_string_values(item) for item in value]
if isinstance(value, str):
stripped = value.strip()
# Only try to parse if it looks like JSON array or object
if (stripped.startswith('[') and stripped.endswith(']')) or \
(stripped.startswith('{') and stripped.endswith('}')):
try:
parsed = json.loads(stripped)
# Recursively normalize the parsed result too
return _normalize_json_string_values(parsed)
except (json.JSONDecodeError, ValueError):
# Try to repair malformed JSON
repaired, was_repaired = repair_json(stripped)
if was_repaired:
try:
parsed = json.loads(repaired)
return _normalize_json_string_values(parsed)
except (json.JSONDecodeError, ValueError):
pass
# Not valid JSON even after repair, return original string
pass
return value
def parse_native_tool_call_arguments(arguments: Any) -> Dict[str, Any]:
"""
Parse native tool call arguments, handling both string and dict formats.
Also normalizes nested JSON strings (e.g. arrays passed as strings).
Includes automatic repair of malformed JSON from LLMs.
Args:
arguments: Arguments as string (JSON) or dict
Returns:
Parsed arguments as dict, or original value if parsing fails
"""
from core.utils.json_helpers import safe_json_parse_with_repair
if isinstance(arguments, dict):
# Normalize any JSON string values within the dict
return _normalize_json_string_values(arguments)
if isinstance(arguments, str):
# Use the repair-enabled parser
parsed = safe_json_parse_with_repair(arguments)
if isinstance(parsed, dict):
# Normalize any JSON string values within the parsed dict
return _normalize_json_string_values(parsed)
# If we got back a string (not parsed), return it
if isinstance(parsed, str):
return parsed
return arguments
return arguments
def convert_to_exec_tool_call(
tool_call: Any,
raw_arguments_str: Optional[str] = None
) -> Dict[str, Any]:
"""
Convert a native tool call object to exec_tool_call format.
Args:
tool_call: Native tool call object (from LiteLLM response) or dict
raw_arguments_str: Optional raw arguments string (if already extracted)
Returns:
Dictionary with 'function_name', 'arguments', 'id', 'raw_arguments', 'source'
"""
# Extract function name and ID
if isinstance(tool_call, dict):
function_name = tool_call.get('function', {}).get('name') or tool_call.get('function_name', 'unknown')
tool_call_id = tool_call.get('id') or str(uuid.uuid4())
if raw_arguments_str is None:
raw_arguments_str = tool_call.get('function', {}).get('arguments') or tool_call.get('raw_arguments', '')
else:
# LiteLLM object
function_name = tool_call.function.name if hasattr(tool_call, 'function') and hasattr(tool_call.function, 'name') else 'unknown'
tool_call_id = tool_call.id if hasattr(tool_call, 'id') else str(uuid.uuid4())
if raw_arguments_str is None:
if hasattr(tool_call, 'function') and hasattr(tool_call.function, 'arguments'):
args = tool_call.function.arguments
# Use ensure_ascii=False to preserve Unicode characters without escaping
raw_arguments_str = args if isinstance(args, str) else json.dumps(args, ensure_ascii=False)
else:
raw_arguments_str = ''
# Parse arguments
parsed_args = parse_native_tool_call_arguments(raw_arguments_str)
return {
"function_name": function_name,
"arguments": parsed_args if isinstance(parsed_args, dict) else raw_arguments_str,
"id": tool_call_id,
# Use ensure_ascii=False to preserve Unicode characters without escaping
"raw_arguments": raw_arguments_str if isinstance(raw_arguments_str, str) else json.dumps(raw_arguments_str, ensure_ascii=False),
"source": "native"
}
def convert_buffer_to_complete_tool_calls(tool_calls_buffer: Dict[int, Dict[str, Any]]) -> List[Dict[str, Any]]:
"""
Convert buffered tool calls to complete tool call format.
Includes automatic repair of malformed JSON from LLMs.
"""
from core.utils.json_helpers import repair_json
complete_tool_calls = []
for idx, tc_buf in tool_calls_buffer.items():
if tc_buf.get('id') and tc_buf.get('function', {}).get('name') and tc_buf.get('function', {}).get('arguments'):
arguments_str = tc_buf['function']['arguments']
try:
parsed = json.loads(arguments_str)
if not isinstance(parsed, dict):
logger.warning(f"Tool call {tc_buf.get('id')} has non-dict arguments, skipping")
continue
complete_tool_calls.append({
"id": tc_buf['id'],
"type": "function",
"function": {
"name": tc_buf['function']['name'],
"arguments": arguments_str
}
})
except (json.JSONDecodeError, TypeError) as e:
# Try to repair the JSON
repaired, was_repaired = repair_json(arguments_str)
if was_repaired:
try:
parsed = json.loads(repaired)
if isinstance(parsed, dict):
logger.info(f"🔧 Tool call {tc_buf.get('id')} repaired successfully")
complete_tool_calls.append({
"id": tc_buf['id'],
"type": "function",
"function": {
"name": tc_buf['function']['name'],
"arguments": repaired # Use repaired JSON
}
})
continue
except (json.JSONDecodeError, TypeError):
pass
logger.warning(f"Tool call {tc_buf.get('id')} has invalid JSON arguments (repair failed), skipping: {str(e)[:100]}")
continue
return complete_tool_calls
def convert_to_unified_tool_call_format(
tool_call: Dict[str, Any],
parse_arguments: bool = True
) -> Dict[str, Any]:
function_name = tool_call.get('function', {}).get('name', 'unknown')
tool_call_id = tool_call.get('id', str(uuid.uuid4()))
raw_arguments = tool_call.get('function', {}).get('arguments', '')
if parse_arguments:
arguments = parse_native_tool_call_arguments(raw_arguments)
else:
arguments = raw_arguments
return {
"tool_call_id": tool_call_id,
"function_name": function_name,
"arguments": arguments if isinstance(arguments, dict) else raw_arguments,
"source": "native"
}
def convert_buffer_to_metadata_tool_calls(
tool_calls_buffer: Dict[int, Dict[str, Any]],
include_partial: bool = False,
delta_mode: bool = False,
sent_lengths: Optional[Dict[int, int]] = None
) -> List[Dict[str, Any]]:
"""
Convert buffered tool calls to unified metadata format for streaming chunks.
Args:
tool_calls_buffer: Dictionary mapping index -> buffered tool call data
include_partial: Whether to include partial/incomplete tool calls (for streaming)
delta_mode: If True, only send new data (delta) not full accumulated content
sent_lengths: Dictionary tracking how much has been sent for each index (for delta mode)
Returns:
List of tool calls in unified metadata format (tool_call_id, function_name, arguments, source)
"""
unified_tool_calls = []
for idx in sorted(tool_calls_buffer.keys()):
tc_buf = tool_calls_buffer[idx]
# Only include tool calls that have at least a name and some arguments (even if partial)
if tc_buf.get('function', {}).get('name'):
# Arguments might be incomplete JSON string from LLM
arguments_str = tc_buf['function'].get('arguments', '')
# DELTA MODE: Only send the new chunk, not full accumulated content
if delta_mode and sent_lengths is not None:
prev_length = sent_lengths.get(idx, 0)
current_length = len(arguments_str)
# Only send if there's new content
if current_length <= prev_length:
continue # Nothing new to send
# Extract only the new delta
arguments_delta = arguments_str[prev_length:]
# Update tracking
sent_lengths[idx] = current_length
# For delta mode, always send as string (frontend will assemble)
unified_tool_calls.append({
"tool_call_id": tc_buf.get('id', f"streaming_tool_{idx}_{str(uuid.uuid4())}"),
"function_name": tc_buf['function']['name'],
"arguments_delta": arguments_delta, # Send ONLY the delta
"is_delta": True, # Flag to indicate this is a delta update
"source": "native"
})
else:
# FULL MODE: Send complete accumulated content (old behavior)
# Try to parse arguments as JSON - if successful, use parsed object
# If it fails (partial/incomplete JSON), keep as string
arguments: Any = arguments_str
if arguments_str:
try:
parsed = json.loads(arguments_str)
# Successfully parsed - normalize nested JSON string values
# (e.g. arrays passed as strings like attachments: "[\"file1\", \"file2\"]")
arguments = _normalize_json_string_values(parsed)
except json.JSONDecodeError:
# Partial/incomplete JSON - keep as string for frontend to handle
arguments = arguments_str
unified_tool_calls.append({
"tool_call_id": tc_buf.get('id', f"streaming_tool_{idx}_{str(uuid.uuid4())}"),
"function_name": tc_buf['function']['name'],
"arguments": arguments, # Object if valid JSON, string if partial
"source": "native" # Always native for native tool calls
})
return unified_tool_calls
|