Stream partial json generated by LLMs into valid json responses
Project description
Structured Streamer
struct_strm (structured streamer) is a Python package that makes it easy to stream partial json generated by LLMs into valid json responses. This enables partial rendering of UI components without needing to wait for a full response, drastically reducing the time to the first word on the user's screen.
Why Use Structured Streamer?
JSON format is the standard when dealing with structured responses from LLMs. In the early days of LLM structured generation we had to validate the JSON response only after the whole JSON response had been returned. Modern approaches use constrained decoding to ensure that only valid json is returned, eliminating the need for post generation validation, and allowing us to use the response imediately. However, the streamed json response is incomplete, so it can't be parsed using traditional methods. This library aims to make it easier to handle this partially generated json to provide a better end user experience.
You can learn more about constrained decoding and context free grammar here: XGrammar - Achieving Efficient, Flexible, and Portable Structured Generation with XGrammar
Installation
pip install struct-strm
Main Features
The primary feature is to wrap LLM outputs to produce valid incremental JSON from partial invalid JSON based on user provided structures. Effectively this acts as a wrapper for your LLM calls. Due to the nature of this library (it is primarily inteded for use in web servers), it is expected that it will be used in async workflows, and is async first.
The library also provides simple HTML templates that serve as examples of how you can integrate the streams in your own components.
Due to the nature of partial json streaming, there can be "wrong" ways to stream responses that are not effective for partial rendering of responeses in the UI. The library also provides examples of tested ways to apply the library to get good results.
High Level Flow
Example Component
This is an example of a form component being incrementally rendered. By using a structured query response from an LLM, in this case a form with form field names and field placeholders, we can stream the form results directly to a HTML component. This drastically reduces the time to first token, and the precieved time that a user needs to wait. More advanced components are under development.
from stuct_strm import parse_openai
from pydantic import BaseModel
from openai import AsyncOpenAI
...
class DefaultFormItem(BaseModel):
field_name: str = ""
field_placeholder: str = ""
class DefaultFormStruct(BaseModel):
form_fields: List[DefaultFormItem] = []
stream_response = client.beta.chat.completions.stream(
model="gpt-4.1",
messages=messages,
response_format=DefaultFormStruct,
temperature=0.0,
)
form_struct_response = parse_openai(DefaultFormStruct, stream_response)
async for instance in form_struct_response:
async for formstruct in instance:
print(formstruct)
Fully formed python classes are returned:
>>> DefaultFormStruct(form_fields=[DefaultFormItem(field_name="fruits", field_placeholder="")])
>>> DefaultFormStruct(form_fields=[DefaultFormItem(field_name="fruits", field_placeholder="apple ")])
>>> DefaultFormStruct(form_fields=[DefaultFormItem(field_name="fruits", field_placeholder="apple orange strawberry")])
>>> etc....
And the corresponding incomplete json string streams would have looked like:
>>> "{"form_fields": [{"field_name": "fruits"
>>> "{"form_fields": [{"field_name": "fruits", "field_placeholder": "apple "
>>> "{"form_fields": [{"field_name": "fruits", "field_placeholder": "apple orange strawberry"}
>>> etc...
Component Streaming
The structured responses can then be easily used to generate incrementally rendered web components.
For example this form:
Other
I started struct_strm to support another project I'm working on to provide an easy entrypoint for Teachers to use LLM tools in their workflows. Check it out if you're interested - Teachers PET
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file struct_strm-0.0.10.tar.gz.
File metadata
- Download URL: struct_strm-0.0.10.tar.gz
- Upload date:
- Size: 16.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b8ae0023b66663b246671dec39c3e23bed6f8d2567807658ad0af6db598ddb5a
|
|
| MD5 |
17126061ea2a7ab737c17419ec3063c1
|
|
| BLAKE2b-256 |
a59e0ac115cd78162242aecbde4ef01aea0bc1445f22695ea26e26016be75cf0
|
Provenance
The following attestation bundles were made for struct_strm-0.0.10.tar.gz:
Publisher:
release.yaml on PrestonBlackburn/structured_streamer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
struct_strm-0.0.10.tar.gz -
Subject digest:
b8ae0023b66663b246671dec39c3e23bed6f8d2567807658ad0af6db598ddb5a - Sigstore transparency entry: 372930435
- Sigstore integration time:
-
Permalink:
PrestonBlackburn/structured_streamer@f49400f372084a18697a45df9830697122b0098d -
Branch / Tag:
refs/tags/v0.0.10 - Owner: https://github.com/PrestonBlackburn
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@f49400f372084a18697a45df9830697122b0098d -
Trigger Event:
push
-
Statement type:
File details
Details for the file struct_strm-0.0.10-py3-none-any.whl.
File metadata
- Download URL: struct_strm-0.0.10-py3-none-any.whl
- Upload date:
- Size: 29.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4b1aece9cc1ee8d89137abe40bf58eb03e65132ce453c25abcb32bec43be9085
|
|
| MD5 |
c5161b960c0b41041e7399d9cc923e67
|
|
| BLAKE2b-256 |
6b41274581f3206fc44878b7897c08602de5d3dda09450fdaabff9f4e070ecf5
|
Provenance
The following attestation bundles were made for struct_strm-0.0.10-py3-none-any.whl:
Publisher:
release.yaml on PrestonBlackburn/structured_streamer
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
struct_strm-0.0.10-py3-none-any.whl -
Subject digest:
4b1aece9cc1ee8d89137abe40bf58eb03e65132ce453c25abcb32bec43be9085 - Sigstore transparency entry: 372930451
- Sigstore integration time:
-
Permalink:
PrestonBlackburn/structured_streamer@f49400f372084a18697a45df9830697122b0098d -
Branch / Tag:
refs/tags/v0.0.10 - Owner: https://github.com/PrestonBlackburn
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@f49400f372084a18697a45df9830697122b0098d -
Trigger Event:
push
-
Statement type: