Metadata-Version: 2.4
Name: crane-llm
Version: 0.1.2
Summary: CRANE-LLM: runtime-augmented crash prediction and diagnosis for ML notebooks
Author: PELAB, Linköping University
License: BSD-3-Clause
Project-URL: Homepage, https://github.com/yarinamomo/crane_llm
Project-URL: Repository, https://github.com/yarinamomo/crane_llm
Project-URL: Issues, https://github.com/yarinamomo/crane_llm/issues
Keywords: jupyter,jupyterlab,jupyterlab-extension,notebook,llm,crash-prediction
Classifier: Framework :: Jupyter
Classifier: Framework :: Jupyter :: JupyterLab
Classifier: Framework :: Jupyter :: JupyterLab :: 4
Classifier: Framework :: Jupyter :: JupyterLab :: Extensions
Classifier: Framework :: Jupyter :: JupyterLab :: Extensions :: Prebuilt
Classifier: License :: OSI Approved :: BSD License
Classifier: Programming Language :: Python :: 3
Classifier: Intended Audience :: Science/Research
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: ipython>=8.5
Requires-Dist: nbformat>=5.7
Requires-Dist: openai>=1.40
Requires-Dist: python-dotenv>=1.0
Requires-Dist: rich>=13.0
Provides-Extra: lab
Requires-Dist: jupyterlab<5,>=4.0.0; extra == "lab"
Provides-Extra: build
Requires-Dist: jupyter-builder<2,>=1.0.0; extra == "build"
Provides-Extra: test
Requires-Dist: pytest>=7; extra == "test"
Provides-Extra: experiments
Requires-Dist: google-genai; extra == "experiments"
Requires-Dist: ollama; extra == "experiments"
Requires-Dist: torch; extra == "experiments"
Requires-Dist: transformers; extra == "experiments"
Dynamic: license-file

# CRANE-LLM

Predict whether a ML notebook cell will crash — before you run it.

CRANE-LLM is a JupyterLab 4 / Notebook 7 extension. Select a cell, click a button, and it tells you whether executing that cell is likely to raise. What makes the prediction useful is that it does not read your code alone: it also inspects the **live state of your kernel**, so it knows the actual shape of the array, the dtype of the column and the length of the list the cell is about to use. The target cell is never executed.

