Skip to content

fix: handle inf/nan float values in JSON serialization#880

Closed
r266-tech wants to merge 1 commit intovolcengine:mainfrom
r266-tech:fix/inf-nan-json-serialization
Closed

fix: handle inf/nan float values in JSON serialization#880
r266-tech wants to merge 1 commit intovolcengine:mainfrom
r266-tech:fix/inf-nan-json-serialization

Conversation

@r266-tech
Copy link
Contributor

Fixes #871

Problem

OpenViking server crashes with ValueError: Out of range float values are not JSON compliant: inf when vector similarity calculations produce infinity values.

Root Cause

FastAPI's default JSONResponse uses json.dumps which raises ValueError when encountering inf or nan float values, as these are not valid JSON.

Fix

  1. Added _sanitize_floats() helper that recursively replaces inf/nan with None
  2. Added SafeJSONResponse class that sanitizes content before JSON serialization
  3. Updated both exception handlers in app.py to use SafeJSONResponse

This ensures the server never crashes due to non-JSON-compliant float values, whether in normal responses or error responses.

Changes

  • 1 file changed, 28 insertions(+), 2 deletions(-)
  • openviking/server/app.py

Add SafeJSONResponse class and _sanitize_floats helper to recursively
replace inf/nan float values with None before JSON serialization.

Fixes server crashes when vector similarity calculations produce
infinity values that are not JSON compliant.

Fixes volcengine#871
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@github-actions
Copy link

Failed to generate code suggestions for PR

@qin-ctx
Copy link
Collaborator

qin-ctx commented Mar 23, 2026

Thanks for the fix! 🙏

#882 has already been merged, which addresses the same issue (#871) by clamping inf/nan scores at the source (adapter and retriever layers). This way search results are returned correctly instead of falling through to an error response.

Since the root cause is now handled upstream, this PR is no longer needed. Going to close it — thanks again for jumping on this!

@qin-ctx qin-ctx closed this Mar 23, 2026
@github-project-automation github-project-automation bot moved this from Backlog to Done in OpenViking project Mar 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Server crashes with ValueError: Out of range float values are not JSON compliant: inf

4 participants