Skip to content

fix(web): tolerate code-fenced JSON in Image Generation assistant#1617

Open
valter-silva-au wants to merge 1 commit into
aws-samples:mainfrom
valter-silva-au:fix/image-assistant-json-fence-parse
Open

fix(web): tolerate code-fenced JSON in Image Generation assistant#1617
valter-silva-au wants to merge 1 commit into
aws-samples:mainfrom
valter-silva-au:fix/image-assistant-json-fence-parse

Conversation

@valter-silva-au

Copy link
Copy Markdown

Description of Changes

Fixes #1392 — Image Generation fails on the second prompt because a ```json code-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 bare JSON.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 bare JSON.parse then throws, the code falls into its catch, sets error: 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:

  1. trims whitespace,
  2. strips a surrounding Markdown code fence (```json / bare ```),
  3. as a last resort, extracts the outermost { ... } when prose surrounds the JSON,
  4. then 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

  • Modified relevant documentation — n/a (internal bug fix, no user-facing docs)
  • Verified operation in local environment — npm -w packages/web run test (278 passing, incl. the new cases), npm run custom-lint:build && npm run web:lint clean, npm run web:build green (Node 22)
  • Executed npm run cdk:test … — n/a (no CDK changes)

Tests

Added packages/web/tests/components/GenerateImageAssistant/parseImageAssistantContent.test.ts covering: 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

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
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.

Image Generation fails on second prompt due to ```json code block prefix causing JSON parse error

1 participant