Skip to content

feat: extract outputs from Jupyter Notebook cells - #2285

Open
Manideep Malyala (manideep-malyala) wants to merge 1 commit into
microsoft:mainfrom
manideep-malyala:feature/jupyter-outputs
Open

feat: extract outputs from Jupyter Notebook cells#2285
Manideep Malyala (manideep-malyala) wants to merge 1 commit into
microsoft:mainfrom
manideep-malyala:feature/jupyter-outputs

Conversation

@manideep-malyala

Copy link
Copy Markdown

Motivation and Context

Currently, the IpynbConverter only extracts source code from Jupyter Notebook cells and drops all cell execution outputs. Because markitdown is primarily designed as a utility tool to convert files into Markdown for Large Language Model (LLM) consumption, dropping these outputs results in a significant loss of context.
For Data Science and RAG pipelines, the execution results of a notebook cell (such as printed metrics, pandas DataFrame plain-text representations, or error tracebacks) are often just as critical as the code itself for the LLM to understand the document's state.
This PR introduces robust output extraction for Jupyter Notebook code cells, drastically improving the utility of .ipynb conversion for AI and indexing workloads.

Description of Changes

  • Targeted Output Parsing: Updated _ipynb_converter.py to inspect the outputs array of any cell_type == "code".
  • Supported Output Types:
    • stream: Captures standard stdout and stderr outputs.
    • execute_result & display_data: Safely extracts the text/plain keys (falling back gracefully if they do not exist), which properly captures printed tables, integers, and standard objects.
    • error: Captures execution failures by extracting the ename (Error Name) and evalue (Error Value).
  • Safe Dictionary Access: All JSON dictionary accesses use .get() with safe defaults to prevent KeyError exceptions on corrupted, malformed, or manually edited notebooks.
  • Clean Markdown Formatting: Appends the parsed outputs cleanly below the source code inside a **Output:** heading and a text code block. This distinct formatting allows LLMs to easily differentiate between executable source code and console output. If a cell has no output (e.g. variable assignment), the block is gracefully omitted.

Testing

  • Updated Test Vectors: Modified the expected Markdown assertions for test_notebook.ipynb within tests/_test_vectors.py to ensure that both standard text streams ("markitdown") and integer outputs ("42") are successfully captured by the new logic.
  • Validation:
    • Ran hatch test -v locally; all 340 test cases passed.
    • Ran pre-commit run --all-files; code has been formatted using black to match Microsoft's repository standards.

@manideep-malyala

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant