Skip to content

fix(nbfmt): preserve Unicode separators in notebook JSON - #1432

Closed
BichengWang wants to merge 2 commits into
google-gemini:mainfrom
BichengWang:fix-nbfmt-unicode-separators
Closed

BichengWang wants to merge 2 commits into
google-gemini:mainfrom
BichengWang:fix-nbfmt-unicode-separators

Conversation

@BichengWang

Copy link
Copy Markdown
Contributor

Formatting a valid notebook containing U+0085, U+2028, or U+2029 currently writes invalid JSON. json.dumps(..., ensure_ascii=False) preserves these characters inside strings, but splitlines() treats them as line boundaries and the subsequent join inserts unescaped newlines into those strings. For example, formatting a markdown cell with source ["before\u2028after"] makes the saved notebook fail json.loads with Invalid control character.

Split the serialized JSON on \n only. Add regression coverage for each separator in string and list cell sources, saved outputs, and metadata, including content preservation and formatting idempotence.

Validation:

  • New regressions fail before the fix and pass afterward.
  • python3 -m unittest tools.test_nbfmt tools.test_check_all_links — 20 tests pass offline.
  • All 154 tracked notebooks formatted in memory produce valid JSON and byte-identical output to the original formatter.
  • git diff --check passes.

Full unittest discovery also imports the optional notebook execution suite and is blocked by the environment's missing nbformat dependency; the formatter and existing link-checker tests run without it.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a bug in the notebook formatter where specific Unicode line/paragraph separators were being incorrectly treated as line breaks during JSON serialization. By switching from splitlines() to a explicit newline split, the formatter now preserves these characters, ensuring that formatted notebooks remain valid JSON and byte-identical where appropriate.

Highlights

  • Unicode Separator Preservation: Updated the JSON formatting logic to split lines using only standard newline characters, preventing the accidental corruption of Unicode separators (U+0085, U+2028, U+2029) within notebook strings.
  • Regression Testing: Added a new test suite in tools/test_nbfmt.py to verify that Unicode separators are correctly preserved across cell sources, outputs, and metadata during the formatting process.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@github-actions github-actions Bot added the status:awaiting review PR awaiting review from a maintainer label Oct 16, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the notebook formatter in tools/nbfmt/formatter.py to split JSON lines using '\n' instead of splitlines(), which preserves Unicode line separators within strings. Additionally, a comprehensive suite of regression tests has been added in tools/test_nbfmt.py to verify that Unicode separators are correctly preserved in markdown cells, outputs, and metadata. There are no review comments, and we have no feedback to provide.

@kkorpal kkorpal self-assigned this Oct 19, 2026
@kkorpal

kkorpal commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator

Thanks @BichengWang , nice catch. Reproduced locally on main (a cell with "before\u2028after" comes out of nbfmt as invalid JSON, Invalid control character) and confirmed your branch fixes it with the text preserved. nbfmt --test on all 154 notebooks still passes and the 20 tests are green.

One note: tools/test_nbfmt.py isn't picked up by CI yet (the workflow only runs tools/test_check_all_links.py). Leaving it to @Giom-V whether to keep the test file as-is, wire it into a workflow, or merge just the formatter fix.

@BichengWang

Copy link
Copy Markdown
Contributor Author

Added the formatter tests to the notebook workflow and enabled it for tool and workflow changes. All 20 formatter and link-checker tests pass locally.

@kkorpal

kkorpal commented Oct 22, 2026

Copy link
Copy Markdown
Collaborator

Thanks @BichengWang, the CI wiring looks right, the test is stdlib-only, the format/lint steps already handle a tools-only change, and the new step ran green on this PR. LGTM. Final call on the CI approach stays with @Giom-V

@kkorpal kkorpal added the type:bug Something isn't working label Oct 27, 2026
@kkorpal

kkorpal commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator

Thanks @BichengWang we appreciate the careful work. Per the updated CONTRIBUTING.md, we now only take tools/ changes for problems that affect main today, and this case doesn't occur in the repo at the moment, so we're closing it. If it ever shows up in practice, we'll gladly revisit.

@kkorpal kkorpal closed this Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status:awaiting review PR awaiting review from a maintainer type:bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants