Repository navigation
fix: harden orjson import and use explicit UTF-8 file encoding - #929
Closed
Abhijeet Prasad (AbhiPrasad) wants to merge 1 commit into
Closed
Abhijeet Prasad (AbhiPrasad) wants to merge 1 commit into
Abhijeet Prasad (AbhiPrasad) wants to merge 1 commit into
Conversation
- bt_json: fall back to stdlib json on any exception from `import orjson`, not just ImportError. orjson 3.12.0 raises SystemError on import under Python 3.15's `-X lazy_imports=all`, which broke the SDK on first use. - Pass `encoding="utf-8"` to text-mode file I/O in the SDK. Python 3.10-3.14 on Windows default to the locale encoding, so writing orjson output (which keeps non-ASCII characters) to a payload dump could fail and the dump was lost. Also covers the btx spec loader, `braintrust push`, the span cache, and harbor's job/trial/manifest/instructions files. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Abhijeet Prasad (AbhiPrasad)
deleted the
abhi/py315-orjson-encoding-hardening
branch
October 13, 2026 17:01
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.
Two small robustness fixes found during Python 3.15 work.
Changes
bt_json.py): fall back to stdlibjsonon any exception fromimport orjson, not justImportError. Under Python 3.15's global lazy-import mode (-X lazy_imports=all/PYTHON_LAZY_IMPORTS=all), orjson 3.12.0 raisesSystemErroron import (an upstream bug). Bareimport braintruststill succeeds because the SDK's submodules are deferred, but first real use (init_logger,bt_dumps) fails withImportError: lazy import of 'braintrust' raised an exception during resolution. orjson 3.13.0 is not affected, but ourtest-litellmgroup pins 3.12.0 on 3.15.encoding="utf-8"to text-mode file I/O in the SDK. Python 3.15 defaults to UTF-8 (PEP 686), but 3.10–3.14 on Windows use the locale encoding. The main site is_HTTPBackgroundLogger._write_payload_to_dir: orjson output keeps non-ASCII characters, so on a non-UTF-8 locale the write raisedUnicodeEncodeErrorand the payload dump was lost (an error is printed to stderr and the file is left empty). Also covers the btx spec loader,braintrust push, the span cache, and harbor's job/trial/manifest/instructions files. A scan of non-test code underpy/src/braintrustfinds no remaining sites.Validation
test_bt_json_falls_back_to_json_when_orjson_import_failssimulates aSystemErrorfrom the orjson import, with a 3.15-only-X lazy_imports=allvariant;test_write_payload_to_dir_uses_utf8runs with-X warn_default_encodingand warnings as errors).-X lazy_imports=all:init_loggerandbt_dumpsfail before, work after.'latin-1' codec can't encode characters), is written correctly after.test_core(3.14.6)test_core(3.15.0rc2)test_harbor(latest)(3.14; harbor doesn't support 3.15)pylintRunning the core and harbor suites with
PYTHONWARNDEFAULTENCODING=1reports noEncodingWarningfrom SDK source, only from tests,noxfile.py, scripts, and vcrpy. Pre-commit passes on the changed files.🤖 Generated with Claude Code
Co-authored by StarfolkAI (@starfolkai)[bot]