[BUG] JupyterNotebook new cell constantly inserted at the beginning of a notebook
Environment
- Platform (select one):
- [x] Anthropic API
- [ ] AWS Bedrock
- [ ] Google Vertex AI
- [ ] Other:
- Claude CLI version: <!-- output of
claude --version--> - Operating System: Linux 6.8.0-64-generic
- Terminal:
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
- Open a Jupyter notebook with multiple cells using NotebookEdit
- Use NotebookEdit with
edit_mode="insert"andcell_type="code"without specifying acell_idparameter - 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_idof 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_idshould 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
Found 3 possible duplicate issues:
- https://github.com/anthropics/claude-code/issues/2925
- https://github.com/anthropics/claude-code/issues/2176
- 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
Also having this issue, does anyone know a workaround? Makes it pretty impossible to work with Jupyter notebooks from Claude code at the moment.
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
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?
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.
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
Is this going to get fixed? This is really annoying when working with claude code.
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
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
-
Always Get Cell IDs First
# Extract cell IDs from any notebook before editing cat notebook.ipynb | grep -E '"id": "[^"]*"' -
Always Use cell_id Parameter
-
NEVER use
NotebookEditwithedit_mode="insert"without specifyingcell_id - ALWAYS target insertion after a specific existing cell
- Use
cell_idto control exact placement in notebook structure
-
NEVER use
-
Proper Insertion Pattern
# 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" ) -
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.
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!
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.