A high-throughput and memory-efficient inference and serving engine for LLMs
Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git
Synced Nov 14, 05:30 UTC
Olmo 3 tool parser and tests (#26143)
Signed-off-by: Pradeep Dasigi <pradeepd@allenai.org>
Pradeep Dasigi committed
Oct 15, 2025 at 09:36 UTC
4794c2bd92d9066babef217cbb0a1b9d3d85f483
4 files changed
+623
docs/features/tool_calling.md
+10
@@ -352,6 +352,16 @@ Supported models:
352
353
Flags: `--tool-call-parser qwen3_xml`
354
355
+### Olmo 3 Models (`olmo3`)
356
+
357
+Olmo 3 models output tool calls in a format that is very similar to the one expected by the `pythonic` parser (see below), with a few differences. Each tool call is a pythonic string, but the parallel tool calls are newline-delimited, and the calls are wrapped within XML tags as `<function_calls>..</function_calls>`. In addition, the parser also allows JSON boolean and null literals (`true`, `false`, and `null`) in addition to the pythonic ones (`True`, `False`, and `None`).
358
+
359
+Supported models:
360
+
361
+* TODO (will be updated after Olmo 3 release)
362
+
363
+Flags: `--tool-call-parser olmo3`
364
+
365
### Models with Pythonic Tool Calls (`pythonic`)
366
367
A growing number of models output a python list to represent tool calls instead of using JSON. This has the advantage of inherently supporting parallel tool calls and removing ambiguity around the JSON schema required for tool calls. The `pythonic` tool parser can support such models.
tests/entrypoints/openai/tool_parsers/test_olmo3_tool_parser.py
new
+243
@@ -0,0 +1,243 @@
1
+# SPDX-License-Identifier: Apache-2.0
2
+# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
+
4
+from unittest.mock import MagicMock, patch
5
+
6
+import pytest
7
+
8
+from tests.entrypoints.openai.tool_parsers.utils import (
9
+ run_tool_extraction,
10
+ run_tool_extraction_streaming,
11
+)
12
+from vllm.entrypoints.openai.protocol import FunctionCall
13
+from vllm.entrypoints.openai.tool_parsers import ToolParser, ToolParserManager
14
+
15
+# https://github.com/meta-llama/llama-models/blob/main/models/llama3_2/text_prompt_format.md#model-response-format-1
16
+SIMPLE_FUNCTION_OUTPUT = "get_weather(city='San Francisco', metric='celsius')"
17
+SIMPLE_FUNCTION_CALL = FunctionCall(
18
+ name="get_weather",
19
+ arguments='{"city": "San Francisco", "metric": "celsius"}',
20
+)
21
+MORE_TYPES_FUNCTION_OUTPUT = (
22
+ "register_user(name='John Doe', "
23
+ "age=37, "
24
+ "address={'city': 'San Francisco', 'state': 'CA'}, "
25
+ "role=None, "
26
+ "passed_test=True, "
27
+ "aliases=['John', 'Johnny'])"
28
+)
29
+MORE_TYPES_FUNCTION_OUTPUT_JSON_LITERALS = (
30
+ "register_user(name='John Doe', "
31
+ "age=37, "
32
+ "address={'city': 'San Francisco', 'state': 'CA'}, "
33
+ "role=null, "
34
+ "passed_test=true, "
35
+ "aliases=['John', 'Johnny'])"
36
+)
37
+MORE_TYPES_FUNCTION_CALL = FunctionCall(
38
+ name="register_user",
39
+ arguments='{"name": "John Doe", '
40
+ '"age": 37, '
41
+ '"address": {"city": "San Francisco", "state": "CA"}, '
42
+ '"role": null, '
43
+ '"passed_test": true, '
44
+ '"aliases": ["John", "Johnny"]}',
45
+)
46
+PARAMETERLESS_FUNCTION_OUTPUT = "get_weather()"
47
+PARAMETERLESS_FUNCTION_CALL = FunctionCall(
48
+ name="get_weather",
49
+ arguments="{}",
50
+)
51
+EMPTY_DICT_FUNCTION_OUTPUT = "do_something_cool(additional_data={})"
52
+EMPTY_DICT_FUNCTION_CALL = FunctionCall(
53
+ name="do_something_cool",
54
+ arguments='{"additional_data": {}}',
55
+)
56
+EMPTY_LIST_FUNCTION_OUTPUT = "do_something_cool(steps=[])"
57
+EMPTY_LIST_FUNCTION_CALL = FunctionCall(
58
+ name="do_something_cool",
59
+ arguments='{"steps": []}',
60
+)
61
+ESCAPED_STRING_FUNCTION_OUTPUT = (
62
+ r"get_weather(city='Martha\'s Vineyard', metric='\"cool units\"')"
63
+)
64
+ESCAPED_STRING_FUNCTION_CALL = FunctionCall(
65
+ name="get_weather",
66
+ arguments='{"city": "Martha\'s Vineyard", "metric": "\\"cool units\\""}',
67
+)
68
+
69
+
70
+@pytest.mark.parametrize("streaming", [True, False])
71
+def test_no_tool_call(streaming: bool):
72
+ mock_tokenizer = MagicMock()
73
+ tool_parser: ToolParser = ToolParserManager.get_tool_parser("olmo3")(mock_tokenizer)
74
+ model_output = "How can I help you today?"
75
+
76
+ content, tool_calls = run_tool_extraction(
77
+ tool_parser, model_output, streaming=streaming
78
+ )
79
+
80
+ assert content == model_output
81
+ assert len(tool_calls) == 0
82
+
83
+
84
+TEST_CASES = [
85
+ pytest.param(
86
+ True,
87
+ f"<function_calls>{SIMPLE_FUNCTION_OUTPUT}</function_calls>",
88
+ [SIMPLE_FUNCTION_CALL],
89
+ id="simple_streaming",
90
+ ),
91
+ pytest.param(
92
+ False,
93
+ f"<function_calls>{SIMPLE_FUNCTION_OUTPUT}</function_calls>",
94
+ [SIMPLE_FUNCTION_CALL],
95
+ id="simple_nonstreaming",
96
+ ),
97
+ pytest.param(
98
+ True,
99
+ f"<function_calls>{MORE_TYPES_FUNCTION_OUTPUT}</function_calls>",
100
+ [MORE_TYPES_FUNCTION_CALL],
101
+ id="more_types_streaming",
102
+ ),
103
+ pytest.param(
104
+ False,
105
+ f"<function_calls>{MORE_TYPES_FUNCTION_OUTPUT}</function_calls>",
106
+ [MORE_TYPES_FUNCTION_CALL],
107
+ id="more_types_nonstreaming",
108
+ ),
109
+ pytest.param(
110
+ True,
111
+ f"<function_calls>{MORE_TYPES_FUNCTION_OUTPUT_JSON_LITERALS}</function_calls>",
112
+ [MORE_TYPES_FUNCTION_CALL],
113
+ id="more_types_streaming_json_literals",
114
+ ),
115
+ pytest.param(
116
+ False,
117
+ f"<function_calls>{MORE_TYPES_FUNCTION_OUTPUT_JSON_LITERALS}</function_calls>",
118
+ [MORE_TYPES_FUNCTION_CALL],
119
+ id="more_types_nonstreaming_json_literals",
120
+ ),
121
+ pytest.param(
122
+ True,
123
+ f"<function_calls>{PARAMETERLESS_FUNCTION_OUTPUT}</function_calls>",
124
+ [PARAMETERLESS_FUNCTION_CALL],
125
+ id="parameterless_streaming",
126
+ ),
127
+ pytest.param(
128
+ False,
129
+ f"<function_calls>{PARAMETERLESS_FUNCTION_OUTPUT}</function_calls>",
130
+ [PARAMETERLESS_FUNCTION_CALL],
131
+ id="parameterless_nonstreaming",
132
+ ),
133
+ pytest.param(
134
+ True,
135
+ f"<function_calls>{EMPTY_DICT_FUNCTION_OUTPUT}</function_calls>",
136
+ [EMPTY_DICT_FUNCTION_CALL],
137
+ id="empty_dict_streaming",
138
+ ),
139
+ pytest.param(
140
+ False,
141
+ f"<function_calls>{EMPTY_DICT_FUNCTION_OUTPUT}</function_calls>",
142
+ [EMPTY_DICT_FUNCTION_CALL],
143
+ id="empty_dict_nonstreaming",
144
+ ),
145
+ pytest.param(
146
+ True,
147
+ f"<function_calls>{EMPTY_LIST_FUNCTION_OUTPUT}</function_calls>",
148
+ [EMPTY_LIST_FUNCTION_CALL],
149
+ id="empty_list_streaming",
150
+ ),
151
+ pytest.param(
152
+ False,
153
+ f"<function_calls>{EMPTY_LIST_FUNCTION_OUTPUT}</function_calls>",
154
+ [EMPTY_LIST_FUNCTION_CALL],
155
+ id="empty_list_nonstreaming",
156
+ ),
157
+ pytest.param(
158
+ True,
159
+ f"<function_calls>{ESCAPED_STRING_FUNCTION_OUTPUT}</function_calls>",
160
+ [ESCAPED_STRING_FUNCTION_CALL],
161
+ id="escaped_string_streaming",
162
+ ),
163
+ pytest.param(
164
+ False,
165
+ f"<function_calls>{ESCAPED_STRING_FUNCTION_OUTPUT}</function_calls>",
166
+ [ESCAPED_STRING_FUNCTION_CALL],
167
+ id="escaped_string_nonstreaming",
168
+ ),
169
+ pytest.param(
170
+ True,
171
+ f"<function_calls>{SIMPLE_FUNCTION_OUTPUT}\n{MORE_TYPES_FUNCTION_OUTPUT}</function_calls>",
172
+ [SIMPLE_FUNCTION_CALL, MORE_TYPES_FUNCTION_CALL],
173
+ id="parallel_calls_streaming",
174
+ ),
175
+ pytest.param(
176
+ False,
177
+ f"<function_calls>{SIMPLE_FUNCTION_OUTPUT}\n{MORE_TYPES_FUNCTION_OUTPUT}</function_calls>",
178
+ [SIMPLE_FUNCTION_CALL, MORE_TYPES_FUNCTION_CALL],
179
+ id="parallel_calls_nonstreaming",
180
+ ),
181
+]
182
+
183
+
184
+@pytest.mark.parametrize("streaming, model_output, expected_tool_calls", TEST_CASES)
185
+def test_tool_call(
186
+ streaming: bool, model_output: str, expected_tool_calls: list[FunctionCall]
187
+):
188
+ mock_tokenizer = MagicMock()
189
+ tool_parser: ToolParser = ToolParserManager.get_tool_parser("olmo3")(mock_tokenizer)
190
+
191
+ content, tool_calls = run_tool_extraction(
192
+ tool_parser, model_output, streaming=streaming
193
+ )
194
+
195
+ assert content is None
196
+ assert len(tool_calls) == len(expected_tool_calls)
197
+ for actual, expected in zip(tool_calls, expected_tool_calls):
198
+ assert actual.type == "function"
199
+ assert actual.function == expected
200
+
201
+
202
+def test_streaming_tool_call_with_large_steps():
203
+ mock_tokenizer = MagicMock()
204
+ tool_parser: ToolParser = ToolParserManager.get_tool_parser("olmo3")(mock_tokenizer)
205
+ model_output_deltas = [
206
+ "<function_calls>get_weather(city='San",
207
+ " Francisco', metric='celsius')\n"
208
+ f"{PARAMETERLESS_FUNCTION_OUTPUT}\n"
209
+ f"{EMPTY_LIST_FUNCTION_OUTPUT}</function_calls>",
210
+ ]
211
+
212
+ reconstructor = run_tool_extraction_streaming(
213
+ tool_parser, model_output_deltas, assert_one_tool_per_delta=False
214
+ )
215
+
216
+ assert reconstructor.other_content == ""
217
+ assert len(reconstructor.tool_calls) == 3
218
+ assert reconstructor.tool_calls[0].function == SIMPLE_FUNCTION_CALL
219
+ assert reconstructor.tool_calls[1].function == PARAMETERLESS_FUNCTION_CALL
220
+ assert reconstructor.tool_calls[2].function == EMPTY_LIST_FUNCTION_CALL
221
+
222
+
223
+@pytest.mark.parametrize("streaming", [False])
224
+def test_regex_timeout_handling(streaming: bool):
225
+ """test regex timeout is handled gracefully"""
226
+ mock_tokenizer = MagicMock()
227
+ tool_parser: ToolParser = ToolParserManager.get_tool_parser("olmo3")(mock_tokenizer)
228
+
229
+ fake_problematic_input = "hello world[A(A=" + "\t)A(A=,\t" * 2
230
+
231
+ # create a mock regex that raises TimeoutError
232
+ mock_regex = MagicMock()
233
+ mock_regex.match.side_effect = TimeoutError("Regex timeout")
234
+
235
+ with patch.object(tool_parser, "TOOL_CALL_REGEX", mock_regex):
236
+ content, tool_calls = run_tool_extraction(
237
+ tool_parser, fake_problematic_input, streaming=streaming
238
+ )
239
+
240
+ # should treat as regular text when regex times out
241
+ assert content == fake_problematic_input
242
+ assert len(tool_calls) == 0
243
+ mock_regex.match.assert_called_once()
vllm/entrypoints/openai/tool_parsers/__init__.py
+2
@@ -18,6 +18,7 @@ from .llama_tool_parser import Llama3JsonToolParser
18
from .longcat_tool_parser import LongcatFlashToolParser
19
from .minimax_tool_parser import MinimaxToolParser
20
from .mistral_tool_parser import MistralToolParser
21
+from .olmo3_tool_parser import Olmo3PythonicToolParser
22
from .openai_tool_parser import OpenAIToolParser
23
from .phi4mini_tool_parser import Phi4MiniJsonToolParser
24
from .pythonic_tool_parser import PythonicToolParser
@@ -45,6 +46,7 @@ __all__ = [
46
"DeepSeekV31ToolParser",
47
"Ernie45ToolParser",
48
"xLAMToolParser",
49
+ "Olmo3PythonicToolParser",
50
"MinimaxToolParser",
51
"KimiK2ToolParser",
52
"HunyuanA13BToolParser",
vllm/entrypoints/openai/tool_parsers/olmo3_tool_parser.py
new
+368
@@ -0,0 +1,368 @@
1
+# SPDX-License-Identifier: Apache-2.0
2
+# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
+import ast
4
+import json
5
+from collections.abc import Sequence
6
+from typing import Any
7
+
8
+import regex as re
9
+from transformers import PreTrainedTokenizerBase
10
+
11
+import vllm.envs as envs
12
+from vllm.entrypoints.openai.protocol import (
13
+ ChatCompletionRequest,
14
+ DeltaFunctionCall,
15
+ DeltaMessage,
16
+ DeltaToolCall,
17
+ ExtractedToolCallInformation,
18
+ FunctionCall,
19
+ ToolCall,
20
+)
21
+from vllm.entrypoints.openai.tool_parsers.abstract_tool_parser import (
22
+ ToolParser,
23
+ ToolParserManager,
24
+)
25
+from vllm.logger import init_logger
26
+
27
+logger = init_logger(__name__)
28
+
29
+
30
+class _UnexpectedAstError(Exception):
31
+ pass
32
+
33
+
34
+@ToolParserManager.register_module("olmo3")
35
+class Olmo3PythonicToolParser(ToolParser):
36
+ """
37
+ Tool call parser for Olmo 3 models that produce tool calls as
38
+ newline-separated pythonic strings.
39
+ Used when --enable-auto-tool-choice --tool-call-parser pythonic are all set
40
+ Code copied from pythonic_tool_parser.py and updated to handle
41
+ - newline separated pythonic tool calls.
42
+ - argument values being null/true/false instead of Pythonic literals.
43
+ """
44
+
45
+ # TODO(mdepinet): Possible future improvements:
46
+ # 1. Support text + tools separated by either <|python_tag|> or \n\n
47
+ # 2. Support tools outside of a list (or separated by a semicolon).
48
+ # This depends on item 1 for consistent streaming.
49
+ # Neither of these are necessary for e.g. ToolACE, but both would help make
50
+ # Llama3.2 models more reliable.
51
+
52
+ TOOL_CALL_REGEX = re.compile(
53
+ r"\[([a-zA-Z]+\w*\(([a-zA-Z]+\w*=.*,\s*)*([a-zA-Z]+\w*=.*\s)?\),\s*)*([a-zA-Z]+\w*\(([a-zA-Z]+\w*=.*,\s*)*([a-zA-Z]+\w*=.*\s*)?\)\s*)+\]",
54
+ re.DOTALL,
55
+ )
56
+
57
+ def __init__(self, tokenizer: PreTrainedTokenizerBase):
58
+ super().__init__(tokenizer)
59
+
60
+ # Rename for readability. This is NOT a tool id.
61
+ @property
62
+ def current_tool_index(self) -> int:
63
+ return self.current_tool_id
64
+
65
+ @current_tool_index.setter
66
+ def current_tool_index(self, value: int) -> None:
67
+ self.current_tool_id = value
68
+
69
+ def extract_tool_calls(
70
+ self, model_output: str, request: ChatCompletionRequest
71
+ ) -> ExtractedToolCallInformation:
72
+ """
73
+ Extract the tool calls from a complete model response.
74
+ """
75
+ original_model_output = model_output
76
+ # Remove xml tags.
77
+ match = re.search(
78
+ r"<function_calls>(.*?)</function_calls>", model_output, re.DOTALL
79
+ )
80
+ if match:
81
+ model_output = match.group(1).strip()
82
+ # Make the newline separated function calls into a list.
83
+ model_output = ", ".join(
84
+ [line.strip() for line in model_output.splitlines() if line.strip()]
85
+ )
86
+ model_output = f"[{model_output}]"
87
+
88
+ is_tool_call_pattern = False
89
+ try:
90
+ is_tool_call_pattern = (
91
+ self.TOOL_CALL_REGEX.match(
92
+ model_output, timeout=envs.VLLM_TOOL_PARSE_REGEX_TIMEOUT_SECONDS
93
+ )
94
+ is not None
95
+ )
96
+ except TimeoutError:
97
+ logger.warning("Regex timeout occurred when matching tool call pattern.")
98
+ logger.debug(
99
+ "Regex timeout occurred when matching user input: %s", model_output
100
+ )
101
+
102
+ if not is_tool_call_pattern:
103
+ return ExtractedToolCallInformation(
104
+ tools_called=False, tool_calls=[], content=original_model_output
105
+ )
106
+
107
+ try:
108
+ module = ast.parse(model_output)
109
+ parsed = getattr(module.body[0], "value", None)
110
+ if isinstance(parsed, ast.List) and all(
111
+ isinstance(e, ast.Call) for e in parsed.elts
112
+ ):
113
+ return ExtractedToolCallInformation(
114
+ tools_called=True,
115
+ tool_calls=[
116
+ _handle_single_tool(e) # type: ignore
117
+ for e in parsed.elts
118
+ ],
119
+ content=None,
120
+ )
121
+ else:
122
+ raise _UnexpectedAstError(
123
+ "Tool output must be a list of function calls"
124
+ )
125
+ except Exception:
126
+ logger.exception("Error in extracting tool call from response.")
127
+ # Treat as regular text
128
+ return ExtractedToolCallInformation(
129
+ tools_called=False, tool_calls=[], content=original_model_output
130
+ )
131
+
132
+ def extract_tool_calls_streaming(
133
+ self,
134
+ previous_text: str,
135
+ current_text: str,
136
+ delta_text: str,
137
+ previous_token_ids: Sequence[int],
138
+ current_token_ids: Sequence[int],
139
+ delta_token_ids: Sequence[int],
140
+ request: ChatCompletionRequest,
141
+ ) -> DeltaMessage | None:
142
+ # All function calls start with the <function_calls> tag.
143
+ # But since this is streaming, we may have seen only part of the tag.
144
+ if not current_text.startswith("<"):
145
+ return DeltaMessage(content=delta_text)
146
+
147
+ try:
148
+ # Remove xml tags.
149
+ if current_text.startswith("<function_calls>"):
150
+ current_text = current_text[len("<function_calls>") :]
151
+ if current_text.endswith("</function_calls>"):
152
+ current_text = current_text[: -len("</function_calls>")]
153
+
154
+ valid_and_added_text = _make_valid_python(current_text)
155
+ if valid_and_added_text is None:
156
+ return None
157
+ valid_text, added_text = valid_and_added_text
158
+
159
+ # Make the newline separated function calls into a list.
160
+ valid_text = ", ".join(
161
+ [line.strip() for line in valid_text.splitlines() if line.strip()]
162
+ )
163
+ valid_text = f"[{valid_text}]"
164
+ module = ast.parse(valid_text)
165
+ parsed = getattr(module.body[0], "value", None)
166
+ if not isinstance(parsed, ast.List) or not all(
167
+ isinstance(e, ast.Call) for e in parsed.elts
168
+ ):
169
+ raise _UnexpectedAstError(
170
+ "Tool output must be a sequence of newline-separated calls"
171
+ )
172
+ tool_calls = [
173
+ _handle_single_tool(e) # type: ignore
174
+ for e in parsed.elts
175
+ ]
176
+
177
+ tool_deltas = []
178
+ for index, new_call in enumerate(tool_calls):
179
+ if index < self.current_tool_index:
180
+ continue
181
+
182
+ self.current_tool_index = index
183
+ if len(self.streamed_args_for_tool) == index:
184
+ self.streamed_args_for_tool.append("")
185
+
186
+ new_call_complete = index < len(tool_calls) - 1 or ")" not in added_text
187
+ if new_call_complete:
188
+ self.current_tool_index += 1
189
+
190
+ withheld_suffix = added_text[:-1] if not new_call_complete else ""
191
+ if not new_call_complete and added_text[-1] == ")":
192
+ # Function call is incomplete. Withhold the closing bracket.
193
+ withheld_suffix = withheld_suffix + "}"
194
+ # Strings get single quotes in the model-produced string.
195
+ # JSON requires double quotes.
196
+ withheld_suffix = withheld_suffix.replace("'", '"')
197
+ delta = _compute_tool_delta(
198
+ self.streamed_args_for_tool[index], new_call, index, withheld_suffix
199
+ )
200
+
201
+ if delta is not None:
202
+ tool_deltas.append(delta)
203
+ if (
204
+ delta.function is not None
205
+ and delta.function.arguments is not None
206
+ ):
207
+ self.streamed_args_for_tool[index] += delta.function.arguments
208
+
209
+ # HACK: serving_chat.py inspects the internal state of tool parsers
210
+ # when determining its final streaming delta, automatically
211
+ # adding autocompleted JSON.
212
+ # These two lines avoid that nonsense while ensuring finish_reason
213
+ # is set to tool_calls when at least one tool is called.
214
+ if tool_deltas and not self.prev_tool_call_arr:
215
+ self.prev_tool_call_arr = [{"arguments": {}}]
216
+
217
+ if tool_deltas:
218
+ return DeltaMessage(tool_calls=tool_deltas)
219
+ elif not added_text and self.current_tool_id > 0:
220
+ # Return an empty DeltaMessage once the tool calls are all done
221
+ # so that finish_reason gets set.
222
+ return DeltaMessage(content="")
223
+ else:
224
+ return None
225
+ except Exception:
226
+ logger.exception("Error trying to handle streaming tool call.")
227
+ logger.debug(
228
+ "Skipping chunk as a result of tool streaming extraction error"
229
+ )
230
+ return None
231
+
232
+
233
+def _get_parameter_value(val: ast.expr) -> Any:
234
+ if isinstance(val, ast.Constant):
235
+ return val.value
236
+ elif isinstance(val, ast.Dict):
237
+ if not all(isinstance(k, ast.Constant) for k in val.keys):
238
+ raise _UnexpectedAstError("Dict tool call arguments must have literal keys")
239
+ return {
240
+ k.value: _get_parameter_value(v) # type: ignore
241
+ for k, v in zip(val.keys, val.values)
242
+ }
243
+ elif isinstance(val, ast.List):
244
+ return [_get_parameter_value(v) for v in val.elts]
245
+ # The model may return function calls where the values are null/true/false
246
+ # because the system prompt has API description in json.
247
+ elif isinstance(val, ast.Name) and val.id in ["null", "true", "false"]:
248
+ if val.id == "null":
249
+ return None
250
+ elif val.id == "true":
251
+ return True
252
+ elif val.id == "false":
253
+ return False
254
+ else:
255
+ raise _UnexpectedAstError("Tool call arguments must be literals")
256
+
257
+
258
+def _handle_single_tool(call: ast.Call) -> ToolCall:
259
+ if not isinstance(call.func, ast.Name):
260
+ raise _UnexpectedAstError("Invalid tool call name")
261
+ function_name = call.func.id
262
+ arguments = {}
263
+ for keyword in call.keywords:
264
+ arguments[keyword.arg] = _get_parameter_value(keyword.value)
265
+ return ToolCall(
266
+ type="function",
267
+ function=FunctionCall(
268
+ name=function_name, arguments=json.dumps(arguments, ensure_ascii=False)
269
+ ),
270
+ )
271
+
272
+
273
+def _make_valid_python(text: str) -> tuple[str, str] | None:
274
+ bracket_stack = []
275
+ for index, char in enumerate(text):
276
+ if char in {"[", "(", "{"}:
277
+ bracket_stack.append(char)
278
+ elif char == "]":
279
+ if not bracket_stack or bracket_stack.pop() != "[":
280
+ raise _UnexpectedAstError("Mismatched square brackets")
281
+ elif char == ")":
282
+ if not bracket_stack or bracket_stack.pop() != "(":
283
+ raise _UnexpectedAstError("Mismatched parentheses")
284
+ elif char == "}":
285
+ if not bracket_stack or bracket_stack.pop() != "{":
286
+ raise _UnexpectedAstError("Mismatched curly braces")
287
+ elif char in {"'", '"'}:
288
+ if bracket_stack and bracket_stack[-1] == char:
289
+ if index > 0 and text[index - 1] == "\\":
290
+ # Treat an escaped quote as a regular character
291
+ pass
292
+ else:
293
+ bracket_stack.pop()
294
+ elif bracket_stack and bracket_stack[-1] in {"'", '"'}:
295
+ # Double quote within a single quote string or vice versa.
296
+ pass
297
+ else:
298
+ bracket_stack.append(char)
299
+
300
+ text = text.rstrip()
301
+ if text.endswith("=") or text.endswith(":"):
302
+ # Since we have no type information for this property/parameter value,
303
+ # we can't fill in a valid value.
304
+ return None
305
+ if bracket_stack and bracket_stack[-1] == "{":
306
+ trailing_dict_text = text[: text.rfind("{")]
307
+ num_keys = trailing_dict_text.count(":")
308
+ num_values = trailing_dict_text.count(",")
309
+ if num_keys <= num_values:
310
+ return None # Incomplete property name within parameter value
311
+ if bracket_stack and bracket_stack[-1] == "(":
312
+ trailing_params_text = text[: text.rfind("(")]
313
+ num_full_param_names = trailing_params_text.count("=")
314
+ num_full_param_values = trailing_params_text.count(",")
315
+ if num_full_param_names <= num_full_param_values:
316
+ return None # Incomplete parameter name
317
+ if text.endswith(","):
318
+ text = text[:-1]
319
+ if (
320
+ bracket_stack
321
+ and bracket_stack[-1] == "["
322
+ and not text.endswith("[")
323
+ and not text.endswith(")")
324
+ ):
325
+ return None # Incomplete function name
326
+
327
+ added_text = ""
328
+ for char in reversed(bracket_stack):
329
+ if char == "[":
330
+ added_text += "]"
331
+ elif char == "(":
332
+ added_text += ")"
333
+ elif char == "{":
334
+ added_text += "}"
335
+ elif char == "'":
336
+ added_text += "'"
337
+ elif char == '"':
338
+ added_text += '"'
339
+
340
+ return text + added_text, added_text
341
+
342
+
343
+def _compute_tool_delta(
344
+ previously_sent_args: str, new_call: ToolCall, index: int, withheld_suffix: str
345
+) -> DeltaToolCall | None:
346
+ new_call_args = new_call.function.arguments
347
+ if withheld_suffix:
348
+ assert new_call_args.endswith(withheld_suffix)
349
+ new_call_args = new_call_args[: -len(withheld_suffix)]
350
+ if not previously_sent_args:
351
+ return DeltaToolCall(
352
+ id=new_call.id,
353
+ type="function",
354
+ index=index,
355
+ function=DeltaFunctionCall(
356
+ name=new_call.function.name,
357
+ arguments=new_call_args,
358
+ ),
359
+ )
360
+
361
+ arg_diff = new_call_args[len(previously_sent_args) :]
362
+ return (
363
+ DeltaToolCall(
364
+ id=None, index=index, function=DeltaFunctionCall(arguments=arg_diff)
365
+ )
366
+ if arg_diff
367
+ else None
368
+ )