[BUG] JupyterNotebook new cell constantly inserted at the beginning of a notebook

Status Closed — not planned
Maintainer reply None cached
Activity 13 comments · opened Jul 28, 2025 · closed Jan 5, 2026

Environment

  • Platform (select one):
  • [x] Anthropic API
  • [ ] AWS Bedrock
  • [ ] Google Vertex AI
  • [ ] Other: <!-- specify -->
  • Claude CLI version: <!-- output of claude --version -->
  • Operating System: Linux 6.8.0-64-generic
  • Terminal: <!-- e.g. iTerm2, Terminal App -->

## Bug Description
The NotebookEdit tool with edit_mode="insert" inserts cells at the beginning of
the notebook instead of at the end when no cell_id is specified, contrary to
expected behavior.

## Steps to Reproduce

  1. Open a Jupyter notebook with multiple cells using NotebookEdit
  2. Use NotebookEdit with edit_mode="insert" and cell_type="code" without

specifying a cell_id parameter

  1. Observe where the new cell is inserted

## Expected Behavior
When using edit_mode="insert" without specifying a cell_id, the new cell
should be inserted at the end of the notebook (as the last cell).

## Actual Behavior
The new cell is inserted at the beginning of the notebook (as the first cell)
instead of at the end.

## Additional Context

  • Workaround: Must specify the cell_id of the last cell to insert after it
  • This affects user experience when trying to add cells to the end of notebooks

during analysis workflows

  • The tool documentation suggests that omitting cell_id should insert at the

beginning, but user expectation is that "insert" without position means "append to
end"

Duplicate of: https://github.com/anthropics/claude-code/issues/2925

View original on GitHub ↗

13 Comments

github-actions[bot] · 1 year ago

Found 3 possible duplicate issues:

  1. https://github.com/anthropics/claude-code/issues/2925
  2. https://github.com/anthropics/claude-code/issues/2176
  3. https://github.com/anthropics/claude-code/issues/5197

This issue will be automatically closed as a duplicate in 3 days.

  • If your issue is a duplicate, please close it and 👍 the existing issue instead
  • To prevent auto-closure, add a comment or 👎 this comment

🤖 Generated with Claude Code

lsimoneau · 1 year ago

Also having this issue, does anyone know a workaround? Makes it pretty impossible to work with Jupyter notebooks from Claude code at the moment.

lit-af · 1 year ago
Also having this issue, does anyone know a workaround? Makes it pretty impossible to work with Jupyter notebooks from Claude code at the moment.

As per my original ticket description, this seem to work:

Workaround: Must specify the cell_id of the last cell to insert after it
PG408 · 1 year ago
> Also having this issue, does anyone know a workaround? Makes it pretty impossible to work with Jupyter notebooks from Claude code at the moment. As per my original ticket description, this seem to work: > Workaround: Must specify the cell_id of the last cell to insert after it

Are you referring to adding the instruction "Must specify the cell_id of the last cell to insert after it" in claude.md?

lit-af · 1 year ago
> > Also having this issue, does anyone know a workaround? Makes it pretty impossible to work with Jupyter notebooks from Claude code at the moment. > > > As per my original ticket description, this seem to work: > > Workaround: Must specify the cell_id of the last cell to insert after it Are you referring to adding the instruction "Must specify the cell_id of the last cell to insert after it" in claude.md?

In each instruction you give to claude you should add: insert new code after cell <10>. Replace <10> by your actual last cell number.

PG408 · 1 year ago
> > > Also having this issue, does anyone know a workaround? Makes it pretty impossible to work with Jupyter notebooks from Claude code at the moment. > > > > > > As per my original ticket description, this seem to work: > > > Workaround: Must specify the cell_id of the last cell to insert after it > > > Are you referring to adding the instruction "Must specify the cell_id of the last cell to insert after it" in claude.md? In each instruction you give to claude you should add: insert new code after cell <10>. Replace <10> by your actual last cell number.

This method might be effective for modifying existing Jupyter notebooks, but if you need Claude to create new files, it won't work. Currently, every time I finish creating a notebook with Claude, I have it review the notebook again, adjust the order, which consumes additional tokens, and for notebooks with more code, the effectiveness may decrease

