Fluxio
← Retour au blog

Streaming Structured JSON: A Step-by-Step Guide

SSE et APIEN

Structured JSON output with streaming responses requires two separate decisions: what data your application accepts, and what users can see before generation finishes. Build those paths separately. This walkthrough uses a support-ticket extractor that previews incoming fields while reserving database writes for the completed, validated result.

§1 Step 1: Define the Ticket JSON Schema

Start with three fields: a string named summary, a priority enum containing low, medium, and high, and an array of strings named action_items. Require every field and disallow additional properties. For missing information, define an explicit policy instead of asking the model to invent values.

Use schema-constrained structured output where supported. JSON mode targets valid JSON, whereas Structured Outputs targets adherence to a supplied schema. Neither should replace your application’s business checks. ([developers.openai.com](https://developers.openai.com/api/docs/guides/structured-outputs?utm_source=openai))

For this extractor, instruct the model to summarize only the supplied ticket, choose priority from documented criteria, and return an empty action_items array when no actions are justified.

§2 Step 2: Verify the Streaming Route

Before building the interface, run a capability test against your exact model, endpoint, and relay route. Check whether streaming and schema constraints work together, which schema keywords are accepted, and how refusals or interruptions appear. Treat transport compatibility and structured-output support as separate acceptance criteria.

Fluxio fits teams seeking a streaming-first, pay-per-token relay with one key across providers and OpenAI-style client compatibility. Use it to simplify access, but verify the chosen route rather than assuming every upstream model exposes identical structured-output behavior.

§3 Step 3: Request a Schema-Constrained Stream

For a compatible Chat Completions route, configure stream: true alongside response_format using type: "json_schema", a schema name, your schema, and strict: true. OpenAI’s Responses API places structured-output configuration under text.format instead. Keep these request shapes separate. ([developers.openai.com](https://developers.openai.com/api/docs/guides/structured-outputs))

Keep the API key on your server. Have your backend initiate the upstream request and forward only the preview events your frontend needs. Set an output budget appropriate for a short ticket summary, and attach an application request identifier for tracing.

§4 Step 4: Accumulate JSON Text Deltas

Read the SDK’s streaming events and append only the relevant output-text deltas to a request-local buffer. Do not concatenate complete event envelopes or mix tool arguments with the ticket object. OpenAI streaming uses server-sent events, with event types identifying different stages and payloads. ([developers.openai.com](https://developers.openai.com/api/docs/guides/streaming-responses))

Do not call a normal JSON parser on each incoming chunk. Structured JSON streams can contain incomplete fragments that become a complete object only after concatenation. A chunk boundary is not an object boundary. ([ai.google.dev](https://ai.google.dev/gemini-api/docs/structured-output))

§5 Step 5: Render Provisional JSON Fields

Maintain separate preview and committed states. Feed accumulated text into an incremental parser designed for incomplete JSON, or use an SDK’s structured-streaming helper where available. OpenAI documents streaming support for processing structured data during generation. ([developers.openai.com](https://developers.openai.com/api/docs/guides/structured-outputs?utm_source=openai))

  • – Show an emerging summary with a visible generating indicator.
  • – Keep the priority badge provisional until the complete result passes validation.
  • – Render action items as text, never as executable instructions.
  • – Throttle preview updates to avoid unnecessary interface rendering.

Do not repair previews by blindly adding closing braces. Design the interface so missing or revised fields remain harmless, and disable actions that depend on a committed ticket.

§6 Step 6: Validate Before Committing the Result

After successful stream completion, parse the full buffer once and validate it against the same schema. Then apply business rules, such as requiring evidence for a high-priority classification. Schema adherence does not establish factual correctness; Google explicitly recommends validating semantic correctness in application code. ([ai.google.dev](https://ai.google.dev/gemini-api/docs/structured-output))

Commit the ticket only after these checks succeed. Handle refusal, truncation, cancellation, and transport failure as distinct outcomes. For recoverable failures, use a bounded retry with a fresh buffer, and prevent duplicate writes with an application-level idempotency key.

§7 Step 7: Test Interrupted JSON Streams

Test quoted text, escaped characters, empty arrays, long summaries, and disconnects midway through a string. Track time to first preview, time to validated completion, validation failures, and retry cost. Your release criterion should be simple: fast previews never become trusted records prematurely.

Sign up for Fluxio to build your streaming workflow.

Créer un compte Fluxio →

Plus d'articles du courant