# `vllm_mlx.engine.chat_template_safety`

Safety normalization for messages before Jinja chat-template rendering.

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

## 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.engine.chat_template_safety
    options:
      members:
        - _close_dangling_think_before_tool_call
        - _message_to_dict
        - normalize_messages_for_chat_template
      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.engine.chat_template_safety._close_dangling_think_before_tool_call" markdown="1">
<summary><code>vllm_mlx.engine.chat_template_safety._close_dangling_think_before_tool_call</code> · function</summary>

```python
vllm_mlx.engine.chat_template_safety._close_dangling_think_before_tool_call(content: str) -> str
```

Keep raw tool XML out of an unterminated ``<think>`` section.

**Parameters**

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

**Returns**

- Type: `str`
- Direct return expressions: `content`; `content[:tool_pos] + '</think>' + content[tool_pos:]`; `content + '</think>'`

**Exceptions and behavior**

Function `_close_dangling_think_before_tool_call` calls `content.rfind`, `content.find`; has 3 explicit return paths.
No direct `raise` statement appears in this definition.

[View source #L8-L29](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/engine/chat_template_safety.py#L8-L29).

</details>

<details class="api-contract" id="contract-vllm_mlx.engine.chat_template_safety._message_to_dict" markdown="1">
<summary><code>vllm_mlx.engine.chat_template_safety._message_to_dict</code> · function</summary>

```python
vllm_mlx.engine.chat_template_safety._message_to_dict(message: Any) -> dict[str, Any] | Any
```

Convert OpenAI message model objects without stringifying them.

**Parameters**

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `message` | `Any` | `yes` | `none` | Required positional or keyword input. |

**Returns**

- Type: `dict[str, Any] | Any`
- Direct return expressions: `dict(message)`; `{key: value for key, value in model_dump(exclude_none=True).items() if value is not None}`; `{k: v for k, v in legacy_dict().items() if v is not None}`; `message`

**Exceptions and behavior**

Function `_message_to_dict` calls `isinstance`, `dict`, `getattr`, `callable`; has 4 explicit return paths.
No direct `raise` statement appears in this definition.

[View source #L32-L46](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/engine/chat_template_safety.py#L32-L46).

</details>

<details class="api-contract" id="contract-vllm_mlx.engine.chat_template_safety.normalize_messages_for_chat_template" markdown="1">
<summary><code>vllm_mlx.engine.chat_template_safety.normalize_messages_for_chat_template</code> · function</summary>

```python
vllm_mlx.engine.chat_template_safety.normalize_messages_for_chat_template(messages: list[Any]) -> list[dict]
```

Return a JSON-safe copy of messages for chat-template rendering.

**Parameters**

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `messages` | `list[Any]` | `yes` | `none` | Required positional or keyword input. |

**Returns**

- Type: `list[dict]`
- Direct return expressions: `normalized`

**Exceptions and behavior**

Function `normalize_messages_for_chat_template` calls `json.loads`, `json.dumps`, `_message_to_dict`, `isinstance`; returns `normalized`.
No direct `raise` statement appears in this definition.

[View source #L49-L90](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/engine/chat_template_safety.py#L49-L90).

</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 |
| --- | --- | --- | --- | --- |
| [`_close_dangling_think_before_tool_call`](#contract-vllm_mlx.engine.chat_template_safety._close_dangling_think_before_tool_call) | function | `_close_dangling_think_before_tool_call(content: str) -> str` | Keep raw tool XML out of an unterminated ``<think>`` section. | [#L8-L29](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/engine/chat_template_safety.py#L8-L29) |
| [`_message_to_dict`](#contract-vllm_mlx.engine.chat_template_safety._message_to_dict) | function | `_message_to_dict(message: Any) -> dict[str, Any] \| Any` | Convert OpenAI message model objects without stringifying them. | [#L32-L46](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/engine/chat_template_safety.py#L32-L46) |
| [`normalize_messages_for_chat_template`](#contract-vllm_mlx.engine.chat_template_safety.normalize_messages_for_chat_template) | function | `normalize_messages_for_chat_template(messages: list[Any]) -> list[dict]` | Return a JSON-safe copy of messages for chat-template rendering. | [#L49-L90](https://github.com/waybarrios/vllm-mlx/blob/a69d47912bcb21d8fe04d48f75fa896b620ffcfa/vllm_mlx/engine/chat_template_safety.py#L49-L90) |
