Skip to content

FIX Pretty-print structured JSON assistant responses in chat bubble#1706

Merged
romanlutz merged 5 commits into
microsoft:mainfrom
romanlutz:fix/gui-promptshield-raw-json
May 13, 2026
Merged

FIX Pretty-print structured JSON assistant responses in chat bubble#1706
romanlutz merged 5 commits into
microsoft:mainfrom
romanlutz:fix/gui-promptshield-raw-json

Conversation

@romanlutz
Copy link
Copy Markdown
Contributor

@romanlutz romanlutz commented May 10, 2026

Fixes finding documented in PyRIT-gui-findings/findings/bug-promptshield-raw-json/.

Summary

Targets that emit structured JSON instead of natural-language text — e.g. PromptShieldTarget returning {"userPromptAnalysis":{...}} — were dumped into the chat bubble as a single line of compact text. The user got no help reading the result and no visual hint that the response was structured rather than prose.

This change adds a tryFormatJson helper in MessageList that detects object- or array-shaped assistant content and renders it pretty-printed (2-space indent) inside a <pre> with monospace font, scoped height, and overflow auto so a large response cannot dominate the chat.

Plain text, malformed JSON, scalar JSON values (true / 42 / null), streaming/loading content, and user-typed JSON are all left as-is.

Tests

8 new tests in MessageList.test.tsx cover:

  • JSON object response renders as pretty-printed <pre> (with round-trip JSON.parse to guard against data corruption)
  • JSON array response renders pretty-printed
  • Plain text content is unchanged (no <pre>)
  • Malformed JSON-shaped content ({not really json) renders as plain text
  • User messages with JSON-shaped content are NOT reformatted
  • Scalar JSON values (true, 42, "hello", null) render as plain text
  • Streaming / loading content (isLoading: true) is not reformatted

Existing 43 tests continue to pass; lint and type-check are clean.

Screenshots

Before

before

After

after

romanlutz and others added 2 commits May 9, 2026 06:17
Targets that emit structured JSON instead of natural-language text
(e.g. PromptShieldTarget returning {"userPromptAnalysis":{...}})
were dumped into the chat bubble as a single line of compact text,
giving the user no help reading the result and no visual hint that
the response is structured rather than prose.

This change adds a tryFormatJson helper in MessageList that detects
object- or array-shaped assistant content and renders it pretty-
printed (2-space indent) inside a <pre> with monospace font, scoped
height, and overflow auto so a large response cannot dominate the
chat. Plain text, malformed JSON, scalar JSON values (true/42/null),
streaming/loading content, and user-typed JSON are all left as-is.

Tests cover all of those cases plus a round-trip JSON.parse on the
formatted output to guard against accidental data corruption.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@rlundeen2 rlundeen2 self-assigned this May 13, 2026
romanlutz and others added 3 commits May 12, 2026 20:43
Resolved conflict in frontend/src/components/Chat/MessageList.test.tsx by keeping both new describe blocks: 'structured JSON assistant responses' (this PR) and 'bubble class composition' (from main). They are independent test groups for different behaviors. All 52 tests in MessageList.test.tsx pass.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@romanlutz romanlutz enabled auto-merge May 13, 2026 04:11
@romanlutz romanlutz added this pull request to the merge queue May 13, 2026
Merged via the queue into microsoft:main with commit 5a23107 May 13, 2026
48 checks passed
@romanlutz romanlutz deleted the fix/gui-promptshield-raw-json branch May 13, 2026 04:38
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.

2 participants