liveBook · Manning

Text/event-stream

As explained in Spring AI in Action

text/event-stream is a MIME type for transmitting a stream of server-sent event data, with each event commonly represented using data-prefixed fields.

text/event-stream lets a Spring endpoint deliver an LLM response incrementally as a Flux, rather than waiting for the complete answer. Each streamed piece commonly arrives with a data: prefix. section 3.4.3

Configure the response with these implementation considerations:

  • Return the Flux produced by the question-answering service. section 3.4.3
  • Declare text/event-stream so clients process pieces as they become available instead of receiving one completed response. section 3.4.3
  • Handle data: prefixes on the client, or use application/ndjson when newline-delimited JSON is more convenient. section 3.4.3

Streaming a board-game answer

An HTTPie request asking about Burger Battle battle cards displays words incrementally, with a short delay between pieces that resembles typing. section 3.4.3

Listing 3.7 A board game service that streams the response
@Override
public Flux<String> askQuestion(Question question) {    #1
  var gameRules = gameRulesService.getRulesFor(question.gameTitle());

  return chatClient.prompt()
      .system(systemSpec -> systemSpec
          .text(promptTemplate)
          .param("gameTitle", question.gameTitle())
          .param("rules", gameRules))
      .user(question.question())
      .stream()         #2
      .content();
}

Inspect the service implementation that switches the chat client to streaming mode and returns generated content as a Flux. section 3.4.3

How Spring AI in Action covers it

How deep it goes
Explains Flux streaming, media-type configuration, data: prefixes, and an application/ndjson alternative.
Examples
Uses Burger Battle answers and HTTPie output showing one word per data-prefixed entry.
What it assumes
Assumes familiarity with Flux and Mono while treating Flux as zero-to-many available pieces.
What you can do after
Apply it via Spring annotations and Flux to expose incrementally generated LLM content over HTTP.
How it connects
Connects Flux, reactive responses, Spring endpoints, LLM generation, HTTPie, and newline-delimited JSON.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring AI in Action. The section links point to the parts of the book it draws on.