claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

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

Open lit-af opened this issue 6 months ago • 11 comments

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

  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
  3. 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

lit-af avatar Jul 28 '25 21:07 lit-af

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

github-actions[bot] avatar Aug 08 '25 20:08 github-actions[bot]

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

lsimoneau avatar Aug 19 '25 11:08 lsimoneau

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

lit-af avatar Aug 19 '25 13:08 lit-af

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?

PG408 avatar Aug 26 '25 11:08 PG408

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.

lit-af avatar Aug 26 '25 13:08 lit-af

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

PG408 avatar Aug 27 '25 05:08 PG408

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

nhopDV avatar Aug 27 '25 18:08 nhopDV

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

PG408 avatar Aug 29 '25 07:08 PG408

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

    # Extract cell IDs from any notebook before editing
    cat notebook.ipynb | grep -E '"id": "[^"]*"'
    
  2. 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
  3. 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"
    )
    
  4. 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.

yn avatar Sep 06 '25 22:09 yn

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!

ChengJiale150 avatar Sep 17 '25 07:09 ChengJiale150

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] avatar Dec 05 '25 10:12 github-actions[bot]