schema-stream
Reference

Public API reference

Generated TypeScript reference for every public Schema Stream export.

This reference is generated with the installed TypeScript compiler API from every export reachable through src/index.ts. It documents the package entry point rather than implementation-only exports.

For complete usage, see the progressive JSON example, SDK examples, Mastra example, and Bun WebSocket UI.

Exports

NameKindDescription
SchemaStreamClassParses chunked JSON into schema-shaped intermediate values. SchemaStream does not validate chunks; consumers should validate the final value with their Zod schema.
OnKeyCompleteCallbackTypeReceives independent path snapshots as streamed values progress and complete.
OnKeyCompleteCallbackParamsTypeLegacy progress state reported after a streamed value changes.
OnValueCompleteCallbackTypeReceives the path and value when each primitive or container finishes parsing.
OnValueCompleteCallbackParamsTypeA single completed-value event without cumulative completion history.
SchemaInputTypeInfers the input value represented by a supported Zod schema.
SchemaPathTypeObject keys and array indexes locating a value in the streamed document.
SchemaStreamChunkTypeProgressive, schema-shaped value yielded for a supported object schema.
SchemaStreamDefaultDataTypeField-level placeholder overrides accepted when creating schema-derived stubs.
SchemaStreamInputChunkTypeA text or UTF-8 byte chunk accepted by iterate().
SchemaStreamOptionsTypeConfigures schema-derived placeholders and completion reporting.
SchemaStreamParseOptionsTypeConfigures JSON tokenization and snapshot cadence for parse() and iterate().
SchemaStreamSourceTypeA Web Stream or async iterable that supplies JSON text or UTF-8 bytes.
SchemaStreamValueTypeRecursively makes streamed fields optional and primitive values nullable.
SchemaStreamValuePathTypeObject keys and array indexes locating a syntactically complete JSON value.
SnapshotPolicyTypeControls when cumulative JSON snapshots are emitted. The default is chunk.
TypeDefaultsTypePrimitive placeholders used until streamed JSON supplies a value.
Zod3ObjectSchemaTypeZod 3 object schema contract required for shape inspection and type inference.
Zod3SchemaTypeMinimal structural contract used to support a Zod 3 schema without importing its runtime.
ZodObjectSchemaTypeObject schemas accepted by the public SchemaStream constructor.
ZodSchemaTypeSchema versions accepted by SchemaStream's compatibility layer.

Classes

SchemaStream

Parses chunked JSON into schema-shaped intermediate values. SchemaStream does not validate chunks; consumers should validate the final value with their Zod schema.

Kind: Class | Source: src/utils/streaming-json-parser.ts

class SchemaStream<TSchema extends ZodObjectSchema>

Type parameters

  • TSchema - Object schema that determines placeholders and snapshot inference.

constructor

Creates parser state and schema-derived placeholders for one streamed JSON document.

constructor(schema: TSchema, options?: SchemaStreamOptions<TSchema>)

Parameters

  • schema - Zod 3, Zod 4, or Zod Mini object schema used for types and placeholders.
  • options - Placeholder defaults and completion reporting.

getSchemaStub

Returns a new schema-derived stub using this instance's primitive defaults.

getSchemaStub<TStubSchema extends ZodObjectSchema>(schema: TStubSchema, defaultData?: SchemaStreamDefaultData<TStubSchema>): SchemaStreamChunk<TStubSchema>

Type parameters

  • TStubSchema - Object schema whose input type determines the returned stub.

Parameters

  • schema - Schema used to derive nested placeholders.
  • defaultData - Field-level placeholders that override derived defaults.

Returns

A new partial, schema-shaped value that is independent of parser state.

parse

Creates a transform that emits cumulative JSON snapshots at the selected cadence. Omitting snapshotPolicy preserves the existing one-snapshot-per-input-chunk behavior.

parse(options?: SchemaStreamParseOptions): TransformStream<Uint8Array, Uint8Array>

Parameters

  • options - Tokenizer behavior and snapshot cadence.

Returns

A byte transform whose outputs are serialized schema-shaped snapshots.

