json_stream_parser library

A streaming JSON parser optimized for LLM responses.

This library provides a JsonStreamParser that allows you to parse JSON data as it streams in, character by character. It's specifically designed for handling Large Language Model (LLM) streaming responses that output structured JSON data.

Features

  • Reactive property access: Subscribe to JSON properties as they complete
  • Path-based subscriptions: Access nested properties with dot notation
  • Chainable API: Fluent syntax for accessing nested structures
  • Type safety: Typed property streams for all JSON types
  • Array support: Access array elements by index and iterate dynamically

Usage

import 'package:llm_json_stream/json_stream_parser.dart';

void main() async {
  final parser = JsonStreamParser(streamFromLLM);

  // Subscribe to specific properties
  parser.getStringProperty('user.name').stream.listen((name) {
    print('Name: $name');
  });

  // Wait for complete values
  final age = await parser.getNumberProperty('user.age').future;
  print('Age: $age');
}

Classes

BooleanPropertyStream
A property stream for JSON boolean values.
JsonStreamParser
A streaming JSON parser optimized for LLM responses.
JsonStreamParserController
ListPropertyStream<T extends Object?>
A property stream for JSON array values.
MapPropertyStream
A property stream for JSON object (map) values.
NullPropertyStream
A property stream for JSON null values.
NumberPropertyStream
A property stream for JSON number values.
PropertyStream<T>
Base class for all property streams.
StringPropertyStream
A property stream for JSON string values.

Functions

streamTextInChunks({required String text, required int chunkSize, required Duration interval}) Stream<String>