langdiff  by globalaiplatform

LLM structured output streaming for UIs

Created 5 months ago
264 stars

Top 96.8% on SourcePulse

GitHubView on GitHub
Project Summary

LangDiff addresses the complexities of streaming structured data from Large Language Models (LLMs) to frontend applications. It offers intelligent, token-by-token parsing with type-safe events and automatically generates JSON Patch diffs for efficient state synchronization. This enables developers to build responsive AI applications where backend LLM output schemas and frontend user experiences can evolve independently, enhancing maintainability and user experience.

How It Works

The library introduces two core innovations: "Intelligent Streaming Parsing" allows defining schemas using Pydantic-style models that inherently understand streaming progression, providing granular, type-safe callbacks (on_append, on_update, on_complete) as data tokens arrive. Complementing this, "Change-Based Synchronization" tracks mutations on application objects (Pydantic or plain Python) and automatically generates RFC 6902 JSON Patch diffs, including a custom append operation. This decouples frontend state from LLM output formats, facilitating independent evolution and efficient updates.

Quick Start & Requirements

  • Installation: uv add langdiff or pip install langdiff.
  • Prerequisites: The openai Python SDK is used in the provided example. No other specific hardware, OS, or CUDA requirements are detailed.
  • Resources: A runnable end-to-end demo is available at py/example.py.

Highlighted Details

  • Streaming Schemas: Define structured outputs using langdiff.Object, langdiff.List, langdiff.String for progressive LLM generation.
  • Type-Safe Callbacks: Receive fine-grained events (on_append, on_update, on_complete) as data streams in.
  • Change Tracking: Instrument existing Pydantic models or Python objects to automatically capture mutations.
  • JSON Patch Diffs: Efficiently synchronize frontend state by sending only changed data, leveraging RFC 6902 with an append extension.
  • Pydantic Interoperability: Derive Pydantic models directly from LangDiff schemas for seamless integration.

Maintenance & Community

No specific details regarding maintainers, community channels (like Discord/Slack), or roadmap were found in the provided README excerpt.

Licensing & Compatibility

  • License: Apache-2.0.
  • Compatibility: The Apache-2.0 license is generally permissive for commercial use and integration into closed-source projects.

Limitations & Caveats

The library is designed to address the limitations of traditional streaming JSON parsers, such as lack of type safety and granular control over partial data. The provided text does not explicitly state any limitations of LangDiff itself, such as alpha status, known bugs, or unsupported platforms.

Health Check
Last Commit

4 months ago

Responsiveness

Inactive

Pull Requests (30d)
0
Issues (30d)
0
Star History
6 stars in the last 30 days

Explore Similar Projects

Feedback? Help us improve.