Skip to main content

Stream partial json generated by LLMs into valid json responses

Project description

Struct Strm Logo

License: MIT Codestyle Black Build Status Coverage

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


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
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.

class DefaultFormItem(BaseModel):
    field_name: str
    field_placeholder: str

class DefaultFormStruct(BaseModel):
    form_fields: List[DefaultFormItem]

# a typical openai structured response stream may look like: 
...
async with client.beta.chat.completions.stream(
    model="gpt-4.1",
    messages=messages,
    response_format=DefaultFormStruct,
    temperature=0.0,
) as stream:
    async for event in stream:
        ...
# where the resulting stream is used to incrementally build the component
# (shown below)

Example Form Streaming

Contributing

Test

pytest

Format

python -m black ./

Docs

mkdocs serve

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

struct_strm-0.0.5.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

struct_strm-0.0.5-py3-none-any.whl (25.2 kB view details)

Uploaded Python 3

File details

Details for the file struct_strm-0.0.5.tar.gz.

File metadata

  • Download URL: struct_strm-0.0.5.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for struct_strm-0.0.5.tar.gz
Algorithm Hash digest
SHA256 24d8e89ba97a3870fb993981a39aeb5735b9e08126a48671efd71dbfe11f1318
MD5 c340b8bfeb4a8dc0867928431d113114
BLAKE2b-256 4aa647472b4ced4557a5c8e3f161ea18c2aaeb9add5ba94116e29f044f0c66c5

See more details on using hashes here.

Provenance

The following attestation bundles were made for struct_strm-0.0.5.tar.gz:

Publisher: build_and_publish.yaml on PrestonBlackburn/structured_streamer

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file struct_strm-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: struct_strm-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 25.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for struct_strm-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 e3ed0cda9dcc34e990847952acc80cca6da072f284af5c828db7bc01caa9d31a
MD5 46677c7f8ab5f3a76fe6c7ae877b4659
BLAKE2b-256 76540cd1a5cb0f42c59820db16bd20fb934932d65ceb628a1cd6fea4cb52adfe

See more details on using hashes here.

Provenance

The following attestation bundles were made for struct_strm-0.0.5-py3-none-any.whl:

Publisher: build_and_publish.yaml on PrestonBlackburn/structured_streamer

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page