Skip to content

fix(ai): bracketed prose no longer costs a reply its structured output (#786) - #800

Closed
dan-petty wants to merge 1 commit into
release/v0.2.24from
fix/bracketed-prose-structured-output
Closed

dan-petty wants to merge 1 commit into
release/v0.2.24from
fix/bracketed-prose-structured-output

Conversation

@dan-petty

Copy link
Copy Markdown
Owner

Summary

This is a critical fix for v0.2.24. 95ed4cc (#675), which fixed #656, made repair_json_string parse the whole reply before its fenced blocks. json-repair turns bracketed prose into list items. A reply that cites [OWASP A03](…) or items[0] before its ```json block, or request.args["id"] after it, therefore came back as a list such as `[["OWASP A03"], {"findings": [...]}]`. `parse_review_response` couldn't read that list and dropped every finding in the reply. The agent loop takes the same path for every structured output.

Fix

  • Fenced blocks are tried before the whole reply again. Each block's value is decoded with json.JSONDecoder.raw_decode, which reads string values whole, so a fence nested inside a string value never ends the block. That was the case fix(ai): response repair cuts fenced json at inner code fences and misreports validation errors #656 fixed.
  • Only a block that needs repair is cut, at a closing line that holds nothing but the backticks. A ```python line no longer closes it.
  • Repairing the whole reply remains the last resort, for unfenced or malformed replies.

Verification

Each reply holds the same three findings:

Reply Before After
Prose, then a ```json block 3 findings 3 findings
[OWASP A03](https://…) in the prose, then the block None 3 findings
`items[0]` in the prose, then the block None 3 findings
The block, then `request.args["id"]` None 3 findings
  • tests/test_response_repair_fences.py covers each table row through parse_review_response, nested fences inside a string value, a json block followed by a python example block, a fenced write_file tool call, a malformed block between bracketed prose, and a bare JSON reply. 7 of its 9 tests failed before the fix.
  • The fix(ai): response repair cuts fenced json at inner code fences and misreports validation errors #656 regressions in tests/test_batch_2_review_defects.py still pass.
  • The 26 test files that touch response repair, tool-call extraction, review parsing or agents: 531 passed.
  • devops ci passes; the test stage took 1m 33s.

Closes #786

#786)

95ed4cc (#675), the #656 fix, made repair_json_string parse a whole
reply before its fenced blocks. json-repair turns bracketed prose into
list items, so a reply citing [OWASP A03](...) or items[0] before its
```json block, or request.args["id"] after it, came back as a list such
as [["OWASP A03"], {"findings": [...]}]. parse_review_response could
not read it and dropped every finding; the agent loop takes the same
path for every structured output.

Fenced blocks come first again. Each block's value is decoded with
json.JSONDecoder.raw_decode, which reads string values whole, so a
fence nested in one never ends the block, the case #656 fixed. Only a
block that needs repair is cut, at a line holding nothing but the
closing fence. Repairing the whole reply stays the last resort.

Closes #786
@dan-petty

Copy link
Copy Markdown
Owner Author

Superseded by #802, which fixed #786 and is merged into release/v0.2.24.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects

Development

Successfully merging this pull request may close these issues.

1 participant