Skip to content

fix: safe weight parsing to prevent ValueError and remove redundant typecasts (#804)#811

Open
suhaniiz wants to merge 1 commit into
param20h:devfrom
suhaniiz:bugfix/weight-accumulation-valueerror-804
Open

fix: safe weight parsing to prevent ValueError and remove redundant typecasts (#804)#811
suhaniiz wants to merge 1 commit into
param20h:devfrom
suhaniiz:bugfix/weight-accumulation-valueerror-804

Conversation

@suhaniiz

@suhaniiz suhaniiz commented Jul 7, 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 #804


📝 What does this PR do?

This PR resolves an inconsistent and fragile weight accumulation pattern inside get_entity_context that left the graph pipeline vulnerable to unexpected crashes.

Changes introduced:

  • Replaced the fragile int(edge.get("weight", 1)) assignment with a robust parsing pattern (int(float(raw_weight))) wrapped inside a structured try-except block. This safely handles float inputs, float strings (e.g., "2.5"), or arbitrary missing properties without raising a ValueError or TypeError.
  • Simplified the cumulative logic to use a clean += operator on the safely handled scalar.
  • Removed the redundant int() typecasting overhead from the final sorted() key lambda loop, as dictionary weight properties are now guaranteed numerical entities.

🗂️ Type of Change

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

🧪 How was this tested?

  • Ran the backend locally (uvicorn app.main:app --reload)
  • Tested the affected API endpoints manually with varied edge weight types
  • Added / updated tests

📸 Screenshots (if UI change)

N/A


⚠️ Anything to flag for reviewers?

The defensive try-except fallback assumes a standard default weight value of 1 if an unparseable data variant appears inside the knowledge graph attributes, keeping context generation intact without bringing down the core RAG server.


✅ 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 7, 2026 16:34
@vercel

vercel Bot commented Jul 7, 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.

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] : Redundant type casting and potential ValueError in get_entity_context weight accumulation

1 participant