Skip to content

Fix pp of an Array of messages - #71

Merged
raghubetina merged 2 commits into
mainfrom
fix-pp-of-message-arrays
Oct 19, 2026
Merged

raghubetina merged 2 commits into
mainfrom
fix-pp-of-message-arrays

Conversation

@raghubetina

Copy link
Copy Markdown
Contributor

pp chat.messages, and IRB's echo of chat.messages, garbled the separators:

[{
       role: "system",
    content: "You are helpful"
},{
       role: "user",
    content: "Draw a picture of a kitten"
}{
        role: "assistant",
...
} , ]

AI::Message#pretty_print writes directly to q.output so that IRB's ColorPrinter can't re-colorize (and escape) amazing_print's ANSI codes. Inside an Array, PP is still holding [ and the , separators in its buffer, so each message's direct write lands ahead of them. q.flush first emits what PP has buffered, so the output becomes:

[{
       role: "system",
    content: "You are helpful"
}, {
       role: "user",
    content: "Draw a picture of a kitten"
}]

This matches p chat.messages. AI::Items#pretty_print had the same direct write and gets the same fix.

Specs

Until now the formatting specs only checked that inspect returns a String. This adds exact-layout specs for:

  • Chat#inspect: a new chat, and a proxy-mode chat with messages (the output the AI::Chat Examples lesson shows verbatim).
  • pp of an Array of messages, and of an Array of Items.

With the lib/ change reverted, exactly the two Array specs fail. The output is identical on Ruby 3.3.11 and 4.0.1: amazing_print 2.0 prints role: style keys on both.

Testing

  • rspec spec/unit: 101 examples, 0 failures on Ruby 3.3.11 and 4.0.1.
  • IRB echo of a.messages with --nocolorize and with colors forced on: separators correct, ANSI codes kept and not escaped.
  • standardrb clean.

Follow-up worth considering, not in this PR: in Chat#inspect, the closing ] of @messages prints at column 0 instead of under @messages. The new spec pins that layout as it is today, so fixing it later means updating the spec.

Message#pretty_print writes straight to q.output so IRB's
ColorPrinter can't escape our ANSI codes. Inside an Array, though, PP
is still buffering the "[" and the separators between elements, so
`pp chat.messages` printed every message first and every comma last:
`[{...},{...}{...} , ]`. Flushing first puts the buffered text out
ahead of each message. Items#pretty_print gets the same fix.

New specs pin the exact plain-text layout of Chat#inspect and of an
Array of messages. Lessons show this output verbatim, and until now
the specs only checked that inspect returned a String.
@raghubetina
raghubetina merged commit fb29ac8 into main Oct 19, 2026
2 checks passed
@raghubetina
raghubetina deleted the fix-pp-of-message-arrays branch October 19, 2026 18:26
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