Skip to content

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

Open
romanlutz wants to merge 2 commits into
microsoft:mainfrom
romanlutz:fix/gui-promptshield-raw-json
Open

FIX Pretty-print structured JSON assistant responses in chat bubble#1706
romanlutz wants to merge 2 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>
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