Throws

  • TypeError - When a byte snapshot threshold is not a positive finite integer.

iterate

Consumes streamed JSON text or bytes and yields independent schema-shaped snapshots. The completed value is still unvalidated; use the producing SDK's settled output or validate the final snapshot with the schema.

iterate<TChunk extends SchemaStreamInputChunk>(source: SchemaStreamSource<TChunk>, options?: SchemaStreamParseOptions): AsyncGenerator<SchemaStreamChunk<TSchema>, void, void>

Type parameters

  • TChunk - Source chunk type, inferred from the stream or async iterable.

Parameters

  • source - JSON text or UTF-8 bytes supplied with source backpressure.
  • options - Tokenizer behavior and snapshot cadence.

Returns

An async generator of independent schema-shaped values.

Throws

  • TypeError - When the source or byte snapshot threshold is invalid.
  • Error - When the source fails or the JSON is malformed or truncated.

Types

OnKeyCompleteCallback

Receives independent path snapshots as streamed values progress and complete.

Kind: Type | Source: src/utils/streaming-json-parser.ts

export type OnKeyCompleteCallback = (data: OnKeyCompleteCallbackParams) => void

OnKeyCompleteCallbackParams

Legacy progress state reported after a streamed value changes.

Kind: Type | Source: src/utils/streaming-json-parser.ts

export type OnKeyCompleteCallbackParams = {
  /** The value currently receiving streamed content, or an empty path after parsing finishes. */
  activePath: SchemaPath
  /** Unique value paths that have completed at least once, in completion order. */
  completedPaths: SchemaPath[]
}

OnValueCompleteCallback

Receives the path and value when each primitive or container finishes parsing.

Kind: Type | Source: src/utils/streaming-json-parser.ts

export type OnValueCompleteCallback = (event: OnValueCompleteCallbackParams) => void

OnValueCompleteCallbackParams

A single completed-value event without cumulative completion history.

Kind: Type | Source: src/utils/streaming-json-parser.ts

export type OnValueCompleteCallbackParams = {
  /**
   * Path of the value that just completed. Children complete before their containers, and an empty
   * path identifies the completed root document.
   */
  path: SchemaStreamValuePath
  /**
   * The syntactically complete JSON value at `path`. The parser does not clone container values for
   * this callback; consumers should treat objects and arrays as read-only because mutations can be
   * visible in later ancestor and root completion events.
   */
  value: unknown
}

SchemaInput

Infers the input value represented by a supported Zod schema.

Kind: Type | Source: src/utils/zod-compat.ts

export type SchemaInput<TSchema extends ZodSchema> = TSchema extends {
  _zod: z4.$ZodType["_zod"]
}
  ? z4.input<TSchema & z4.$ZodType>
  : TSchema extends Zod3Schema
    ? TSchema["_input"]
    : never

SchemaPath

Object keys and array indexes locating a value in the streamed document.

Kind: Type | Source: src/utils/streaming-json-parser.ts

export type SchemaPath = (string | number | undefined)[]

SchemaStreamChunk

Progressive, schema-shaped value yielded for a supported object schema.

Kind: Type | Source: src/utils/zod-compat.ts

export type SchemaStreamChunk<TSchema extends ZodObjectSchema> = SchemaStreamValue<
  SchemaInput<TSchema>
>

SchemaStreamDefaultData

Field-level placeholder overrides accepted when creating schema-derived stubs.

Kind: Type | Source: src/utils/zod-compat.ts

export type SchemaStreamDefaultData<TSchema extends ZodObjectSchema> = Partial<
  SchemaStreamChunk<TSchema>
>

SchemaStreamInputChunk

A text or UTF-8 byte chunk accepted by iterate().

Kind: Type | Source: src/utils/streaming-json-parser.ts

export type SchemaStreamInputChunk = string | Uint8Array

SchemaStreamOptions

Configures schema-derived placeholders and completion reporting.

Kind: Type | Source: src/utils/streaming-json-parser.ts

