Class Stream<T>

java.lang.Object
com.langfuse.client.core.Stream<T>
Type Parameters:
T - The type of objects in the stream.
All Implemented Interfaces:
Closeable, AutoCloseable, Iterable<T>

public final class Stream<T> extends Object implements Iterable<T>, Closeable
The Stream class implements Iterable to provide a simple mechanism for reading and parsing objects of a given type from data streamed via a Reader using a specified delimiter.

Stream assumes that data is being pushed to the provided Reader asynchronously and utilizes a Scanner to block during iteration if the next object is not available. Iterable stream for parsing JSON and Server-Sent Events (SSE) data. Supports both newline-delimited JSON and SSE with optional stream termination.

  • Constructor Details

    • Stream

      public Stream(Class<T> valueType, Reader reader, String delimiter)
      Constructs a new Stream with the specified value type, reader, and delimiter.
      Parameters:
      valueType - The class of the objects in the stream.
      reader - The reader that provides the streamed data.
      delimiter - The delimiter used to separate elements in the stream.
  • Method Details

    • fromJson

      public static <T> Stream<T> fromJson(Class<T> valueType, Reader reader, String delimiter)
    • fromJson

      public static <T> Stream<T> fromJson(Class<T> valueType, Reader reader)
    • fromSse

      public static <T> Stream<T> fromSse(Class<T> valueType, Reader sseReader)
    • fromSse

      public static <T> Stream<T> fromSse(Class<T> valueType, Reader sseReader, String streamTerminator)
    • fromSseWithEventDiscrimination

      public static <T> Stream<T> fromSseWithEventDiscrimination(Class<T> valueType, Reader sseReader, String discriminatorProperty)
      Creates a stream from SSE data with event-level discrimination support. Use this when the SSE payload is a discriminated union where the discriminator is an SSE envelope field (e.g., 'event').
      Type Parameters:
      T - The type of objects in the stream.
      Parameters:
      valueType - The class of the objects in the stream.
      sseReader - The reader that provides the SSE data.
      discriminatorProperty - The property name used for discrimination (e.g., "event").
      Returns:
      A new Stream instance configured for SSE with event-level discrimination.
    • fromSseWithEventDiscrimination

      public static <T> Stream<T> fromSseWithEventDiscrimination(Class<T> valueType, Reader sseReader, String discriminatorProperty, String streamTerminator)
      Creates a stream from SSE data with event-level discrimination support and a stream terminator.
      Type Parameters:
      T - The type of objects in the stream.
      Parameters:
      valueType - The class of the objects in the stream.
      sseReader - The reader that provides the SSE data.
      discriminatorProperty - The property name used for discrimination (e.g., "event").
      streamTerminator - The terminator string that signals end of stream (e.g., "[DONE]").
      Returns:
      A new Stream instance configured for SSE with event-level discrimination.
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • iterator

      public Iterator<T> iterator()
      Returns an iterator over the elements in this stream that blocks during iteration when the next object is not yet available.
      Specified by:
      iterator in interface Iterable<T>
      Returns:
      An iterator that can be used to traverse the elements in the stream.