Skip to main content

Streaming Repair Guide

Batch repair waits for the full output. Streaming repair processes JSON token-by-token as it arrives. No buffering delays.

Why Streaming Matters

The Problem:
  • LLMs stream tokens one at a time
  • Your UI wants to show progress
  • OutputFixingParser waits for full output (2-5s delay)
The Solution: Shim repairs JSON incrementally. Push chunks as they arrive. Get parseable JSON immediately.

How It Works

Three-Step Process

  1. Start Session → Get session_id
  2. Push Chunks → Send tokens as they arrive, get state back
  3. Finalize → Get repaired result with confidence score

Session Lifecycle


State Flags

Each push returns a StreamingState object:

Example State Progression


Buffer Bottleneck Solution

Problem: Waiting for full output adds latency. Solution: Shim’s streaming engine:
  1. Strips markdown fences early
  2. Holds incomplete tokens (e.g., "0.")
  3. Attempts parse on every chunk
  4. Returns partial as soon as parseable

Numerical Gyrations

Partial numbers can cause UI flicker:
Shim detects incomplete numbers and holds them:

Session Management

Expiration

Sessions expire after 60 seconds of inactivity.

Circuit Breaker

Sessions terminate at 5MB buffer (hallucination loop protection).

Complete Example


Best Practices

1. Check safe_to_emit Before Displaying

2. Handle Session Expiration

3. Set Max Tokens on LLM

Prevent hallucination loops:

4. Finalize When structurally_complete


Performance

  • Latency: <1ms per push
  • Memory: O(m) buffer where m = total output size (circuit breaker at 5MB)
  • Throughput: 1M+ chunks/sec per Worker

Next Steps

Streaming API Reference

Full API documentation

TypeScript SDK

Use the official SDK

Error Handling

Handle errors gracefully

Confidence Levels

Understand confidence scoring