Skip to content

fix: resolve state mutation by cloning graph before json conversion in save_graph (#796)#800

Open
suhaniiz wants to merge 1 commit into
param20h:devfrom
suhaniiz:fix/state-mutation-graph-796
Open

fix: resolve state mutation by cloning graph before json conversion in save_graph (#796)#800
suhaniiz wants to merge 1 commit into
param20h:devfrom
suhaniiz:fix/state-mutation-graph-796

Conversation

@suhaniiz

@suhaniiz suhaniiz commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

📋 PR Checklist

Thank you for contributing to PDF-Assistant-RAG! 🎉
Please fill out this template before submitting. PRs without it filled in will be closed.


🔗 Related Issue

Closes #796


📝 What does this PR do?

Resolves a silent state mutation bug where processing sequential document chunks in memory caused an unhandled AttributeError.

The build_graph function was calling _convert_sets_for_json(graph) right before returning, which mutated the underlying graph's node/edge properties (pages and chunks) from mutable Python set structures into sorted list structures. Consequently, subsequent in-memory operations attempting to call .add() on these properties failed.

This PR removes the mutation call from build_graph and safely decouples serialization by calling _convert_sets_for_json() on a shallow clone (graph.copy()) inside save_graph right before writing to disk.


🗂️ Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • 🔧 Refactor / code cleanup
  • 📝 Documentation update
  • 🎨 UI / styling change
  • ⚙️ CI / tooling / config change
  • 🧪 Tests

🧪 How was this tested?

  • Tested the affected API endpoints manually
  • Verified in-memory graph persistence structure remains intact across multi-batch processing cycles.

📸 Screenshots (if UI change)

N/A (Backend logic fix)


⚠️ Anything to flag for reviewers?

The fix uses a lightweight graph.copy() shallow copy from NetworkX inside save_graph. This ensures we don't manipulate the core operational graph being actively processed or maintained in memory while fully isolating the structural type adjustments needed strictly for JSON compliance.


✅ Self-Review Checklist

  • My branch is based on dev, not main
  • I have not added any secrets / API keys
  • I have not modified main branch or any HuggingFace deployment config
  • My code follows the existing style (no unnecessary formatting changes)
  • I have updated relevant docs / comments if needed

@suhaniiz suhaniiz requested a review from param20h as a code owner July 6, 2026 18:28
@vercel

vercel Bot commented Jul 6, 2026

Copy link
Copy Markdown

@suhaniiz is attempting to deploy a commit to the param20h's projects Team on Vercel.

A member of the Team first needs to authorize it.

@suhaniiz

Copy link
Copy Markdown
Contributor Author

@param20h , kindly review this, it is under gssoc!

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.

[BUG] : State Mutation Bug in Graph Build Phase

1 participant