# OpenAIAutoModeMiddleware

> **Class** in `langchain_openai`

📖 [View in docs](https://reference.langchain.com/python/langchain-openai/middleware/auto_mode/OpenAIAutoModeMiddleware)

Allow low-risk tool calls and block risky calls using OpenAI Decisions.

Intercepts explicitly configured tools immediately before execution and asks a
`Predicate` question for the probability that each call is risky or
insufficiently authorized. Calls below `0.5` execute normally. Other calls return
an error `ToolMessage` without invoking the tool. Tools not listed in `tools`
bypass classification.

The model receives the proposed tool call and up to 30 recent messages. Base64
images are included; audio, video, and file content, which the Decisions API
does not accept, are replaced by placeholders. If a request with images is
rejected (HTTP 400 or 413), the call is classified once more with images
replaced by placeholders. Only explicit user messages authorize execution.

The middleware fails closed: refusals block the call, and classification errors
propagate without executing the tool. It blocks risky calls; it does not request
human approval.

!!! warning

    This middleware is experimental. Its API may change without notice.

## Signature

```python
OpenAIAutoModeMiddleware(
    self,
    *,
    tools: Sequence[str | BaseTool],
    model: str | OpenAIDecisions,
    instructions: str = _DEFAULT_INSTRUCTIONS,
)
```

## Description

??? example "Guard a destructive tool"

```python
from langchain.agents import create_agent
from langchain_openai.middleware import OpenAIAutoModeMiddleware

auto_mode = OpenAIAutoModeMiddleware(
    tools=[delete_file],
    model="gpt-6-luna",
)
agent = create_agent(
    "openai:gpt-6-luna",
    tools=[read_file, delete_file],
    middleware=[auto_mode],
)
```

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `tools` | `Sequence[str \| BaseTool]` | Yes | Tool names or `BaseTool` instances to classify before execution. |
| `model` | `str \| OpenAIDecisions` | Yes | Decisions model name, or a configured `OpenAIDecisions` instance. |
| `instructions` | `str` | No | Risk question sent to the Decisions API. Describe what should count as risky and safe here. (default: `_DEFAULT_INSTRUCTIONS`) |

## Extends

- `AgentMiddleware[AgentState[ResponseT], ContextT, ResponseT]`

## Constructors

```python
__init__(
    self,
    *,
    tools: Sequence[str | BaseTool],
    model: str | OpenAIDecisions,
    instructions: str = _DEFAULT_INSTRUCTIONS,
) -> None
```

| Name | Type |
|------|------|
| `tools` | `Sequence[str \| BaseTool]` |
| `model` | `str \| OpenAIDecisions` |
| `instructions` | `str` |


## Properties

- `trace_policy`
- `tool_names`
- `decisions`
- `instructions`

## Methods

- [`wrap_tool_call()`](https://reference.langchain.com/python/langchain-openai/middleware/auto_mode/OpenAIAutoModeMiddleware/wrap_tool_call)
- [`awrap_tool_call()`](https://reference.langchain.com/python/langchain-openai/middleware/auto_mode/OpenAIAutoModeMiddleware/awrap_tool_call)

---

[View source on GitHub](https://github.com/langchain-ai/langchain/blob/16a08bda1247010c23fcfa887e421259a5fced5e/libs/partners/openai/langchain_openai/middleware/auto_mode.py#L64)