Skip to content

Commit 77f085d

Browse files
authored
Add github workflow to check notebook syntax is valid json (#407)
## Problem A notebook was accidentally committed with syntax errors that prevent it from being viewed or run. ## Solution - Add a test workflow to ensure all notebooks contain valid json on every commit. - Fix the broken notebook If you commit a notebook with broken JSON formatting, you will now see a failed workflow with error message like this: <img width="819" alt="Screenshot 2025-02-19 at 3 14 08 PM" src="https://github.com/user-attachments/assets/b682c6ae-1f06-4086-b563-edac3553bced" /> ## Type of Change - [x] Bug fix (non-breaking change which fixes an issue)
1 parent fa3ab56 commit 77f085d

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
import nbformat
3+
import os
4+
import sys
5+
6+
def validate_notebook(notebook_path):
7+
try:
8+
with open(notebook_path, "r", encoding="utf-8") as f:
9+
nb = nbformat.read(f, as_version=4)
10+
nbformat.validate(nb)
11+
print(f"Validated: {notebook_path}")
12+
except Exception as e:
13+
print(f"Validation failed for {notebook_path}: {e}", file=sys.stderr)
14+
return False
15+
return True
16+
17+
def main():
18+
has_error = False
19+
# Walk through the repository to find all .ipynb files
20+
failing_notebooks = []
21+
for root, _, files in os.walk("."):
22+
for file in files:
23+
if file.endswith(".ipynb"):
24+
notebook_path = os.path.join(root, file)
25+
if not validate_notebook(notebook_path):
26+
failing_notebooks.append(notebook_path)
27+
has_error = True
28+
29+
if has_error:
30+
print()
31+
print(f"The following notebooks did not conform to the expected JSON format:")
32+
for notebook in failing_notebooks:
33+
print(f" - {notebook}")
34+
print()
35+
print("Please fix these notebooks and push again.")
36+
sys.exit(1)
37+
38+
if __name__ == "__main__":
39+
main()

.github/workflows/validate.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Validate Notebook JSON
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
jobs:
10+
validate-notebooks:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
15+
- name: Set up Python
16+
uses: actions/setup-python@v5
17+
with:
18+
python-version: '3.11'
19+
20+
- name: Install dependencies
21+
run: pip install nbformat
22+
23+
- name: Validate all notebooks
24+
run: |
25+
python .github/scripts/validate-notebook-formats.py

learn/assistant/pinecone-assistant-getting-started.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"source": [
2121
"# Pinecone Assistant Getting Started\n",
2222
"\n",
23-
"Welcome to the getting started Notebook for [Pinecone assistant](https://www.pinecone.io/blog/pinecone-assistant/)!",
23+
"Welcome to the getting started Notebook for [Pinecone assistant](https://www.pinecone.io/blog/pinecone-assistant/)!"
2424
],
2525
"metadata": {
2626
"id": "IpHsLVa0mAJe"

0 commit comments

Comments
 (0)