This is the tool from the paper *[CRANE-LLM: Runtime-Augmented LLMs for Crash Prediction and Diagnosis in ML Notebooks](https://arxiv.org/abs/2602.18537)*.

CRANE-LLM works in two ways:

- **In JupyterLab**, as a toolbar button with a sidebar. This is the full experience.
- **Anywhere else a notebook runs Python** — Kaggle, Colab, VS Code, classic Notebook — as the `%%crane_llm` cell magic. See *On Kaggle and Colab* below.

## Install

```bash
pip install "crane-llm[lab]"
jupyter lab
```

The `[lab]` part installs JupyterLab 4 alongside; leave it out if the environment already has JupyterLab 4: `pip install "crane-llm"`. Check it registered:

```bash
jupyter labextension list        # expect: crane-llm-jlab <version> enabled ok
```

## Set up a model

You need an API key for whichever model you want to use. One line in any notebook cell, remembered across sessions:

```python
import crane_llm
crane_llm.set_api_key("sk-...")
```

That writes `~/.crane_llm/config.json`. On Kaggle and Colab, store the key as a notebook secret instead, as described under *On Kaggle and Colab* below.

Settings are resolved in this order, and the first one found wins:

1. an explicit argument, e.g. `%%crane_llm gpt-5-mini`
2. the `CRANE_LLM_API_KEY`, `CRANE_LLM_MODEL` and `CRANE_LLM_BASE_URL` environment variables
3. the provider's own variables, `OPENAI_API_KEY` and `OPENAI_BASE_URL`
4. `~/.crane_llm/config.json`
5. on Kaggle and Colab, a notebook secret named `CRANE_LLM_API_KEY` or `OPENAI_API_KEY`

A `.env` file is not a step of its own. It is read first, from the directory you started Jupyter in or any directory above it, and fills in whichever of those variables the environment does not already define.

### Using a provider other than OpenAI

Any OpenAI-compatible endpoint works by adding a `base_url` and a model name, which covers Claude and Gemini through a gateway, and local models with no account at all:

| Provider | `base_url` | Example model |
|---|---|---|
| OpenAI | *(none needed)* | `gpt-5` |
| OpenRouter (Claude, Gemini, Llama, …) | `https://openrouter.ai/api/v1` | `anthropic/claude-sonnet-4.5` |
| Google Gemini | `https://generativelanguage.googleapis.com/v1beta/openai/` | `gemini-2.5-flash` |
| Groq | `https://api.groq.com/openai/v1` | `qwen/qwen3-32b` |
| Ollama, local, no key needed | `http://localhost:11434/v1` | `qwen2.5-coder:32b` |

```python
import crane_llm
crane_llm.set_api_key(
    "sk-or-...",
    base_url="https://openrouter.ai/api/v1",
    model="anthropic/claude-sonnet-4.5",
)
```

Or entirely locally:

```python
crane_llm.set_api_key(base_url="http://localhost:11434/v1", model="qwen2.5-coder:32b")
```

With a `base_url`, requests use the Chat Completions API, which every OpenAI-compatible server supports.

## Use it

1. Run some cells as usual and let them finish.
2. Select the cell you want to check.
3. Click **CRANE-LLM** in the toolbar, or run *Run CRANE-LLM* from the command palette.

The verdict appears under the cell, and the right sidebar shows the full prompt and the raw response.

| Colour | Meaning |
|---|---|
| red | a crash is certain or predicted |
| green | no crash is predicted |
| amber | the response could not be read as a verdict |
| grey | the prediction has gone stale |

A badge next to the verdict says where it came from:

- **Built-in check · certain**: CRANE-LLM found the crash itself, from the live kernel state, and did not call the model. The cell will raise when it runs.
- **LLM prediction · *model***: the model judged the cell. This is a prediction, and it can be wrong.

Wait for running cells to finish first: CRANE-LLM reads the kernel's current state, and refuses to start while the notebook is busy. A prediction goes stale when you edit the cell or run another one.

Two switches decide how the button checks a cell. They sit in a panel on the toolbar button (hover over it), in the sidebar and in the command palette, next to a third, *Check cells before they run*, described under *Checking every cell before it runs* below:

- **Use the LLM.** On by default. Turned off, only the built-in checks run and nothing is sent to any model, so no API key is needed. When the checks find no certain crash, the verdict says so in blue: that is not a "no crash" prediction, since the checks only report crashes they are certain of.
- **Include runtime information.** Turned off, the model predicts from the code alone, which is also worth trying when a prompt gets too large. The built-in checks are then skipped too. It has no effect while the LLM is off.

### What the runtime information contains

Only the variables the target cell uses are included: every name it reads that exists in the kernel, plus the attributes and methods it uses on them.

For each object, the prompt describes its type and the facts that crashes usually depend on: the value of a number or string, the shape, dtype and NaN status of an array, the columns of a DataFrame, whether a model has been fitted. What exactly is included for each kind of object is listed under [Runtime information sent to the model](https://github.com/yarinamomo/crane_llm#runtime-information-sent-to-the-model) in the project README.

Some of this is your data itself: whole strings, a few values per column, dictionary entries. It is sent to the model provider along with your code, so turn runtime information off for notebooks whose data must not leave your machine. Collecting it does not change your variables.

### Built-in checks

Before calling the LLM model, CRANE-LLM checks the cell against the live kernel state for crashes that can be detected for certain: an undefined name, a column that does not exist, arrays whose shapes do not fit, a model that has not been fitted, a file that is missing, and many more. When one of these is found, you get the answer immediately, without waiting for the model or spending tokens on it. The full list, by library, is under [Built-in checks](https://github.com/yarinamomo/crane_llm#built-in-checks) in the project README.

A check reports only crashes that are certain. Code whose outcome it cannot know, such as a call to one of your own functions or the body of a loop, is left to the LLM. A check never declares a cell safe: when nothing certain is found, the LLM is asked.

Some checks read every value of your data, such as `astype(int)` or NaN in scikit-learn input. They are skipped for data with more than a million values, so that a check never takes noticeably long. Change the limit once with:

```python
import crane_llm
crane_llm.set_scan_limit(5_000_000)   # 0 turns these checks off; None restores the default
```

or set the `CRANE_LLM_SCAN_LIMIT` environment variable.

While a check runs, the sidebar and a box under the cell list each step as it happens: the built-in checker, then, if it found nothing certain, building the prompt and waiting for the LLM model. A verdict from the model also says that the checker ran first and found nothing.

The checks run inside your kernel and send nothing anywhere.

### Origin of the crash

The cell that crashes is rarely where the mistake was made. When a crash is found or predicted, CRANE-LLM lists under **Origin of the crash** the cells that gave the variables involved their current state:

- the cell that last **assigned** the variable, and the line that did it,
- every cell that **modified** it since, for example by dropping columns in place or fitting a model,
- for a variable that does not exist, the cells of the notebook that **define it** and why they did not: not run yet, or raised before reaching the assignment. This one needs the whole notebook, so it appears in JupyterLab only, not with the cell magic.

Click an entry to jump to its cell. The cells themselves are outlined with a dashed violet line and carry a short note saying what they did, until the verdict goes stale.

CRANE-LLM records the order the cells actually ran in from the moment the kernel starts, or from `%load_ext crane_llm` with the cell magic, so load it early. Cells run before that are known from their code only, and are marked as such.

### Checking every cell before it runs

The built-in checks can also run by themselves, on every cell you run, just before it runs. Turn on **Check cells before they run**: in the panel under the toolbar button, in the sidebar or in the command palette. Where the toolbar button is not available, run `%crane_llm guard on` after `%load_ext crane_llm`. It is off by default.

When the checks find that a cell will crash, CRANE-LLM does not run it. The cell shows why, with the origin of the crash, and ends with a `CrashPrevented` error instead of the crash. No line of the cell runs, not even the lines before the one that would fail. Like any error, it stops *Run All* at that cell.

Only the built-in checks are used, never the LLM model: nothing is sent anywhere, no API key is needed, and a cell is stopped only for a crash that is certain. A cell in which the checks find nothing runs as usual, which does not mean it is safe.

To run a stopped cell anyway, for example to see Python's own error, add this line anywhere in it:

```python
# crane: run
```

Cells that start with a cell magic such as `%%time` are not checked.

In JupyterLab the switch is remembered and applies to every notebook. `%crane_llm guard on` lasts until the kernel restarts. `%crane_llm guard off` turns it off, and `%crane_llm guard` says whether it is on.

### The cell magic

Where the toolbar button is not available, put the code you want to check in a cell under `%%crane_llm`:

```python
%load_ext crane_llm
```

```python
%%crane_llm
model.fit(x_train, y_train)
```

The cell body is analysed, not executed. The verdict appears as the cell's output, in the same colours and with the same badge as above, followed by the origin of the crash and, folded under *Prompt and raw response*, what was sent to the model. It turns grey once you run any other cell.

`%%crane_llm --no-runinfo` turns runtime information off, `%%crane_llm --no-llm` runs only the built-in checks, and a model name overrides the configured one, as in `%%crane_llm gpt-5-mini`.

## On Kaggle and Colab

Hosted notebooks cannot load JupyterLab extensions, so there is no toolbar button or sidebar; the cell magic does the same job.

**1. Store your API key as a secret**, once per account, so that it never appears in the notebook. Skip this step if you only want the built-in checks, described in step 4.

- *Kaggle:* in the notebook editor, **Add-ons → Secrets → Add a new secret**, with the label `CRANE_LLM_API_KEY` and your key as the value. Tick the checkbox next to it in each notebook that should use it.
- *Colab:* the key icon in the left sidebar, a secret named `CRANE_LLM_API_KEY`, with **Notebook access** switched on.

CRANE-LLM reads the secret itself; there is no setup cell to write.

**2. Install and load it** in the notebook. On Kaggle, first switch **Internet** on in the notebook's settings panel, which requires a phone-verified account.

```python
%pip install crane-llm
%load_ext crane_llm
```

To have every cell checked before it runs, add `%crane_llm guard on` to that cell; see *Checking every cell before it runs* above.

**3. Run your notebook as usual, then check a cell** by copying its code under `%%crane_llm`:

```python
%%crane_llm
# your code (in the target cell)
```

**4. Choose what runs**, with a flag on the first line. There are no switches on hosted notebooks, so the flag applies to that one check only and has to be written again on the next one.

| First line | What happens | API key needed |
|---|---|---|
| `%%crane_llm` | The built-in checks run first. If they find no certain crash, the model is asked, with runtime information. | yes |
| `%%crane_llm --no-llm` | Only the built-in checks run. Nothing is sent to any model. | no |
| `%%crane_llm --no-runinfo` | The model judges the code alone. The built-in checks are skipped, since they read the live kernel state. | yes |

With `--no-llm`, a cell where the checks find nothing gets a blue verdict saying so, which is not a prediction that the cell is safe. A model name can be combined with the other flags, as in `%%crane_llm gpt-5-mini --no-runinfo`.

Hosted sessions start from a fresh image each time, so the `%pip install` cell has to be run again in every new session. Competitions that require Internet to be off cannot use CRANE-LLM, since the install needs it, and so does the model call unless you use `--no-llm`.

## Requirements and what is not included

Python 3.9+. The toolbar button needs JupyterLab 4, which `crane-llm[lab]` installs; the cell magic needs only IPython. Two things are deliberately left out:

- **Notebook 7.** Add `pip install notebook` if you want that interface rather than JupyterLab.
- **The ML stack.** Runtime summarisation understands *pandas, numpy, torch, scikit-learn, TensorFlow* objects when those packages are present in your kernel, and quietly skips them when they are not. Your own notebooks will already have brought whichever ones they use.

## Links

- Source, issues and full documentation:
  <https://github.com/yarinamomo/crane_llm>
- Releases, including installable wheels:
  <https://github.com/yarinamomo/crane_llm/releases>
- Paper artefacts, datasets and reproduction instructions:
  <https://github.com/yarinamomo/crane_llm/blob/HEAD/PAPER.md>

## License

BSD 3-Clause.
