tool-parser 1.7.0

Tool/function call parser for LLM model outputs
Documentation
1
2
3
4
5
6
7
8
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//! Sarashina tool call parser
//!
//! Parses tool calls emitted by SoftBank Sarashina models.
//!
//! # Format
//!
//! Sarashina models emit tool calls as a Python-literal list of dicts
//! (single-quoted), optionally prefixed by a `<|tool_calls|>` marker:
//! ```text
//! <|tool_calls|>[{'name': 'get_weather', 'arguments': {'city': 'Tokyo'}}]
//! ```
//!
//! In practice `<|tool_calls|>` is a special token (id 127) that is stripped
//! during detokenization, so the parser usually sees only the bare list
//! `[{'name': ..., 'arguments': {...}}]`. The parser therefore treats the
//! marker as optional and also recognizes the bare leading list.
//!
//! The payload uses Python literal syntax (single quotes, `True`/`False`/`None`),
//! so it is parsed with `rustpython_parser` — reusing the same converter as the
//! pythonic parser — rather than `serde_json`.
//!
//! # Field Mapping
//! - `name` → `name`
//! - `arguments` (a dict) → `arguments` (JSON string)

use async_trait::async_trait;
use openai_protocol::common::Tool;
use serde_json::Value;

use crate::{
    errors::ParserResult,
    parsers::{
        helpers,
        pythonic::{expression_to_json, parse_python_expression},
    },
    traits::ToolParser,
    types::{FunctionCall, StreamingParseResult, ToolCall, ToolCallItem},
};

const TOOL_CALLS_MARKER: &str = "<|tool_calls|>";

/// State machine for Sarashina tool parsing.
#[derive(Debug, Clone, Copy, PartialEq)]
enum ParseState {
    /// Looking for the start of a tool-call block.
    Text,
    /// Past the start, accumulating the Python-literal payload.
    InCalls,
}

/// Sarashina tool call parser.
///
/// Handles `[{'name': ..., 'arguments': {...}}]`, with or without a leading
/// `<|tool_calls|>` marker.
pub struct SarashinaParser {
    /// Current parsing state (streaming only).
    state: ParseState,
    /// Buffer for accumulating chunks across streaming calls.
    buffer: String,
    /// Index of the next tool call to stream.
    stream_tool_index: usize,
}

impl SarashinaParser {
    /// Create a new Sarashina parser.
    pub fn new() -> Self {
        Self {
            state: ParseState::Text,
            buffer: String::new(),
            stream_tool_index: 0,
        }
    }

    /// Whether `text` (already left-trimmed) begins a bare tool-call list,
    /// i.e. a list whose first element is a dict: `[{` (allowing whitespace).
    fn starts_bare_list(trimmed: &str) -> bool {
        let mut chars = trimmed.chars();
        if chars.next() != Some('[') {
            return false;
        }
        matches!(chars.find(|c| !c.is_whitespace()), Some('{'))
    }

    /// Whether `text` (already left-trimmed) could still grow into a bare
    /// tool-call list once more chunks arrive (streaming look-ahead).
    fn maybe_bare_list_prefix(trimmed: &str) -> bool {
        if trimmed.is_empty() || Self::starts_bare_list(trimmed) {
            return true;
        }
        // `[` possibly followed by only whitespace — still waiting for the
        // first `{` to arrive in a later chunk.
        let mut chars = trimmed.chars();
        chars.next() == Some('[') && chars.all(|c| c.is_whitespace())
    }

    /// Parse the Python-literal payload (a list of dicts, or a single dict)
    /// into `ToolCall`s. Returns an empty vec when the payload is not a valid
    /// tool-call structure.
    fn parse_payload(payload: &str) -> Vec<ToolCall> {
        let Ok(expr) = parse_python_expression(payload) else {
            return vec![];
        };
        let Ok(value) = expression_to_json(&expr) else {
            return vec![];
        };

        let items = match value {
            Value::Array(arr) => arr,
            single @ Value::Object(_) => vec![single],
            _ => return vec![],
        };

        let mut calls = Vec::new();
        for item in items {
            let Value::Object(obj) = item else { continue };
            let Some(name) = obj.get("name").and_then(|v| v.as_str()) else {
                continue;
            };
            let arguments = obj
                .get("arguments")
                .map(|v| v.to_string())
                .unwrap_or_else(|| "{}".to_string());
            calls.push(ToolCall {
                function: FunctionCall {
                    name: name.to_string(),
                    arguments,
                },
            });
        }
        calls
    }

