> ## Documentation Index
> Fetch the complete documentation index at: https://alibi.itsdote.tech/llms.txt
> Use this file to discover all available pages before exploring further.

# alibi_sdk.plugins.sqlalchemy_plugin

> SQLAlchemy plugin — generated from python/alibi_sdk source.

SQLAlchemy plugin path — event listeners for mutations + in-span reads.

Install::

from alibi\_sdk.plugins import create\_sqlalchemy\_plugin
plugin = create\_sqlalchemy\_plugin(engine)
alibi.init(project="demo", plugins=\[plugin])

Listeners honor the active sandbox binding (schema / search\_path pin).

**Commit awareness:** while a real SQLAlchemy transaction is open, mutation/read
events are buffered per connection. They flush into the Alibi span buffer on the
**outermost** `commit` (the one that persists) and are discarded on `rollback`.
Nested transactions / SAVEPOINTs do not flush: a nested rollback drops only events
recorded after that SAVEPOINT; a nested release keeps them pending until the root
commit. Outside a transaction (DBAPI autocommit isolation), events record
immediately — the previous behavior.

*Source:* [`python/alibi_sdk/alibi_sdk/plugins/sqlalchemy_plugin.py`](https://github.com/valid22/alibi-ai/blob/main/python/alibi_sdk/alibi_sdk/plugins/sqlalchemy_plugin.py)

## `SQLAlchemyPlugin`

**Bases:** `Plugin`

#### `__init__`

```python theme={null}
def __init__(engine: Any = None, schema: str = 'alibi_sandbox', tables: Optional[Sequence[str]] = None, fail_closed: bool = True) -> None
```

**Parameters**

| Name | Type |
| - | - |
| `engine` | `Any` |
| `schema` | `str` |
| `tables` | `Optional[Sequence[str]]` |
| `fail_closed` | `bool` |

**Returns**

`None`

#### `active_schema`

```python theme={null}
def active_schema() -> str
```

**Returns**

`str`

#### `attach`

```python theme={null}
def attach(engine: Any) -> None
```

Register SQLAlchemy event listeners on the engine.

**Parameters**

| Name | Type |
| - | - |
| `engine` | `Any` |

**Returns**

`None`

#### `snapshot`

```python theme={null}
async def snapshot(span: SpanContext) -> SnapshotHandle
```

**Parameters**

| Name | Type |
| - | - |
| `span` | `SpanContext` |

**Returns**

`SnapshotHandle`

#### `observe`

```python theme={null}
async def observe(span: SpanContext) -> dict[str, Any]
```

**Parameters**

| Name | Type |
| - | - |
| `span` | `SpanContext` |

**Returns**

`dict[str, Any]`

#### `restore`

```python theme={null}
async def restore(span: SpanContext, handle: SnapshotHandle) -> None
```

**Parameters**

| Name | Type |
| - | - |
| `span` | `SpanContext` |
| `handle` | `SnapshotHandle` |

**Returns**

`None`

### `create_sqlalchemy_plugin`

```python theme={null}
def create_sqlalchemy_plugin(engine: Any = None, schema: str = 'alibi_sandbox', tables: Optional[Sequence[str]] = None, fail_closed: bool = True) -> SQLAlchemyPlugin
```

**Parameters**

| Name | Type |
| - | - |
| `engine` | `Any` |
| `schema` | `str` |
| `tables` | `Optional[Sequence[str]]` |
| `fail_closed` | `bool` |

**Returns**

`SQLAlchemyPlugin`


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.