feat(models): implement structured JSON output for OpenAI#2
Merged
Conversation
Replace regex-based response parsing with JSON Schema structured output: - Add StructuredReviewResponse type with per-comment severity, category, confidence - Add JSON Schema (src/schemas/review-response.json) for OpenAI response_format - Update OpenAI adapter to use json_schema response format - Add validation utilities for structured responses - Maintain backward compatibility with legacy free-text parsing as fallback - Add comprehensive tests for schema validation and conversion This eliminates the fragile regex parsing that could silently drop comments if the LLM formatted responses slightly differently. Refs: Phase 1 Task 3 - Structured JSON output from LLM
…mode OpenAI's json_schema strict mode requires ALL properties to be listed in the 'required' array. Changed summary and suggestion from optional to required (can be empty strings): - Update JSON schema to include 'summary' and 'suggestion' in required - Update TypeScript types to make fields required - Update validation logic to check for required fields - Update all tests to include summary and suggestion fields - Update prompt to clarify fields are required but can be empty
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.
Summary
Implements Phase 1 Task 3: Structured JSON output from LLM to replace fragile regex parsing.
Changes
New schema module (
src/schemas/):review-response.ts- TypeScript types for structured output (severity, category, confidence, title, explanation, suggestion)review-response.json- JSON Schema for OpenAI'sresponse_formatvalidate.ts- Validation utilities with fallback to legacy parsingvalidate.test.ts- 33 comprehensive testsUpdated OpenAI adapter (
src/models/openai.ts):json_schemaresponse format for reliable structured outputcritical | warning | suggestion | nitpick)Why
The previous regex-based parsing (
/([\w/.-]+):(\d+)\s*[—–-]\s*(.*?)(?=...)/gs) was fragile:Testing
All 94 tests pass:
Backward Compatibility
The implementation gracefully falls back to legacy free-text parsing if:
This ensures existing behavior is preserved while enabling the new structured format.