Skip to content

try orjson#6116

Draft
benedikt-bartscher wants to merge 17 commits intoreflex-dev:mainfrom
benedikt-bartscher:try-orjson
Draft

try orjson#6116
benedikt-bartscher wants to merge 17 commits intoreflex-dev:mainfrom
benedikt-bartscher:try-orjson

Conversation

@benedikt-bartscher
Copy link
Copy Markdown
Contributor

No description provided.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Feb 6, 2026

Merging this PR will not alter performance

✅ 9 untouched benchmarks


Comparing benedikt-bartscher:try-orjson (88cddb6) with main (c278404)

Open in CodSpeed

@benedikt-bartscher benedikt-bartscher marked this pull request as ready for review February 7, 2026 13:38
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Feb 7, 2026

Greptile Overview

Greptile Summary

This PR migrates from Python's standard json library to the faster orjson library across the codebase. The migration introduces wrapper functions orjson_dumps() and orjson_loads() in reflex/utils/format.py that gracefully fall back to stdlib json when orjson is not installed.

Key changes:

  • Added orjson as an optional dependency in pyproject.toml
  • Created wrapper functions with automatic fallback to stdlib json
  • Replaced json.dumps/json.loads calls throughout the codebase
  • Updated test expectations to match orjson's compact output format (no spaces after separators)

Issues found:

  • Exception handling in reflex/app.py:2175 still catches json.JSONDecodeError, but orjson.loads() raises orjson.JSONDecodeError instead. This will cause exceptions from orjson to bubble up uncaught.

Confidence Score: 3/5

  • This PR has a critical exception handling bug that could cause runtime failures
  • The migration is mostly well-executed with proper fallback mechanisms, but the exception handling issue in reflex/app.py is a critical bug that will cause orjson parsing errors to bubble up uncaught instead of being properly handled. This could lead to unhandled exceptions in the websocket event handling path.
  • Pay close attention to reflex/app.py - the exception handler at line 2175 needs to be fixed to catch the correct exception type from orjson

Important Files Changed

Filename Overview
reflex/app.py Replaced json.loads with format.orjson_loads in websocket and file reading logic, but exception handling still catches json.JSONDecodeError which won't work with orjson
reflex/utils/format.py Added orjson_dumps and orjson_loads wrapper functions with graceful fallback to stdlib json when orjson is unavailable
pyproject.toml Added orjson as optional dependency and included in dev dependencies
reflex/compiler/templates.py Replaced json.dumps with orjson_dumps for JavaScript template generation
reflex/utils/path_ops.py Replaced json.load/dump with orjson_loads/dumps for JSON file operations

Sequence Diagram

sequenceDiagram
    participant App as Reflex App
    participant Socket as WebSocket Handler
    participant Format as format.py
    participant OrJSON as orjson (optional)
    participant StdJSON as json (stdlib)
    
    App->>Format: orjson_dumps(data)
    Format->>Format: try import orjson
    alt orjson installed
        Format->>OrJSON: orjson.dumps()
        OrJSON-->>Format: bytes
        Format-->>App: str (decoded)
    else orjson not installed
        Format->>StdJSON: json.dumps()
        StdJSON-->>Format: str
        Format-->>App: str
    end
    
    Socket->>Format: orjson_loads(json_str)
    Format->>Format: try import orjson
    alt orjson installed
        Format->>OrJSON: orjson.loads()
        OrJSON-->>Format: dict/list
        Format-->>Socket: parsed data
    else orjson not installed
        Format->>StdJSON: json.loads()
        StdJSON-->>Format: dict/list
        Format-->>Socket: parsed data
    end
    
    Note over Socket: Exception handler catches<br/>json.JSONDecodeError<br/>but orjson raises<br/>orjson.JSONDecodeError
Loading

Copy link
Copy Markdown
Contributor

@greptile-apps greptile-apps Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment thread reflex/app.py
@adhami3310
Copy link
Copy Markdown
Member

the issue with orjson is that we lose NaN handling which is relevant for numpy, which is why we were using JSON5 as well

@benedikt-bartscher
Copy link
Copy Markdown
Contributor Author

benedikt-bartscher commented Feb 12, 2026

Yeah, that's why i did not update it in all places

benedikt-bartscher and others added 3 commits February 24, 2026 21:41
…llback

Happy path uses native JSON.parse; the catch rewrites Python"s bare
Infinity/-Infinity/NaN tokens outside string literals before retrying,
so only payloads that actually contain specials pay the extra cost.
NaN has no JSON literal and becomes null (matches JSON.stringify).
…o null

Swap bare NaN for a sentinel string before JSON.parse and revive it back
to a real NaN so Python-side float(nan) round-trips to the frontend.
@benedikt-bartscher
Copy link
Copy Markdown
Contributor Author

related #6339

@benedikt-bartscher benedikt-bartscher requested a review from a team as a code owner April 25, 2026 22:13
Apply orjson_dumps/orjson_loads helpers to the new reflex_base
package locations after upstream's restructuring.
@benedikt-bartscher benedikt-bartscher marked this pull request as draft April 26, 2026 16:50
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.

3 participants