fix: handle inf/nan float values in JSON serialization#880
Closed
r266-tech wants to merge 1 commit intovolcengine:mainfrom
Closed
fix: handle inf/nan float values in JSON serialization#880r266-tech wants to merge 1 commit intovolcengine:mainfrom
r266-tech wants to merge 1 commit intovolcengine:mainfrom
Conversation
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
|
|
|
Failed to generate code suggestions for PR |
Collaborator
|
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! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #871
Problem
OpenViking server crashes with
ValueError: Out of range float values are not JSON compliant: infwhen vector similarity calculations produce infinity values.Root Cause
FastAPI's default
JSONResponseusesjson.dumpswhich raisesValueErrorwhen encounteringinfornanfloat values, as these are not valid JSON.Fix
_sanitize_floats()helper that recursively replacesinf/nanwithNoneSafeJSONResponseclass that sanitizes content before JSON serializationapp.pyto useSafeJSONResponseThis ensures the server never crashes due to non-JSON-compliant float values, whether in normal responses or error responses.
Changes
openviking/server/app.py