Streaming responses
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
send text to the client as it is produced.
Planned topics
- A long reply can take seconds. Streaming sends pieces as they are generated.
- Spring AI offers
.stream().content(), which returns a reactiveFlux<String>(one piece at a time). - A Spring MVC endpoint can return it as server-sent events (
text/event-stream); verify the exact setup before recording. - Users see the first words sooner, though total time is the same.
- Structured output is awkward while streaming; stream plain text, validate whole results.
Draft outline details
Demonstration
@GetMapping(value = "/describe/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
Flux<String> stream(@RequestParam String name) {
return chatClient.prompt().user("Describe " + name).stream().content();
}Check your understanding
What type carries the pieces?
Show answer
A Flux<String>.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.