Bases: BaseToolCallParser
Parser for extracting non-JSON formatted tool calls from text.
Specializes in parsing tool calls that use custom formats like
{args}.
Example
config = {
"formats": {
"non_json_format": {
"function_call_pattern": r'<function=(.*?)>{(.*?)}</function>'
}
}
}
parser = NonJSONToolCallParser(config)
result = parser.parse('<function=my_tool>{"arg1": "value"}</function>')
Source code in src/tools/core/parsers/non_json_tool_call_parser.py
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 | class NonJSONToolCallParser(BaseToolCallParser):
"""Parser for extracting non-JSON formatted tool calls from text.
Specializes in parsing tool calls that use custom formats like
<function=name>{args}</function>.
Example:
```python
config = {
"formats": {
"non_json_format": {
"function_call_pattern": r'<function=(.*?)>{(.*?)}</function>'
}
}
}
parser = NonJSONToolCallParser(config)
result = parser.parse('<function=my_tool>{"arg1": "value"}</function>')
```
"""
def extract(self, text: str) -> Dict[str, Any]:
"""Extract non-JSON tool calls using regex patterns.
Searches for tool calls using configured regex patterns and parses
their arguments as JSON.
Args:
text (str): Cleaned input text containing tool calls.
Returns:
Dict[str, Any]: Parsed tool calls or error information.
- Success format: {"tool_calls": [{"name": "...", "arguments": {...}}, ...]}
- Error format: {"error": "error message"}
- No tool calls: {"content": "original text"}
"""
pattern = self.config.get("formats").get("non_json_format").get("function_call_pattern")
matches = re.findall(pattern, text)
if not matches:
return {"content": text}
tool_calls = []
for match in matches:
try:
tool_calls.append({
"name": match[0],
"arguments": json.loads(match[1]) # Parse arguments as JSON
})
except json.JSONDecodeError:
return {"error": f"Failed to parse arguments for function: {match[0]}"}
return {"tool_calls": tool_calls}
|
Extract non-JSON tool calls using regex patterns.
Searches for tool calls using configured regex patterns and parses
their arguments as JSON.
Parameters:
Name |
Type |
Description |
Default |
text
|
str
|
Cleaned input text containing tool calls.
|
required
|
Returns:
Type |
Description |
Dict[str, Any]
|
Dict[str, Any]: Parsed tool calls or error information.
- Success format: {"tool_calls": [{"name": "...", "arguments": {...}}, ...]}
- Error format: {"error": "error message"}
- No tool calls: {"content": "original text"}
|
Source code in src/tools/core/parsers/non_json_tool_call_parser.py
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 | def extract(self, text: str) -> Dict[str, Any]:
"""Extract non-JSON tool calls using regex patterns.
Searches for tool calls using configured regex patterns and parses
their arguments as JSON.
Args:
text (str): Cleaned input text containing tool calls.
Returns:
Dict[str, Any]: Parsed tool calls or error information.
- Success format: {"tool_calls": [{"name": "...", "arguments": {...}}, ...]}
- Error format: {"error": "error message"}
- No tool calls: {"content": "original text"}
"""
pattern = self.config.get("formats").get("non_json_format").get("function_call_pattern")
matches = re.findall(pattern, text)
if not matches:
return {"content": text}
tool_calls = []
for match in matches:
try:
tool_calls.append({
"name": match[0],
"arguments": json.loads(match[1]) # Parse arguments as JSON
})
except json.JSONDecodeError:
return {"error": f"Failed to parse arguments for function: {match[0]}"}
return {"tool_calls": tool_calls}
|