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,9 +517,11 @@ 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('}]'):
_pat = next((p for p in ['[{"type":"tool_use"', '[{"type": "tool_use"'] if p in content), None)
if _pat:
try: try:
idx = content.index('[{"type":"tool_use"') idx = content.index(_pat)
raw = json.loads(content[idx:]) raw = json.loads(content[idx:])
tool_calls = [MockToolCall(b["name"], b.get("input", {}), id=b.get("id", "")) for b in raw if b.get("type") == "tool_use"] 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() content = content[:idx].strip()