fix: fallback tool_use parser handles both with/without space in type key

This commit is contained in:
Liang Jiaqing
2026-03-29 21:04:34 +08:00
parent 58e2f45793
commit f0dea09123

View File

@@ -517,13 +517,15 @@ class NativeOAISession:
text_parts = [b["text"] for b in content_blocks if b.get("type") == "text"] text_parts = [b["text"] for b in content_blocks if b.get("type") == "text"]
content = "\n".join(text_parts).strip() content = "\n".join(text_parts).strip()
tool_calls = [MockToolCall(b["name"], b.get("input", {}), id=b.get("id", "")) for b in content_blocks if b.get("type") == "tool_use"] tool_calls = [MockToolCall(b["name"], b.get("input", {}), id=b.get("id", "")) for b in content_blocks if b.get("type") == "tool_use"]
if len(tool_calls) == 0 and content.endswith('}]') and '[{"type":"tool_use"' in content: if len(tool_calls) == 0 and content.endswith('}]'):
try: _pat = next((p for p in ['[{"type":"tool_use"', '[{"type": "tool_use"'] if p in content), None)
idx = content.index('[{"type":"tool_use"') if _pat:
raw = json.loads(content[idx:]) try:
tool_calls = [MockToolCall(b["name"], b.get("input", {}), id=b.get("id", "")) for b in raw if b.get("type") == "tool_use"] idx = content.index(_pat)
content = content[:idx].strip() raw = json.loads(content[idx:])
except: pass tool_calls = [MockToolCall(b["name"], b.get("input", {}), id=b.get("id", "")) for b in raw if b.get("type") == "tool_use"]
content = content[:idx].strip()
except: pass
think_pattern = r"<think(?:ing)?>(.*?)</think(?:ing)?>"; thinking = '' think_pattern = r"<think(?:ing)?>(.*?)</think(?:ing)?>"; thinking = ''
think_match = re.search(think_pattern, content, re.DOTALL) think_match = re.search(think_pattern, content, re.DOTALL)
if think_match: if think_match: