Software Development Community Question

What is the best way to stream partial structured data using Instructor?

Posted on 2025-05-25 • In Software Development • By Matthew Wilson

I'm building a real-time dashboard and I want to show the LLM's progress as it generates a large list of items. I’m using Instructor, but I'm not sure how to handle partial objects. Is there a specific way to iterate over a response so that the UI updates as each field is filled in, rather than waiting for the entire JSON block to finish?

3 Answers 9,887 Views

All answers to this question 3 Answers

Cynthia Roberts Answered on 2025-05-26
Answered

You should look into the Partial type provided by the library. When you wrap your model in Partial[MyModel], the library uses a specialized parser that can handle incomplete JSON strings. You then use stream=True in your completion call and iterate through the generator. Each iteration yields a Pydantic object with whatever fields have been populated so far. I used this for a travel itinerary generator where the flight details appeared instantly while the hotel descriptions were still being "typed" by the model. It makes the app feel incredibly fast.

Patrick Harris Answered on 2025-05-27
Answered

Can we use this streaming feature with local models like Llama 3 running through Ollama, or is it restricted to the OpenAI API?

Matthew Wilson Commented on 2025-05-28
Commented

Patrick, it definitely works with local models! As long as the provider supports the OpenAI-compatible streaming protocol, the library can patch it. I’ve personally tested it with Ollama and it works great for extracting structured data from local inference without the latency of a full round-trip.

Brian Anderson Answered on 2025-05-29
Answered

The user experience improvement is massive when you aren't staring at a loading spinner for ten seconds.

Cynthia Roberts Commented on 2025-05-30
Commented

Spot on, Brian. For long extractions, streaming partial results is the only way to keep users engaged.

Write a Comment

Your email address will not be published. Required fields are marked (*)

Join the LearnersEra Community

Ask questions, share knowledge and learn from professionals across technology, project management and certification topics.

Change Country and Nearest City

Select your country and nearest city to see courses available in your region.