FIX Pretty-print structured JSON assistant responses in chat bubble#1706
Open
romanlutz wants to merge 2 commits into
Open
FIX Pretty-print structured JSON assistant responses in chat bubble#1706romanlutz wants to merge 2 commits into
romanlutz wants to merge 2 commits into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
PromptShieldTargetreturning{"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
tryFormatJsonhelper inMessageListthat 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.tsxcover:<pre>(with round-tripJSON.parseto guard against data corruption)<pre>){not really json) renders as plain texttrue,42,"hello",null) render as plain textisLoading: true) is not reformattedExisting 43 tests continue to pass; lint and type-check are clean.
Screenshots
Before
After