fix(web): tolerate code-fenced JSON in Image Generation assistant#1617
Open
valter-silva-au wants to merge 1 commit into
Open
fix(web): tolerate code-fenced JSON in Image Generation assistant#1617valter-silva-au wants to merge 1 commit into
valter-silva-au wants to merge 1 commit into
Conversation
The Image Generation assistant prompts the model to return only a JSON
object, but in multi-turn sessions the model frequently wraps the 2nd+
response in a Markdown code fence (```json ... ```). The call site used a
bare JSON.parse, which then threw and dropped the prompt/preset, surfacing
an error on the second turn even though the response was otherwise valid.
Extract a pure parseImageAssistantContent helper that trims, strips a
surrounding code fence, and falls back to extracting the outermost {...}
when prose surrounds the JSON before parsing. On genuinely unparseable
output it returns the same error shape the call site produced before, so
truly-bad output still degrades gracefully. Add unit tests covering plain,
fenced, bare-fenced, whitespace-padded, prose-wrapped, and invalid inputs.
Fixes aws-samples#1392
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.
Description of Changes
Fixes #1392 — Image Generation fails on the second prompt because a
```jsoncode-block prefix causes a JSON parse error.Root cause. The Image Generation assistant prompts the model to return only a JSON object (
prompt/negativePrompt/comment/recommendedStylePreset), and the call site parsed each assistant message with a bareJSON.parse(m.content)(packages/web/src/components/GenerateImageAssistant.tsx). In multi-turn sessions the model frequently wraps the 2nd+ response in a Markdown code fence (```json ... ```). The bareJSON.parsethen throws, the code falls into itscatch, setserror: true, and the prompt/preset are lost — exactly the "1st prompt works, 2nd fails" symptom in the issue.Fix. Extract a small, pure helper
parseImageAssistantContent(raw)that:```json/ bare```),{ ... }when prose surrounds the JSON,JSON.parses and validates the result is a plain object.On genuinely unparseable output it returns the same
{ prompt: null, …, error: true }shape the call site produced before, so truly-bad responses still degrade gracefully (the existing retry UI is unchanged). The component call site now delegates to this helper.I fixed this on the parse side rather than hardening the system prompt because it is deterministic: regardless of whether the model emits a fence, the frontend recovers. A prompt-side change ("never use code fences") only lowers the failure rate — it can't eliminate it, since the model is probabilistic. The two are complementary; this PR is the robust half. Happy to adjust if maintainers prefer a prompt-side tweak as well.
No backend / CDK / API surface is touched; the change is a single call-site swap plus an additive, backward-compatible helper.
Checklist
npm -w packages/web run test(278 passing, incl. the new cases),npm run custom-lint:build && npm run web:lintclean,npm run web:buildgreen (Node 22)npm run cdk:test… — n/a (no CDK changes)Tests
Added
packages/web/tests/components/GenerateImageAssistant/parseImageAssistantContent.test.tscovering: plain JSON,```json-fenced, bare```-fenced, leading/trailing whitespace, prose-wrapped, invalid →error: true, and non-object JSON (primitive/array) →error: true. A fenced payload and its unfenced equivalent now parse to the identical object.Related Issues