    /// Find the byte offset just past the balanced bracket that starts at
    /// `open_idx`, skipping over string literals. Returns `None` when the
    /// bracket is not yet closed (partial payload during streaming).
    fn find_balanced_end(s: &str, open_idx: usize) -> Option<usize> {
        let bytes = s.as_bytes();
        let open = bytes[open_idx];
        let close = match open {
            b'[' => b']',
            b'{' => b'}',
            _ => return None,
        };

        let mut depth = 0usize;
        let mut in_str: Option<u8> = None;
        let mut escaped = false;

        for (i, &c) in bytes.iter().enumerate().skip(open_idx) {
            if let Some(quote) = in_str {
                if escaped {
                    escaped = false;
                } else if c == b'\\' {
                    escaped = true;
                } else if c == quote {
                    in_str = None;
                }
            } else if c == b'\'' || c == b'"' {
                in_str = Some(c);
            } else if c == open {
                depth += 1;
            } else if c == close {
                depth -= 1;
                if depth == 0 {
                    return Some(i + 1);
                }
            }
        }
        None
    }

    /// Try to extract a complete tool-call block from the head of the buffer
    /// (streaming). Consumes the block on success and returns to `Text` state.
    /// If the balanced block is not a valid tool-call list, the consumed span
    /// is emitted as normal text rather than dropped.
    fn try_extract_calls(&mut self) -> StreamingParseResult {
        let Some(open) = self.buffer.find(['[', '{']) else {
            return StreamingParseResult::default();
        };
        let Some(end) = Self::find_balanced_end(&self.buffer, open) else {
            return StreamingParseResult::default();
        };

        let calls = Self::parse_payload(&self.buffer[open..end]);
        let consumed = self.buffer[..end].to_string();
        self.buffer.drain(..end);
        self.state = ParseState::Text;

        if calls.is_empty() {
            // Balanced brackets but not a tool call — preserve as normal text.
            return StreamingParseResult {
                normal_text: consumed,
                calls: vec![],
            };
        }

        // Names are emitted as produced by the model; validation against the
        // request's tool schemas is left to the caller.
        let mut items = Vec::with_capacity(calls.len());
        for call in calls {
            items.push(ToolCallItem {
                tool_index: self.stream_tool_index,
                name: Some(call.function.name),
                parameters: call.function.arguments,
            });
            self.stream_tool_index += 1;
        }
        StreamingParseResult {
            normal_text: String::new(),
            calls: items,
        }
    }
}

impl Default for SarashinaParser {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl ToolParser for SarashinaParser {
    async fn parse_complete(&self, text: &str) -> ParserResult<(String, Vec<ToolCall>)> {
        // Payload comes after the marker if present, otherwise the bare text.
        let (prefix, rest, had_marker) = match text.find(TOOL_CALLS_MARKER) {
            Some(i) => (&text[..i], &text[i + TOOL_CALLS_MARKER.len()..], true),
            None => ("", text, false),
        };
        // Text to return when there is no tool call. The marker (a special
        // token) is dropped so it never leaks into user-visible output.
        let no_call = || {
            if had_marker {
                format!("{prefix}{rest}").trim().to_string()
            } else {
                text.trim().to_string()
            }
        };

        let Some(open) = rest.find(['[', '{']) else {
            return Ok((no_call(), vec![]));
        };

        // Without a marker, only treat the payload as a tool call when it is the
        // leading content (nothing but whitespace before the bracket), so a
        // bracket inside ordinary prose is left untouched.
        if !had_marker && !rest[..open].trim().is_empty() {
            return Ok((no_call(), vec![]));
        }

        let Some(end) = Self::find_balanced_end(rest, open) else {
            return Ok((no_call(), vec![]));
        };

        let tool_calls = Self::parse_payload(&rest[open..end]);
        if tool_calls.is_empty() {
            // Not a valid tool-call list — leave the surrounding text untouched.
            return Ok((no_call(), vec![]));
        }

        let mut normal_text = prefix.to_string();
        normal_text.push_str(&rest[end..]);
        Ok((normal_text.trim().to_string(), tool_calls))
    }

