Skip to content

feat(ai): replace JSON parsing with structured output via tool_use - #66

Merged
silver-snoopy merged 1 commit into
masterfrom
worktree-structured-output
Apr 13, 2026
Merged

feat(ai): replace JSON parsing with structured output via tool_use#66
silver-snoopy merged 1 commit into
masterfrom
worktree-structured-output

Conversation

@silver-snoopy

Copy link
Copy Markdown
Owner

Summary

  • Add completeStructured<T>() to AIProvider — uses Claude's tool_choice (forced tool call) and Gemini's responseSchema to guarantee schema-valid JSON responses
  • Migrate 3 LLM consumers — report generator, plan tuner, and plan parser all replaced fragile 3-tier JSON parsing (JSON.parse → jsonrepair → brace-matching) with structured output
  • Remove jsonrepair dependency — ~250 lines of parsing code eliminated, retry-on-parse-failure loop removed
  • Upgrade plan parser — from regex-only to LLM-powered with intelligent exercise classification, regex fallback when AI unavailable
  • Security fixes from review — PUT route rawText size cap, prompt injection scanning in plan parser LLM path, Gemini JSON.parse error handling
  • Documentation — architecture.md updated with workout-plans service tree, API endpoints, data model tables; ADE Phase 8 checklist added to CLAUDE.md

Files changed (17)

Layer Files Change
Shared ai.ts +StructuredOutputConfig interface, +completeStructured() on AIProvider
Providers claude-provider.ts, gemini-provider.ts Implement completeStructured()
Retry retry-utils.ts +completeStructuredWithRetry()
Report report-generator.ts Replace 3-tier parser with structured output
Tuner tuner.ts, tuner-prompt-builder.ts Replace parser + retry loop with structured output
Parser plan-parser.ts LLM structured output + regex fallback
Routes workout-plans.ts Pass aiProvider to parser, add PUT size cap
Deps package.json, package-lock.json Remove jsonrepair
Docs architecture.md, CLAUDE.md, plan status Architecture sync, ADE Phase 8 checklist
Tests 3 test files Updated mocks from completecompleteStructured

Test plan

  • 409 backend tests passing (43 test files)
  • 74 frontend tests passing (17 test files)
  • npm run lint — 0 errors
  • npm run format:check — clean
  • tsc build clean (shared + backend)
  • Code review: 3 parallel agents (Logic, Conventions, Security) — all HIGH/MEDIUM findings addressed
  • Integration test: generate a report with structured output against live AI provider
  • Integration test: tune a plan with structured output
  • Integration test: parse a free-text plan via LLM path

🤖 Generated with Claude Code

Replace fragile 3-tier JSON parsing (JSON.parse → jsonrepair → brace-matching)
across report generator, plan tuner, and plan parser with Claude's tool_use
structured output, guaranteeing schema-valid responses.

- Add StructuredOutputConfig interface + completeStructured() to AIProvider
- ClaudeProvider: tool_choice forced tool call extracts schema-constrained JSON
- GeminiProvider: responseMimeType + responseSchema for equivalent behavior
- Add completeStructuredWithRetry() to retry-utils
- Report generator: remove extractFirstJson, parseAIResponse, isValidScorecard
- Plan tuner: remove parseTunerResponse, extractFirstJson, retry-on-parse loop
- Plan parser: LLM structured output with regex fallback when AI unavailable
- Remove jsonrepair dependency
- Add rawText size cap to PUT /api/workout-plans/:id (security review fix)
- Add flagSuspiciousInput to plan parser LLM path (prompt injection defense)
- Update docs/architecture.md: file tree, API endpoints, data model tables
- Add Phase 8 documentation checklist to ADE workflow in CLAUDE.md

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@vercel

vercel Bot commented Apr 13, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
vitals Ignored Ignored Apr 13, 2026 9:32pm

@silver-snoopy
silver-snoopy merged commit 2df4b8d into master Apr 13, 2026
5 checks passed
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.

1 participant