Skip to content

Conversation

@benedikt-bartscher
Copy link
Contributor

No description provided.

@codspeed-hq
Copy link

codspeed-hq bot commented Feb 6, 2026

CodSpeed Performance Report

Merging this PR will not alter performance

Comparing benedikt-bartscher:try-orjson (1d44fd4) with main (5d1d6db)

Summary

✅ 8 untouched benchmarks

@benedikt-bartscher benedikt-bartscher marked this pull request as ready for review February 7, 2026 13:38
@greptile-apps
Copy link
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
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

try:
fields = json.loads(fields)
fields = format.orjson_loads(fields)
except json.JSONDecodeError as ex:
Copy link
Contributor

Choose a reason for hiding this comment

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

orjson.loads() raises orjson.JSONDecodeError, not json.JSONDecodeError. This exception handler will not catch errors from orjson.

Suggested change
except json.JSONDecodeError as ex:
except (json.JSONDecodeError, Exception) as ex:

Or import orjson at the top and catch both exception types properly.

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.

1 participant