dexifried commited on
Commit
71a75b4
·
1 Parent(s): b7c551a

Fix tool extraction: raw JSON decode for Qwen3 plain output

Browse files
Files changed (1) hide show
  1. app.py +16 -0
app.py CHANGED
@@ -101,10 +101,26 @@ def _execute_tool(name: str, args: dict) -> str:
101
  return f"Tool error: {type(e).__name__}: {e}"
102
 
103
  def _extract_tool_calls(text: str):
 
 
104
  calls = []
 
105
  for m in re.finditer(r'', text, re.DOTALL):
106
  try: calls.append(json.loads(m.group(1).strip()))
107
  except: pass
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  return calls
109
 
110
  def _dispatch_tool_calls(calls: list):
 
101
  return f"Tool error: {type(e).__name__}: {e}"
102
 
103
  def _extract_tool_calls(text: str):
104
+ """Extract JSON tool calls from LLM output.
105
+ Handles: raw JSON, blocks, embedded JSON objects."""
106
  calls = []
107
+ # Method 1: blocks (legacy format)
108
  for m in re.finditer(r'', text, re.DOTALL):
109
  try: calls.append(json.loads(m.group(1).strip()))
110
  except: pass
111
+ if calls:
112
+ return calls
113
+ # Method 2: Raw JSON objects anywhere in text (Qwen3 outputs plain JSON)
114
+ decoder = json.JSONDecoder()
115
+ pos = 0
116
+ while pos < len(text):
117
+ try:
118
+ obj, end = decoder.raw_decode(text, pos)
119
+ if isinstance(obj, dict) and "name" in obj and "arguments" in obj:
120
+ calls.append(obj)
121
+ pos = end
122
+ except json.JSONDecodeError:
123
+ pos += 1
124
  return calls
125
 
126
  def _dispatch_tool_calls(calls: list):