nhopDV · 1 year ago

Is this going to get fixed? This is really annoying when working with claude code.

PG408 · 1 year ago

For the scenario of creating ipynb files, I have a better workaround: create the ipynb file yourself, then add enough empty cells within the file, and let Claude Code write inside these empty cells instead of generating new ones

> > > > Also having this issue, does anyone know a workaround? Makes it pretty impossible to work with Jupyter notebooks from Claude code at the moment. > > > > > > > > > As per my original ticket description, this seem to work: > > > > Workaround: Must specify the cell_id of the last cell to insert after it > > > > > > Are you referring to adding the instruction "Must specify the cell_id of the last cell to insert after it" in claude.md? > > > In each instruction you give to claude you should add: insert new code after cell <10>. Replace <10> by your actual last cell number. This method might be effective for modifying existing Jupyter notebooks, but if you need Claude to create new files, it won't work. Currently, every time I finish creating a notebook with Claude, I have it review the notebook again, adjust the order, which consumes additional tokens, and for notebooks with more code, the effectiveness may decrease
yn · 11 months ago

I found that this workaround works: put this into CLAUDE.md (yes as you can tell the instructions themselves are generated by Claude Code as a summary of a session of me yelling at it for a bit)

Technical Tool Guidelines

Jupyter Notebook Editing (NotebookEdit Tool)

Critical Known Issue

The NotebookEdit tool has a default insertion behavior that inserts new cells at the very top of the notebook when no cell_id is specified. This can create ordering problems and notebook structure issues.

Required Best Practices

  1. Always Get Cell IDs First

``bash
# Extract cell IDs from any notebook before editing
cat notebook.ipynb | grep -E '"id": "[^"]*"'
``

  1. Always Use cell_id Parameter
  • NEVER use NotebookEdit with edit_mode="insert" without specifying cell_id
  • ALWAYS target insertion after a specific existing cell
  • Use cell_id to control exact placement in notebook structure
  1. Proper Insertion Pattern

```python
# CORRECT: Target specific cell for insertion
NotebookEdit(
notebook_path="path/to/notebook.ipynb",
edit_mode="insert",
cell_id="existing_cell_id", # Insert AFTER this cell
cell_type="markdown",
new_source="content"
)

# WRONG: No cell_id specified - will insert at top
NotebookEdit(
notebook_path="path/to/notebook.ipynb",
edit_mode="insert", # This will go to the top!
cell_type="markdown",
new_source="content"
)
```

  1. Before Large Notebook Operations
  • Read file to understand structure and get cell IDs
  • Plan insertion sequence to avoid ordering issues
  • Test with single cell insertion first
  • Consider using fresh/clean notebooks for complex structures

Why This Matters

Incorrect cell insertion can create notebook structure problems that are difficult to fix, especially with large notebooks that exceed file size reading limits. Following these practices ensures precise control over notebook organization.

ChengJiale150 · 11 months ago

Why not try my Jupyter MCP Server jupyter-mcp-server ? It has these amazing features:

  • 📚 Multi-Notebook Management: Simultaneously manipulate multiple Notebooks within a single CLI interaction session
  • 🔁 Interactive Execution: Not only can it edit, but it can also execute! It can automatically adjust execution strategies based on cell outputs
  • 📊 Multimodal Output: Fully leverage powerful multimodal capabilities! Supports outputting multimodal results such as text, images, tables, and more

Additionally, it supports connecting to multiple Jupyter servers simultaneously, whether local or remote! It supports one-click quick installation using uvx like this:

claude mcp add Jupyter-MCP-Server uvx better-jupyter-mcp-server

Come and give it a try!

github-actions[bot] · 8 months ago

This issue has been inactive for 30 days. If the issue is still occurring, please comment to let us know. Otherwise, this issue will be automatically closed in 30 days for housekeeping purposes.

github-actions[bot] · 7 months ago

This issue has been automatically closed due to 60 days of inactivity. If you're still experiencing this issue, please open a new issue with updated information.

github-actions[bot] · 7 months ago

This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.