export type SchemaStreamOptions<TSchema extends ZodObjectSchema> = {
  /** Field-level placeholders that take precedence over schema and primitive defaults. */
  defaultData?: SchemaStreamDefaultData<TSchema>
  /** Fallback placeholders for primitive schema nodes. Defaults to `null`. */
  typeDefaults?: TypeDefaults
  /** Called as values progress and once more with an empty active path after completion. */
  onKeyComplete?: OnKeyCompleteCallback
  /**
   * Called once for each completed JSON value, including containers and the root document. Unlike
   * `onKeyComplete`, this callback emits a path delta and does not copy cumulative history.
   */
  onValueComplete?: OnValueCompleteCallback
}

SchemaStreamParseOptions

Configures JSON tokenization and snapshot cadence for parse() and iterate().

Kind: Type | Source: src/utils/streaming-json-parser.ts

export type SchemaStreamParseOptions = {
  /** Buffers string bytes in fixed-size blocks instead of emitting every incremental string. */
  stringBufferSize?: number
  /** Converts unescaped newlines inside strings to `\n`; enabled by default. */
  handleUnescapedNewLines?: boolean
  /** Selects when cumulative schema-shaped snapshots are emitted. */
  snapshotPolicy?: SnapshotPolicy
}

SchemaStreamSource

A Web Stream or async iterable that supplies JSON text or UTF-8 bytes.

Kind: Type | Source: src/utils/streaming-json-parser.ts

export type SchemaStreamSource<TChunk extends SchemaStreamInputChunk = SchemaStreamInputChunk> =
  | ReadableStream<TChunk>
  | AsyncIterable<TChunk>

SchemaStreamValue

Recursively makes streamed fields optional and primitive values nullable.

Kind: Type | Source: src/utils/zod-compat.ts

export type SchemaStreamValue<TValue> = unknown extends TValue
  ? unknown
  : TValue extends readonly (infer TItem)[]
    ? SchemaStreamValue<TItem>[]
    : TValue extends Record<PropertyKey, unknown>
      ? { [TKey in keyof TValue]?: SchemaStreamValue<TValue[TKey]> }
      : TValue | null

SchemaStreamValuePath

Object keys and array indexes locating a syntactically complete JSON value.

Kind: Type | Source: src/utils/streaming-json-parser.ts

export type SchemaStreamValuePath = readonly (string | number)[]

SnapshotPolicy

Controls when cumulative JSON snapshots are emitted. The default is chunk.

Kind: Type | Source: src/utils/streaming-json-parser.ts

export type SnapshotPolicy =
  | {
      /** Emits after every input chunk. */
      mode: "chunk"
    }
  | {
      /** Emits when an input chunk completes one or more primitive values. */
      mode: "value"
    }
  | {
      /** Emits after this many or more source bytes have arrived. */
      bytes: number
      mode: "bytes"
    }
  | {
      /** Emits one snapshot after the complete JSON document is parsed. */
      mode: "final"
    }

TypeDefaults

Primitive placeholders used until streamed JSON supplies a value.

Kind: Type | Source: src/utils/streaming-json-parser.ts

export type TypeDefaults = {
  string?: string | null | undefined
  number?: number | null | undefined
  boolean?: boolean | null | undefined
}

Zod3ObjectSchema

Zod 3 object schema contract required for shape inspection and type inference.

Kind: Type | Source: src/utils/zod-compat.ts

export type Zod3ObjectSchema = Zod3Schema & {
  readonly shape: Readonly<Record<string, Zod3Schema>>
}

Zod3Schema

Minimal structural contract used to support a Zod 3 schema without importing its runtime.

Kind: Type | Source: src/utils/zod-compat.ts

export type Zod3Schema = {
  readonly _def: unknown
  readonly _input: unknown
  readonly _output: unknown
}

ZodObjectSchema

Object schemas accepted by the public SchemaStream constructor.

Kind: Type | Source: src/utils/zod-compat.ts

export type ZodObjectSchema = Zod3ObjectSchema | z4.$ZodObject

ZodSchema

Schema versions accepted by SchemaStream's compatibility layer.

Kind: Type | Source: src/utils/zod-compat.ts

export type ZodSchema = Zod3Schema | z4.$ZodType

On this page