fix(ai): enforce JSON output and stop failing on truncation (#72) #75
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "fix/ai-json-generation"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem (#72)
AI jobs intermittently ended with
{"error": "LLM response was not valid JSON"}, often after a long (~55s) run.Root cause
finish_reasonwas ignored and Anthropic'sstop_reasonwas only checked forrefusal. The ~55s duration is the tell-tale sign of hitting the cap.Changes
response_format: {"type":"json_object"}for JSON jobs; mapfinish_reason == "length"to a newErrResponseTruncatedinstead of returning broken content.{to force a JSON object and stitch the brace back onto the reply; mapstop_reason == "max_tokens"toErrResponseTruncated.GenerateJSONhelper.extractJSONObjectwith a balanced-brace scan so prose containing}no longer derails parsing.Tests (added, TDD)
response_formatonly for JSON requests; reports truncation.{and stitches it back; reports truncation.extractJSONObjectsurvives surrounding prose with braces + nested objects.go test ./...andgo vet ./internal/ai/pass.Closes #72.
🤖 Generated with Claude Code