    async fn parse_incremental(
        &mut self,
        chunk: &str,
        _tools: &[Tool],
    ) -> ParserResult<StreamingParseResult> {
        self.buffer.push_str(chunk);

        match self.state {
            ParseState::Text => {
                if let Some(pos) = self.buffer.find(TOOL_CALLS_MARKER) {
                    let text_before = self.buffer[..pos].to_string();
                    self.buffer.drain(..pos + TOOL_CALLS_MARKER.len());
                    self.state = ParseState::InCalls;

                    let mut result = self.try_extract_calls();
                    result.normal_text = format!("{}{}", text_before, result.normal_text);
                    return Ok(result);
                }
                if helpers::ends_with_partial_token(&self.buffer, TOOL_CALLS_MARKER).is_some() {
                    // Possible partial marker at the tail — keep buffering.
                    return Ok(StreamingParseResult::default());
                }

                // Marker-less: the detokenized reply is a bare tool-call list.
                let trimmed = self.buffer.trim_start();
                if Self::starts_bare_list(trimmed) {
                    self.state = ParseState::InCalls;
                    return Ok(self.try_extract_calls());
                }
                if Self::maybe_bare_list_prefix(trimmed) {
                    // Ambiguous so far — wait for more chunks.
                    return Ok(StreamingParseResult::default());
                }

                let normal_text = std::mem::take(&mut self.buffer);
                Ok(StreamingParseResult {
                    normal_text,
                    calls: vec![],
                })
            }
            ParseState::InCalls => Ok(self.try_extract_calls()),
        }
    }

    fn has_tool_markers(&self, text: &str) -> bool {
        text.contains(TOOL_CALLS_MARKER) || Self::starts_bare_list(text.trim_start())
    }

    fn reset(&mut self) {
        self.state = ParseState::Text;
        self.buffer.clear();
        self.stream_tool_index = 0;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_with_marker() {
        let parser = SarashinaParser::new();
        let input = "<|tool_calls|>[{'name': 'get_weather', 'arguments': {'city': 'Tokyo'}}]";
        let (normal_text, tools) = parser.parse_complete(input).await.unwrap();
        assert_eq!(normal_text, "");
        assert_eq!(tools.len(), 1);
        assert_eq!(tools[0].function.name, "get_weather");
        let args: Value = serde_json::from_str(&tools[0].function.arguments).unwrap();
        assert_eq!(args["city"], "Tokyo");
    }

    #[tokio::test]
    async fn test_markerless_bare_list() {
        // This is what the server actually sees (special token stripped).
        let parser = SarashinaParser::new();
        let input = "[{'name': 'get_weather', 'arguments': {'city': 'Tokyo'}}]";
        let (normal_text, tools) = parser.parse_complete(input).await.unwrap();
        assert_eq!(normal_text, "");
        assert_eq!(tools.len(), 1);
        assert_eq!(tools[0].function.name, "get_weather");
        let args: Value = serde_json::from_str(&tools[0].function.arguments).unwrap();
        assert_eq!(args["city"], "Tokyo");
    }

    #[tokio::test]
    async fn test_leading_text_with_marker() {
        let parser = SarashinaParser::new();
        let input = "Let me check.<|tool_calls|>[{'name': 'ping', 'arguments': {}}]";
        let (normal_text, tools) = parser.parse_complete(input).await.unwrap();
        assert_eq!(normal_text, "Let me check.");
        assert_eq!(tools.len(), 1);
        assert_eq!(tools[0].function.arguments, "{}");
    }

    #[tokio::test]
    async fn test_multiple_tool_calls() {
        let parser = SarashinaParser::new();
        let input = "[{'name': 'search', 'arguments': {'q': 'rust'}}, \
             {'name': 'get_weather', 'arguments': {'city': 'Paris'}}]";
        let (_, tools) = parser.parse_complete(input).await.unwrap();
        assert_eq!(tools.len(), 2);
        assert_eq!(tools[0].function.name, "search");
        assert_eq!(tools[1].function.name, "get_weather");
    }

    #[tokio::test]
    async fn test_nested_arguments() {
        let parser = SarashinaParser::new();
        let input =
            "[{'name': 'process', 'arguments': {'config': {'nested': {'values': [1, 2, 3]}}}}]";
        let (_, tools) = parser.parse_complete(input).await.unwrap();
        assert_eq!(tools.len(), 1);
        let args: Value = serde_json::from_str(&tools[0].function.arguments).unwrap();
        assert_eq!(
            args["config"]["nested"]["values"],
            serde_json::json!([1, 2, 3])
        );
    }

    #[tokio::test]
    async fn test_python_literals_coerced() {
        let parser = SarashinaParser::new();
        let input = "[{'name': 'flags', 'arguments': {'a': True, 'b': False, 'c': None}}]";
        let (_, tools) = parser.parse_complete(input).await.unwrap();
        let args: Value = serde_json::from_str(&tools[0].function.arguments).unwrap();
        assert_eq!(args["a"], serde_json::json!(true));
        assert_eq!(args["b"], serde_json::json!(false));
        assert_eq!(args["c"], Value::Null);
    }

    #[tokio::test]
    async fn test_apostrophe_in_value() {
        let parser = SarashinaParser::new();
        let input = "[{'name': 'say', 'arguments': {'text': 'it\\'s [done]'}}]";
        let (_, tools) = parser.parse_complete(input).await.unwrap();
        assert_eq!(tools.len(), 1);
        let args: Value = serde_json::from_str(&tools[0].function.arguments).unwrap();
        assert_eq!(args["text"], "it's [done]");
    }

    #[tokio::test]
    async fn test_plain_text_untouched() {
        let parser = SarashinaParser::new();
        let input = "The weather in Tokyo is sunny.";
        let (normal_text, tools) = parser.parse_complete(input).await.unwrap();
        assert_eq!(tools.len(), 0);
        assert_eq!(normal_text, "The weather in Tokyo is sunny.");
    }

    #[tokio::test]
    async fn test_non_toolcall_list_untouched() {
        // A bare list that is not a tool-call structure must be left as text.
        let parser = SarashinaParser::new();
        let input = "[1, 2, 3]";
        let (normal_text, tools) = parser.parse_complete(input).await.unwrap();
        assert_eq!(tools.len(), 0);
        assert_eq!(normal_text, "[1, 2, 3]");
    }

    #[tokio::test]
    async fn test_has_tool_markers() {
        let parser = SarashinaParser::new();
        assert!(parser.has_tool_markers("<|tool_calls|>[]"));
        assert!(parser.has_tool_markers("[{'name': 'x', 'arguments': {}}]"));
        assert!(!parser.has_tool_markers("just text"));
        assert!(!parser.has_tool_markers("[TOOL_CALLS]")); // Mistral format
    }

    #[tokio::test]
    async fn test_streaming_markerless_chunked() {
        let mut parser = SarashinaParser::new();
        let chunks = [
            "[{'name': 'get_",
            "weather', 'arguments': {'city': 'To",
            "kyo'}}]",
        ];
        let mut calls = Vec::new();
        for c in chunks {
            let r = parser.parse_incremental(c, &[]).await.unwrap();
            calls.extend(r.calls);
        }
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].name.as_deref(), Some("get_weather"));
        let args: Value = serde_json::from_str(&calls[0].parameters).unwrap();
        assert_eq!(args["city"], "Tokyo");
    }

