Repository navigation
fix: preserve nested attachments in dataclass fields - #912
Gareth.Y (linjiaye929) wants to merge 2 commits into
Conversation
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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2e27140463
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "Codex (@codex) review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "Codex (@codex) address that feedback".
| visited_discard(obj_id) | ||
|
|
||
| try: | ||
| if dataclasses.is_dataclass(v) and not isinstance(v, type): |
There was a problem hiding this comment.
Preserve dataclass-based BaseAttachment instances
When a user implements the public BaseAttachment ABC as a dataclass, this branch matches before _to_bt_safe() can apply its BaseAttachment check, so bt_safe_deep_copy() converts the attachment into a plain field dictionary. _extract_attachments() only uploads values that remain BaseAttachment instances, so logging this otherwise-valid custom attachment silently skips its upload and reference replacement; the parent implementation preserved it by passing through _to_bt_safe(). Check the special attachment types before flattening dataclasses.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Confirmed and fixed in 30c6cbe. Dataclass BaseAttachment and ReadonlyAttachment subclasses now retain their existing special handling before generic dataclass traversal. Added 14 regression cases covering direct and nested serialization plus local logger extraction; all failed on the reviewed head and pass with the fix. The original dataclass regressions also remain green (28 focused tests total), and test_core passes 1004 tests. Logger coverage verifies extraction/scheduling and reference replacement without real uploads. AI-assisted follow-up.
Dataclass input/output with attachments inside a list, dict, or tuple loses those attachments during serialization. The field takes a string-encoding path, so the log contains
<braintrust.logger.Attachment object at ...>and attachment extraction finds nothing to upload. Direct attachment fields already work.Route dataclasses through the existing deep-copy walker. This preserves nested attachment identity, shares cycle/depth tracking across dataclasses and containers, and keeps the existing fallback for unreadable fields.
Tests cover direct and nested fields, dataclasses inside lists, shared references, cycles, depth limits, ordinary JSON fields, and unreadable fields. Logger input/output tests verify that both Attachment and ExternalAttachment reach flush-time extraction without making upload requests or mutating the original payload.
Validation on Python 3.12.14:
nox -s test_coresession: 990 passed, 73 skipped, 12 xfailed. This is the core suite without optional provider SDKs.test_bt_json.pyandtest_logger.pywith orjson 3.13.0 and Pydantic 2.14.0: 235 passed, 3 skipped.pylint --errors-onlyandgit diff --checkpassed.Checks used isolated environments.
make fixupcould not run becausemiseis unavailable, so itspre-commit run --all-filescommand was run directly. Other Python versions, provider integration suites, and real attachment uploads were not tested locally.AI assistance was used to implement and test this change.