# `vllm_mlx.tool_parsers.gemma4_tool_parser`

Gemma 4 tool call parser for vllm-mlx.

[View the complete module source at #L1-L513](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L1-L513).

## API details

Each callable below includes its exact signature, type annotations, inputs, defaults, return contract, documented exceptions, implementation source, and parsed docstring sections when the source provides them.

::: vllm_mlx.tool_parsers.gemma4_tool_parser
    options:
      members:
        - logger
        - TOOL_CALL_START
        - TOOL_CALL_END
        - _PLACEHOLDER_RE
        - _STRING_DELIM_RE
        - _CALL_PREFIX
        - _BARE_KEY
        - _BARE_VALUE
        - _JSON_LITERALS
        - _MAX_ARG_BLOCK_LEN
        - _CALL_PAREN_RE
        - _TOOL_CODE_FENCE_RE
        - _TOOL_CODE_ASSIGN_RE
        - _FALLBACK_MARKER_RE
        - _find_balanced_brace
        - _find_balanced_paren
        - _quote_bare_value
        - _gemma4_args_to_json
        - _call_node_to_tool
        - _parse_python_call
        - _parse_calls_from_code
        - _strip_spans
        - generate_tool_id
        - Gemma4ToolParser
      filters: []
      show_if_no_docstring: true

## Complete contract reference

Expand any definition for its exact inputs, annotations, defaults, return contract, directly raised exceptions, source-grounded behavior, and immutable line link. This section includes private and nested definitions that ordinary API generators omit.

<details class="api-contract" id="contract-vllm_mlx.tool_parsers.gemma4_tool_parser._find_balanced_brace" markdown="1">
<summary><code>vllm_mlx.tool_parsers.gemma4_tool_parser._find_balanced_brace</code> · function</summary>

```python
vllm_mlx.tool_parsers.gemma4_tool_parser._find_balanced_brace(text: str, start: int) -> int
```

Find the index of the closing } that balances the { at `start`.

**Parameters**

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `text` | `str` | `yes` | `none` | The string to search (may contain <\|"\|> tokens) |
| `start` | `int` | `yes` | `none` | Index of the opening { |

**Returns**

- Type: `int`
- Direct return expressions: `-1`; `i`

**Exceptions and behavior**

Function `_find_balanced_brace` calls `len`, `text.startswith`; has 2 explicit return paths.
No direct `raise` statement appears in this definition.

[View source #L92-L125](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L92-L125).

</details>

<details class="api-contract" id="contract-vllm_mlx.tool_parsers.gemma4_tool_parser._find_balanced_paren" markdown="1">
<summary><code>vllm_mlx.tool_parsers.gemma4_tool_parser._find_balanced_paren</code> · function</summary>

```python
vllm_mlx.tool_parsers.gemma4_tool_parser._find_balanced_paren(text: str, start: int) -> int
```

Find the index of the closing ) that balances the ( at `start`.

**Parameters**

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `text` | `str` | `yes` | `none` | Required positional or keyword input. |
| `start` | `int` | `yes` | `none` | Required positional or keyword input. |

**Returns**

- Type: `int`
- Direct return expressions: `-1`; `i`

**Exceptions and behavior**

Function `_find_balanced_paren` calls `len`; has 2 explicit return paths.
No direct `raise` statement appears in this definition.

[View source #L128-L159](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L128-L159).

</details>

<details class="api-contract" id="contract-vllm_mlx.tool_parsers.gemma4_tool_parser._quote_bare_value" markdown="1">
<summary><code>vllm_mlx.tool_parsers.gemma4_tool_parser._quote_bare_value</code> · function</summary>

```python
vllm_mlx.tool_parsers.gemma4_tool_parser._quote_bare_value(m: re.Match) -> str
```

Substitution callback for _BARE_VALUE — quotes bare identifiers that are not JSON literals (true/false/null).

**Parameters**

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `m` | `re.Match` | `yes` | `none` | Required positional or keyword input. |

**Returns**

- Type: `str`
- Direct return expressions: `m.group(0)`; `f'{ws}"{word}"'`

**Exceptions and behavior**

Function `_quote_bare_value` calls `m.group`; has 2 explicit return paths.
No direct `raise` statement appears in this definition.

[View source #L162-L168](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L162-L168).

</details>

<details class="api-contract" id="contract-vllm_mlx.tool_parsers.gemma4_tool_parser._gemma4_args_to_json" markdown="1">
<summary><code>vllm_mlx.tool_parsers.gemma4_tool_parser._gemma4_args_to_json</code> · function</summary>

```python
vllm_mlx.tool_parsers.gemma4_tool_parser._gemma4_args_to_json(text: str) -> str
```

Convert Gemma 4 tool call args to valid JSON.

**Parameters**

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `text` | `str` | `yes` | `none` | Required positional or keyword input. |

**Returns**

- Type: `str`
- Direct return expressions: `text`

**Exceptions and behavior**

Function `_gemma4_args_to_json` calls `_STRING_DELIM_RE.sub`, `_BARE_KEY.sub`, `_BARE_VALUE.sub`, `_PLACEHOLDER_RE.sub`; returns `text`.
No direct `raise` statement appears in this definition.

[View source #L171-L207](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L171-L207).

</details>

<details class="api-contract" id="contract-vllm_mlx.tool_parsers.gemma4_tool_parser._gemma4_args_to_json._capture" markdown="1">
<summary><code>vllm_mlx.tool_parsers.gemma4_tool_parser._gemma4_args_to_json._capture</code> · nested function</summary>

```python
vllm_mlx.tool_parsers.gemma4_tool_parser._gemma4_args_to_json._capture(m: re.Match) -> str
```

Nested Function `_gemma4_args_to_json._capture` calls `strings.append`, `m.group`, `len`; returns `f'\x00{len(strings) - 1}\x00'`.

**Parameters**

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `m` | `re.Match` | `yes` | `none` | Required positional or keyword input. |

**Returns**

- Type: `str`
- Direct return expressions: `f'\x00{len(strings) - 1}\x00'`

**Exceptions and behavior**

Nested Function `_gemma4_args_to_json._capture` calls `strings.append`, `m.group`, `len`; returns `f'\x00{len(strings) - 1}\x00'`.
No direct `raise` statement appears in this definition.

[View source #L187-L189](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L187-L189).

</details>

<details class="api-contract" id="contract-vllm_mlx.tool_parsers.gemma4_tool_parser._gemma4_args_to_json._restore" markdown="1">
<summary><code>vllm_mlx.tool_parsers.gemma4_tool_parser._gemma4_args_to_json._restore</code> · nested function</summary>

```python
vllm_mlx.tool_parsers.gemma4_tool_parser._gemma4_args_to_json._restore(m: re.Match) -> str
```

Nested Function `_gemma4_args_to_json._restore` calls `int`, `m.group`, `len`, `json.dumps`; returns `json.dumps(strings[idx]) if idx < len(strings) else m.group(0)`.

**Parameters**

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `m` | `re.Match` | `yes` | `none` | Required positional or keyword input. |

**Returns**

- Type: `str`
- Direct return expressions: `json.dumps(strings[idx]) if idx < len(strings) else m.group(0)`

**Exceptions and behavior**

Nested Function `_gemma4_args_to_json._restore` calls `int`, `m.group`, `len`, `json.dumps`; returns `json.dumps(strings[idx]) if idx < len(strings) else m.group(0)`.
No direct `raise` statement appears in this definition.

[View source #L201-L203](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L201-L203).

</details>

<details class="api-contract" id="contract-vllm_mlx.tool_parsers.gemma4_tool_parser._call_node_to_tool" markdown="1">
<summary><code>vllm_mlx.tool_parsers.gemma4_tool_parser._call_node_to_tool</code> · function</summary>

```python
vllm_mlx.tool_parsers.gemma4_tool_parser._call_node_to_tool(call: ast.Call) -> tuple[str, dict[str, Any]] | None
```

Map a Python `ast.Call` node to (function_name, kwargs_dict).

**Parameters**

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `call` | `ast.Call` | `yes` | `none` | Required positional or keyword input. |

**Returns**

- Type: `tuple[str, dict[str, Any]] | None`
- Direct return expressions: `None`; `(name, args)`

**Exceptions and behavior**

Function `_call_node_to_tool` calls `isinstance`, `ast.literal_eval`; has 2 explicit return paths.
No direct `raise` statement appears in this definition.

[View source #L210-L233](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L210-L233).

</details>

<details class="api-contract" id="contract-vllm_mlx.tool_parsers.gemma4_tool_parser._parse_python_call" markdown="1">
<summary><code>vllm_mlx.tool_parsers.gemma4_tool_parser._parse_python_call</code> · function</summary>

```python
vllm_mlx.tool_parsers.gemma4_tool_parser._parse_python_call(src: str) -> tuple[str, dict[str, Any]] | None
```

Parse a single `fn(...)` Python call expression into (name, kwargs).

**Parameters**

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `src` | `str` | `yes` | `none` | Required positional or keyword input. |

**Returns**

- Type: `tuple[str, dict[str, Any]] | None`
- Direct return expressions: `None`; `_call_node_to_tool(node.body)`

**Exceptions and behavior**

Function `_parse_python_call` calls `ast.parse`, `src.strip`, `isinstance`, `_call_node_to_tool`; has 2 explicit return paths.
No direct `raise` statement appears in this definition.

[View source #L236-L244](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L236-L244).

</details>

<details class="api-contract" id="contract-vllm_mlx.tool_parsers.gemma4_tool_parser._parse_calls_from_code" markdown="1">
<summary><code>vllm_mlx.tool_parsers.gemma4_tool_parser._parse_calls_from_code</code> · function</summary>

```python
vllm_mlx.tool_parsers.gemma4_tool_parser._parse_calls_from_code(code: str) -> list[tuple[str, dict[str, Any]]]
```

Parse every top-level `fn(...)` call statement in a code-fence body.

**Parameters**

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `code` | `str` | `yes` | `none` | Required positional or keyword input. |

**Returns**

- Type: `list[tuple[str, dict[str, Any]]]`
- Direct return expressions: `results`

**Exceptions and behavior**

Function `_parse_calls_from_code` calls `ast.parse`, `textwrap.dedent(code).strip`, `textwrap.dedent`, `isinstance`; returns `results`.
No direct `raise` statement appears in this definition.

[View source #L247-L259](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L247-L259).

</details>

<details class="api-contract" id="contract-vllm_mlx.tool_parsers.gemma4_tool_parser._strip_spans" markdown="1">
<summary><code>vllm_mlx.tool_parsers.gemma4_tool_parser._strip_spans</code> · function</summary>

```python
vllm_mlx.tool_parsers.gemma4_tool_parser._strip_spans(text: str, spans: list[tuple[int, int]]) -> str
```

Remove the given [start, end) spans from `text` (handles overlaps).

**Parameters**

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `text` | `str` | `yes` | `none` | Required positional or keyword input. |
| `spans` | `list[tuple[int, int]]` | `yes` | `none` | Required positional or keyword input. |

**Returns**

- Type: `str`
- Direct return expressions: `text`; `''.join(out)`

**Exceptions and behavior**

Function `_strip_spans` calls `sorted`, `max`, `out.append`, `''.join`; has 2 explicit return paths.
No direct `raise` statement appears in this definition.

[View source #L262-L275](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L262-L275).

</details>

<details class="api-contract" id="contract-vllm_mlx.tool_parsers.gemma4_tool_parser.generate_tool_id" markdown="1">
<summary><code>vllm_mlx.tool_parsers.gemma4_tool_parser.generate_tool_id</code> · function</summary>

```python
vllm_mlx.tool_parsers.gemma4_tool_parser.generate_tool_id() -> str
```

Generate a unique tool call ID.

**Parameters**

This callable has no explicit inputs.

**Returns**

- Type: `str`
- Direct return expressions: `f'call_{uuid.uuid4().hex[:8]}'`

**Exceptions and behavior**

Function `generate_tool_id` calls `uuid.uuid4`; returns `f'call_{uuid.uuid4().hex[:8]}'`.
No direct `raise` statement appears in this definition.

[View source #L278-L280](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L278-L280).

</details>

<details class="api-contract" id="contract-vllm_mlx.tool_parsers.gemma4_tool_parser.Gemma4ToolParser" markdown="1">
<summary><code>vllm_mlx.tool_parsers.gemma4_tool_parser.Gemma4ToolParser</code> · class</summary>

```python
vllm_mlx.tool_parsers.gemma4_tool_parser.Gemma4ToolParser()
```

Tool call parser for Gemma 4 models.

**Parameters**

This callable has no explicit inputs.

**Returns**

- Constructs: `vllm_mlx.tool_parsers.gemma4_tool_parser.Gemma4ToolParser`

**Exceptions and behavior**

Class `Gemma4ToolParser` derives from `ToolParser` and declares 5 direct member(s).
No direct `raise` statement appears in this definition.

[View source #L284-L513](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L284-L513).

</details>

<details class="api-contract" id="contract-vllm_mlx.tool_parsers.gemma4_tool_parser.Gemma4ToolParser.extract_tool_calls" markdown="1">
<summary><code>vllm_mlx.tool_parsers.gemma4_tool_parser.Gemma4ToolParser.extract_tool_calls</code> · method</summary>

```python
vllm_mlx.tool_parsers.gemma4_tool_parser.Gemma4ToolParser.extract_tool_calls(model_output: str, request: dict[str, Any] | None = None) -> ExtractedToolCallInformation
```

Extract tool calls from a complete Gemma 4 model response.

**Parameters**

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `model_output` | `str` | `yes` | `none` | Required positional or keyword input. |
| `request` | `dict[str, Any] \| None` | `no` | `None` | Optional positional or keyword input; defaults to `None`. |

**Returns**

- Type: `ExtractedToolCallInformation`
- Direct return expressions: `ExtractedToolCallInformation(tools_called=True, tool_calls=tool_calls, content=content_before)`; `fallback`; `ExtractedToolCallInformation(tools_called=False, tool_calls=[], content=model_output)`

**Exceptions and behavior**

Method `Gemma4ToolParser.extract_tool_calls` calls `self.strip_think_tags`, `self._extract_canonical`, `ExtractedToolCallInformation`, `self._extract_fallback`; has 3 explicit return paths.
No direct `raise` statement appears in this definition.

[View source #L300-L324](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L300-L324).

</details>

<details class="api-contract" id="contract-vllm_mlx.tool_parsers.gemma4_tool_parser.Gemma4ToolParser._extract_canonical" markdown="1">
<summary><code>vllm_mlx.tool_parsers.gemma4_tool_parser.Gemma4ToolParser._extract_canonical</code> · method</summary>

```python
vllm_mlx.tool_parsers.gemma4_tool_parser.Gemma4ToolParser._extract_canonical(cleaned: str) -> tuple[list[dict[str, Any]], str | None]
```

Parse the canonical <|tool_call>call:fn{...}<tool_call|> form.

**Parameters**

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `cleaned` | `str` | `yes` | `none` | Required positional or keyword input. |

**Returns**

- Type: `tuple[list[dict[str, Any]], str | None]`
- Direct return expressions: `([], None)`; `(tool_calls, content_before)`

**Exceptions and behavior**

Method `Gemma4ToolParser._extract_canonical` calls `cleaned.find`, `cleaned[:start_idx].strip`, `len`, `_CALL_PREFIX.search`; has 2 explicit return paths.
No direct `raise` statement appears in this definition.

[View source #L326-L382](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L326-L382).

</details>

<details class="api-contract" id="contract-vllm_mlx.tool_parsers.gemma4_tool_parser.Gemma4ToolParser._extract_fallback" markdown="1">
<summary><code>vllm_mlx.tool_parsers.gemma4_tool_parser.Gemma4ToolParser._extract_fallback</code> · method</summary>

```python
vllm_mlx.tool_parsers.gemma4_tool_parser.Gemma4ToolParser._extract_fallback(cleaned: str) -> ExtractedToolCallInformation | None
```

Parse the Python-style fallback forms (issue #80).

**Parameters**

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `cleaned` | `str` | `yes` | `none` | Required positional or keyword input. |

**Returns**

- Type: `ExtractedToolCallInformation | None`
- Direct return expressions: `None`; `ExtractedToolCallInformation(tools_called=True, tool_calls=tool_calls, content=content)`

**Exceptions and behavior**

Method `Gemma4ToolParser._extract_fallback` calls `_TOOL_CODE_FENCE_RE.finditer`, `_parse_calls_from_code`, `m.group`, `tool_calls.append`; has 2 explicit return paths.
No direct `raise` statement appears in this definition.

[View source #L384-L463](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L384-L463).

</details>

<details class="api-contract" id="contract-vllm_mlx.tool_parsers.gemma4_tool_parser.Gemma4ToolParser._format_streaming" markdown="1">
<summary><code>vllm_mlx.tool_parsers.gemma4_tool_parser.Gemma4ToolParser._format_streaming</code> · method</summary>

```python
vllm_mlx.tool_parsers.gemma4_tool_parser.Gemma4ToolParser._format_streaming(result: ExtractedToolCallInformation) -> dict[str, Any]
```

Render extracted tool calls into the streaming delta shape.

**Parameters**

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `result` | `ExtractedToolCallInformation` | `yes` | `none` | Required positional or keyword input. |

**Returns**

- Type: `dict[str, Any]`
- Direct return expressions: `{'tool_calls': [{'index': i, 'id': tc['id'], 'type': 'function', 'function': {'name': tc['name'], 'arguments': tc['argu…`

**Exceptions and behavior**

Method `Gemma4ToolParser._format_streaming` calls `enumerate`; returns `{'tool_calls': [{'index': i, 'id': tc['id'], 'type': 'function', 'function': {'name': tc['name'], 'arguments': tc['argu…`.
No direct `raise` statement appears in this definition.

[View source #L465-L480](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L465-L480).

</details>

<details class="api-contract" id="contract-vllm_mlx.tool_parsers.gemma4_tool_parser.Gemma4ToolParser.extract_tool_calls_streaming" markdown="1">
<summary><code>vllm_mlx.tool_parsers.gemma4_tool_parser.Gemma4ToolParser.extract_tool_calls_streaming</code> · method</summary>

```python
vllm_mlx.tool_parsers.gemma4_tool_parser.Gemma4ToolParser.extract_tool_calls_streaming(previous_text: str, current_text: str, delta_text: str, previous_token_ids: Sequence[int] | None = None, current_token_ids: Sequence[int] | None = None, delta_token_ids: Sequence[int] | None = None, request: dict[str, Any] | None = None) -> dict[str, Any] | None
```

Extract tool calls from streaming Gemma 4 model output.

**Parameters**

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `previous_text` | `str` | `yes` | `none` | Required positional or keyword input. |
| `current_text` | `str` | `yes` | `none` | Required positional or keyword input. |
| `delta_text` | `str` | `yes` | `none` | Required positional or keyword input. |
| `previous_token_ids` | `Sequence[int] \| None` | `no` | `None` | Optional positional or keyword input; defaults to `None`. |
| `current_token_ids` | `Sequence[int] \| None` | `no` | `None` | Optional positional or keyword input; defaults to `None`. |
| `delta_token_ids` | `Sequence[int] \| None` | `no` | `None` | Optional positional or keyword input; defaults to `None`. |
| `request` | `dict[str, Any] \| None` | `no` | `None` | Optional positional or keyword input; defaults to `None`. |

**Returns**

- Type: `dict[str, Any] | None`
- Direct return expressions: `{'content': delta_text}`; `self._format_streaming(result)`; `None`

**Exceptions and behavior**

Method `Gemma4ToolParser.extract_tool_calls_streaming` calls `bool`, `_FALLBACK_MARKER_RE.search`, `self.extract_tool_calls`, `self._format_streaming`; has 3 explicit return paths.
No direct `raise` statement appears in this definition.

[View source #L482-L513](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L482-L513).

</details>

## Complete symbol map

This map also includes private definitions and nested helpers. The signature column exposes every explicit input even when an internal helper has no dedicated parameter prose.

| Symbol | Kind | Signature and inputs | What it does | Source |
| --- | --- | --- | --- | --- |
| [`_find_balanced_brace`](#contract-vllm_mlx.tool_parsers.gemma4_tool_parser._find_balanced_brace) | function | `_find_balanced_brace(text: str, start: int) -> int` | Find the index of the closing } that balances the { at `start`. | [#L92-L125](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L92-L125) |
| [`_find_balanced_paren`](#contract-vllm_mlx.tool_parsers.gemma4_tool_parser._find_balanced_paren) | function | `_find_balanced_paren(text: str, start: int) -> int` | Find the index of the closing ) that balances the ( at `start`. | [#L128-L159](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L128-L159) |
| [`_quote_bare_value`](#contract-vllm_mlx.tool_parsers.gemma4_tool_parser._quote_bare_value) | function | `_quote_bare_value(m: re.Match) -> str` | Substitution callback for _BARE_VALUE — quotes bare identifiers that are not JSON literals (true/false/null). | [#L162-L168](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L162-L168) |
| [`_gemma4_args_to_json`](#contract-vllm_mlx.tool_parsers.gemma4_tool_parser._gemma4_args_to_json) | function | `_gemma4_args_to_json(text: str) -> str` | Convert Gemma 4 tool call args to valid JSON. | [#L171-L207](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L171-L207) |
| [`_gemma4_args_to_json._capture`](#contract-vllm_mlx.tool_parsers.gemma4_tool_parser._gemma4_args_to_json._capture) | nested function | `_gemma4_args_to_json._capture(m: re.Match) -> str` | Nested Function `_gemma4_args_to_json._capture` calls `strings.append`, `m.group`, `len`; returns `f'\x00{len(strings) - 1}\x00'`. | [#L187-L189](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L187-L189) |
| [`_gemma4_args_to_json._restore`](#contract-vllm_mlx.tool_parsers.gemma4_tool_parser._gemma4_args_to_json._restore) | nested function | `_gemma4_args_to_json._restore(m: re.Match) -> str` | Nested Function `_gemma4_args_to_json._restore` calls `int`, `m.group`, `len`, `json.dumps`; returns `json.dumps(strings[idx]) if idx < len(strings) else m.group(0)`. | [#L201-L203](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L201-L203) |
| [`_call_node_to_tool`](#contract-vllm_mlx.tool_parsers.gemma4_tool_parser._call_node_to_tool) | function | `_call_node_to_tool(call: ast.Call) -> tuple[str, dict[str, Any]] \| None` | Map a Python `ast.Call` node to (function_name, kwargs_dict). | [#L210-L233](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L210-L233) |
| [`_parse_python_call`](#contract-vllm_mlx.tool_parsers.gemma4_tool_parser._parse_python_call) | function | `_parse_python_call(src: str) -> tuple[str, dict[str, Any]] \| None` | Parse a single `fn(...)` Python call expression into (name, kwargs). | [#L236-L244](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L236-L244) |
| [`_parse_calls_from_code`](#contract-vllm_mlx.tool_parsers.gemma4_tool_parser._parse_calls_from_code) | function | `_parse_calls_from_code(code: str) -> list[tuple[str, dict[str, Any]]]` | Parse every top-level `fn(...)` call statement in a code-fence body. | [#L247-L259](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L247-L259) |
| [`_strip_spans`](#contract-vllm_mlx.tool_parsers.gemma4_tool_parser._strip_spans) | function | `_strip_spans(text: str, spans: list[tuple[int, int]]) -> str` | Remove the given [start, end) spans from `text` (handles overlaps). | [#L262-L275](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L262-L275) |
| [`generate_tool_id`](#contract-vllm_mlx.tool_parsers.gemma4_tool_parser.generate_tool_id) | function | `generate_tool_id() -> str` | Generate a unique tool call ID. | [#L278-L280](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L278-L280) |
| [`Gemma4ToolParser`](#contract-vllm_mlx.tool_parsers.gemma4_tool_parser.Gemma4ToolParser) | class | `Gemma4ToolParser()` | Tool call parser for Gemma 4 models. | [#L284-L513](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L284-L513) |
| [`Gemma4ToolParser.extract_tool_calls`](#contract-vllm_mlx.tool_parsers.gemma4_tool_parser.Gemma4ToolParser.extract_tool_calls) | method | `Gemma4ToolParser.extract_tool_calls(model_output: str, request: dict[str, Any] \| None = None) -> ExtractedToolCallInformation` | Extract tool calls from a complete Gemma 4 model response. | [#L300-L324](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L300-L324) |
| [`Gemma4ToolParser._extract_canonical`](#contract-vllm_mlx.tool_parsers.gemma4_tool_parser.Gemma4ToolParser._extract_canonical) | method | `Gemma4ToolParser._extract_canonical(cleaned: str) -> tuple[list[dict[str, Any]], str \| None]` | Parse the canonical <\|tool_call>call:fn{...}<tool_call\|> form. | [#L326-L382](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L326-L382) |
| [`Gemma4ToolParser._extract_fallback`](#contract-vllm_mlx.tool_parsers.gemma4_tool_parser.Gemma4ToolParser._extract_fallback) | method | `Gemma4ToolParser._extract_fallback(cleaned: str) -> ExtractedToolCallInformation \| None` | Parse the Python-style fallback forms (issue #80). | [#L384-L463](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L384-L463) |
| [`Gemma4ToolParser._format_streaming`](#contract-vllm_mlx.tool_parsers.gemma4_tool_parser.Gemma4ToolParser._format_streaming) | method | `Gemma4ToolParser._format_streaming(result: ExtractedToolCallInformation) -> dict[str, Any]` | Render extracted tool calls into the streaming delta shape. | [#L465-L480](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L465-L480) |
| [`Gemma4ToolParser.extract_tool_calls_streaming`](#contract-vllm_mlx.tool_parsers.gemma4_tool_parser.Gemma4ToolParser.extract_tool_calls_streaming) | method | `Gemma4ToolParser.extract_tool_calls_streaming(previous_text: str, current_text: str, delta_text: str, previous_token_ids: Sequence[int] \| None = None, current_token_ids: Sequence[int] \| None = None, delta_token_ids: Sequence[int] \| None = None, request: dict[str, Any] \| None = None) -> dict[str, Any] \| None` | Extract tool calls from streaming Gemma 4 model output. | [#L482-L513](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/tool_parsers/gemma4_tool_parser.py#L482-L513) |