    #[tokio::test]
    async fn test_streaming_with_marker_chunked() {
        let mut parser = SarashinaParser::new();
        let chunks = [
            "Sure.",
            "<|tool_ca",
            "lls|>[{'name': 'ping', 'arg",
            "uments': {}}]",
        ];
        let mut normal = String::new();
        let mut calls = Vec::new();
        for c in chunks {
            let r = parser.parse_incremental(c, &[]).await.unwrap();
            normal.push_str(&r.normal_text);
            calls.extend(r.calls);
        }
        assert_eq!(normal, "Sure.");
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].name.as_deref(), Some("ping"));
    }

    #[tokio::test]
    async fn test_streaming_markerless_whitespace_split() {
        // The `[` and the first `{` arrive in different chunks, separated by
        // whitespace — the look-ahead must keep buffering, not flush as text.
        let mut parser = SarashinaParser::new();
        let chunks = ["[ ", "  {'name': 'ping', 'arguments': {}}]"];
        let mut normal = String::new();
        let mut calls = Vec::new();
        for c in chunks {
            let r = parser.parse_incremental(c, &[]).await.unwrap();
            normal.push_str(&r.normal_text);
            calls.extend(r.calls);
        }
        assert_eq!(normal, "");
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].name.as_deref(), Some("ping"));
    }

    #[tokio::test]
    async fn test_marker_not_leaked_on_malformed() {
        // Marker present but payload unbalanced: no tool call, and the
        // `<|tool_calls|>` special token must not leak into the text.
        let parser = SarashinaParser::new();
        let input = "before <|tool_calls|>[{'name': 'x'";
        let (normal_text, tools) = parser.parse_complete(input).await.unwrap();
        assert_eq!(tools.len(), 0);
        assert!(!normal_text.contains("<|tool_calls|>"));
        assert!(normal_text.starts_with("before"));
    }
}