Migrating from OpenAI JSON Mode
OpenAI JSON mode reduces failures but doesn’t eliminate them. Shim catches what JSON mode misses.The Problem with JSON Mode
OpenAI’sresponse_format: { "type": "json_object" } improves JSON output but has gaps:
1. Still Produces Invalid JSON
JSON mode can still return:- Truncated objects (missing closing brackets)
- Trailing commas
- Malformed decimals
- Unquoted keys (rare but happens)
2. No Schema Validation
JSON mode produces JSON, but not necessarily your schema:3. No Repair Feedback
JSON mode either succeeds or fails. No indication of what was fixed.Why Add Shim
Shim acts as a safety layer on top of JSON mode:- Catch remaining failures: Repair what JSON mode missed
- Schema validation: Enforce your schema with type coercion
- Confidence scoring: Know when repairs were needed
- Streaming support: Repair as tokens arrive
Migration Path
Before: JSON Mode Only
After: JSON Mode + Shim
Benefits
| Feature | JSON Mode Only | JSON Mode + Shim |
|---|---|---|
| Valid JSON | 95-98% | 99.9% |
| Schema validation | No | Yes |
| Type coercion | No | Yes |
| Repair visibility | No | Yes |
| Confidence scoring | No | Yes |
| Streaming repair | No | Yes |
Streaming with JSON Mode
Before: No Streaming Validation
After: Streaming with Shim
Schema Enforcement
Problem: JSON Mode Doesn’t Validate Schema
Solution: Shim Schema Validation
Confidence Feedback
Before: No Visibility
After: Confidence Scoring
Error Handling
Before: Try/Catch
After: Structured Errors
Cost Comparison
JSON Mode Only
JSON Mode + Shim Pro
Function Calling Alternative
Option 1: OpenAI Function Calling
More reliable than JSON mode for structured outputs:Option 2: Function Calling + Shim
Belt-and-suspenders approach:When to Use Shim
Use Shim If:
- JSON mode failures occur (even 1-2%)
- Schema validation needed
- Type coercion required
- Streaming repair wanted
- Repair visibility important
Skip Shim If:
- JSON mode has 100% success rate (rare)
- No schema validation needed
- Extra latency unacceptable (
<10ms) - Budget is extremely tight
Fallback Strategy
Use Shim as a fallback only:Production Checklist
- Enable JSON mode on OpenAI calls
- Add Shim API key to environment
- Install
shim-sdk(if using TypeScript) - Wrap JSON.parse with Shim repair
- Add schema validation
- Implement confidence-based logging
- Monitor repair rates in console
- Alert on high failure rates
Migration Strategies
Strategy 1: Safety Net (Recommended)
Always use Shim. Catch all failures.Strategy 2: Fallback Only
Try native parse first, Shim on failure.Strategy 3: Conditional (Advanced)
Use Shim only for complex schemas.FAQ
Should I still use JSON mode?
Yes. JSON mode reduces failures. Shim catches what remains.Can I use Shim without JSON mode?
Yes. Shim works with any LLM output (JSON mode or not).Does Shim add latency?
Yes,<10ms. Worth it for reliability.
What about Structured Outputs (new API)?
Structured Outputs are more reliable than JSON mode. Still, Shim adds schema validation and repair visibility.Can I use both function calling and Shim?
Yes. Function calling → Shim → your app.Next Steps
Quick Start
Get your first repair working
Schema Validation
Add JSON Schema validation
Streaming Guide
Repair streaming outputs
Confidence Levels
Understand confidence scoring
