Agent-R1 icon indicating copy to clipboard operation
Agent-R1 copied to clipboard

Validate Observation representation invariant at construction time

Open Andrew9603 opened this issue 2 months ago • 0 comments

Summary

Observation documents that exactly one of text, messages, or token_ids should be set, but this invariant is currently not validated at construction time.

Consequently:

  • Observation() fails only later during prompt conversion.
  • An observation containing multiple representations is silently accepted, with _obs_to_prompt() selecting one according to its branch order.

Proposed change

Add a lightweight __post_init__() validation to Observation that raises ValueError unless exactly one representation is non-None.

The validation would use is not None, so intentionally empty representations such as Observation(messages=[]) remain valid.

I would also add CPU-only unit tests covering:

  • Each valid representation
  • Empty strings and empty lists
  • No representation
  • Conflicting combinations of multiple representations

This change would apply only to Observation. Action would remain unchanged because the rollout loop intentionally constructs it with both decoded text and token IDs.

Would this validation align with the intended API contract? I’d be happy to submit a PR if so.

Andrew9603 avatar Sep 15 '26 09:09 Andrew9603