llm_json_stream 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

Quick Start

import 'package:llm_json_stream/llm_json_stream.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');
}

Categories

Documentation

For detailed architecture documentation, see the doc/CONTRIBUTING/ folder.

Classes

BooleanPropertyStream
A property stream for JSON boolean values.
JsonStreamParser
A streaming JSON parser optimized for LLM responses.
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.
ParseEvent
Represents a log event from the JSON stream parser.
PropertyStream<T>
Base class for all property streams.
StringPropertyStream
A property stream for JSON string values.

Enums

ParseEventType
Types of parsing events that can be logged.

Functions

streamTextInChunks({required String text, required int chunkSize, required Duration interval}) Stream<String>
Utility function to stream text in chunks with configurable timing.