Skip to content

fix(trajectories): preserve supplied Messages fields in saved traject… - #947

Open
shobhitagnihotri69 wants to merge 1 commit into
OpenPipe:mainfrom
shobhitagnihotri69:fix/messages-trajectory-serialization-parity
Open

shobhitagnihotri69 wants to merge 1 commit into
OpenPipe:mainfrom
shobhitagnihotri69:fix/messages-trajectory-serialization-parity

Conversation

@shobhitagnihotri69

Copy link
Copy Markdown

Summary

Preserves provider-supplied fields and explicit nulls on MessagesExchange (Anthropic Messages API) responses when saving trajectories, mirroring the exact behavior introduced for ResponsesExchange in #938.

Context & Implementation

In #938 (@bradhilton), ResponsesExchange introduced a custom @pydantic.field_serializer("response") with exclude_unset=True so that provider omissions and explicit nulls survive trajectory serialization and replay.

MessagesExchange suffered from the exact same defect:

  1. Without exclude_unset=True, saving Anthropic trajectories materialized unsupplied default fields (container: None, stop_details: None, etc.), cluttering serialized tapes and breaking round-trip equality.
  2. Under exclude_defaults=True, provider-supplied explicit nulls (e.g. stop_sequence: None) were dropped.

Changes:

  1. Added @pydantic.field_serializer("response") with exclude_unset=True to MessagesExchange in src/art/trajectories/__init__.py.
  2. Added comprehensive unit tests in tests/unit/trajectories/test_messages_serialization.py covering round-trips, explicit nulls, nested includes/excludes, and legacy tape preservation.

Verification

  • uv run pytest tests/unit/trajectories/test_messages_serialization.py tests/unit/trajectories/test_responses_serialization.py: 50 passed in 1.28s.
  • uv run ruff check src/art/trajectories/__init__.py tests/unit/trajectories/test_messages_serialization.py: All checks passed.

This branch has not been deployed

No deployments
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