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
