fix: sanitize NaN/Infinity to null in serialize_json#43
Draft
saksharthakkar wants to merge 1 commit intomainfrom
Draft
fix: sanitize NaN/Infinity to null in serialize_json#43saksharthakkar wants to merge 1 commit intomainfrom
saksharthakkar wants to merge 1 commit intomainfrom
Conversation
…pliance Python's json.dumps() outputs bare NaN/Infinity tokens which violate RFC 8259. Strict JSON parsers (.NET, Go, Rust) reject these, causing OPS0006 errors in UpsertSpan/ShellReportStart when span attributes contain non-finite floats. Add _sanitize_nan() recursive walk before json.dumps() to convert NaN/Infinity to None (null), plus allow_nan=False as defense-in-depth. This matches the pattern used by orjson, pydantic, pandas, Datadog, and Sentry. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.
Summary
_sanitize_nan()to recursively replacefloat('nan'),float('inf'),float('-inf')withNonebefore JSON serializationallow_nan=Falsetojson.dumps()as defense-in-depth_sanitize_nanfor use by downstream packages (e.g.,uipath-tracingexporter)Root Cause (AE-1026)
The .NET backend returns
"Unexpected character encountered while parsing value"(OPS0006). This specific Newtonsoft.Json error means it hit an unexpected token likeNorIwhere it expected a JSON value — ruling out the ticket's stated cause of "special characters (backslashes, quotes, newlines)" whichjson.dumps()handles correctly.The only case where Python's
json.dumps()produces invalid JSON isNaN/Infinityfloats — it outputs bare tokens (NaN,Infinity) that violate RFC 8259 §6. These survive the full serialization chain:serialize_json()→_safe_parse_json()(Python'sjson.loadsaccepts NaN) → re-serialized viajson.dumps(attributes_dict)→Attributesstring hits .NET parser → failure. The intermittent nature (5 failures, 3 orgs) matches: only agent runs with tool outputs containing NaN floats (data processing, division-by-zero) trigger it.Approach
Recursive NaN →
nullsanitization beforejson.dumps()is industry-standard (e.g., pandas, Redash, orjson, pydantic v2, Datadog, Sentry all do the same). CustomJSONEncoder.default()does NOT work for floats since they're natively serializable, so the recursive walk is necessary.Test plan
null🤖 Generated with Claude Code