Fix tool call argument serialization issue (#39)
* Tool calling arguments JSON fix * Extracted duplicated code to helper function * Update utils.py --------- Co-authored-by: Game_Time <108236317+RayBytes@users.noreply.github.com>
This commit is contained in:
@@ -254,6 +254,30 @@ def sse_translate_chat(
|
|||||||
ws_index: dict[str, int] = {}
|
ws_index: dict[str, int] = {}
|
||||||
ws_next_index: int = 0
|
ws_next_index: int = 0
|
||||||
|
|
||||||
|
def _serialize_tool_args(eff_args: Any) -> str:
|
||||||
|
"""
|
||||||
|
Serialize tool call arguments with proper JSON handling.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
eff_args: Arguments to serialize (dict, list, str, or other)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
JSON string representation of the arguments
|
||||||
|
"""
|
||||||
|
if isinstance(eff_args, (dict, list)):
|
||||||
|
return json.dumps(eff_args)
|
||||||
|
elif isinstance(eff_args, str):
|
||||||
|
try:
|
||||||
|
parsed = json.loads(eff_args)
|
||||||
|
if isinstance(parsed, (dict, list)):
|
||||||
|
return json.dumps(parsed)
|
||||||
|
else:
|
||||||
|
return json.dumps({"query": eff_args})
|
||||||
|
except (json.JSONDecodeError, ValueError):
|
||||||
|
return json.dumps({"query": eff_args})
|
||||||
|
else:
|
||||||
|
return "{}"
|
||||||
|
|
||||||
def _extract_usage(evt: Dict[str, Any]) -> Dict[str, int] | None:
|
def _extract_usage(evt: Dict[str, Any]) -> Dict[str, int] | None:
|
||||||
try:
|
try:
|
||||||
usage = (evt.get("response") or {}).get("usage")
|
usage = (evt.get("response") or {}).get("usage")
|
||||||
@@ -320,12 +344,7 @@ def sse_translate_chat(
|
|||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
eff_params = ws_state.get(call_id, params if isinstance(params, (dict, list, str)) else {})
|
eff_params = ws_state.get(call_id, params if isinstance(params, (dict, list, str)) else {})
|
||||||
if isinstance(eff_params, (dict, list)):
|
args_str = _serialize_tool_args(eff_params)
|
||||||
args_str = json.dumps(eff_params)
|
|
||||||
elif isinstance(eff_params, str):
|
|
||||||
args_str = json.dumps({"query": eff_params})
|
|
||||||
else:
|
|
||||||
args_str = "{}"
|
|
||||||
if call_id not in ws_index:
|
if call_id not in ws_index:
|
||||||
ws_index[call_id] = ws_next_index
|
ws_index[call_id] = ws_next_index
|
||||||
ws_next_index += 1
|
ws_next_index += 1
|
||||||
@@ -402,12 +421,7 @@ def sse_translate_chat(
|
|||||||
pass
|
pass
|
||||||
eff_args = ws_state.get(call_id, raw_args if isinstance(raw_args, (dict, list, str)) else {})
|
eff_args = ws_state.get(call_id, raw_args if isinstance(raw_args, (dict, list, str)) else {})
|
||||||
try:
|
try:
|
||||||
if isinstance(eff_args, (dict, list)):
|
args = _serialize_tool_args(eff_args)
|
||||||
args = json.dumps(eff_args)
|
|
||||||
elif isinstance(eff_args, str):
|
|
||||||
args = json.dumps({"query": eff_args})
|
|
||||||
else:
|
|
||||||
args = "{}"
|
|
||||||
except Exception:
|
except Exception:
|
||||||
args = "{}"
|
args = "{}"
|
||||||
if item.get("type") == "web_search_call" and verbose and vlog:
|
if item.get("type") == "web_search_call" and verbose and vlog:
|
||||||
|
|||||||
Reference in New Issue
Block a user